0% found this document useful (0 votes)
8 views

Java Theory QuestionsC10 Class as the Basis of all Computation - Copy

Java

Uploaded by

ishanjadhav077
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java Theory QuestionsC10 Class as the Basis of all Computation - Copy

Java

Uploaded by

ishanjadhav077
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Theory Questions – Class 10

----------------------------------------------------------------------------------------------------------------

Questions on Class as the Basis of all Computation

1. What is state and behaviour? Give one example.


Ans. The variables declared within the class represent state and the methods which utilize these
variables represent behaviour.
Example:
class Demo{
int x;
char ch; // these are data members represents state
public void show() //working on state or variable represents behaviour
{
x = 39;
x = x 30;
ch += 2;
}
}

2. Explain local variables, instance variables and class variables?


Ans.
i. Local variables: are used inside blocks or in methods as temporary variables and are
used to store information needed by that particular block method.
ii. Instance variables: are used to define attributes or the state of a particular object and are
used to store information needed by multiple objects of a class.
iii. Class variables: are global to a class and to all the instances of the class and are useful
for communicating between different objects of all the same classes or keeping track of
global states. They are declared using "static" keyword.

3. What are different accessibility modes in Java?


Ans. The different accessibility modes of java are:
i. public
ii. private
iii. protected
iv. default

4. Give the difference between primitive data type and reference data type with one
example.
Ans.

1
Primitive data type Reference data type
1. The fundamental data types available 1. They are complex data types
with Java are known as primitive constructed by combining primitive
data type. data types.
2. They are defined by Java 2. They are defined by programmers
3. They can store only one vale 3. They can store multiple values either
of same type or multiple
4. Example: int, float, char, double, 4. Example: class, arrays, reference.
long, short, byte, boolean.

5. What is a static method or function in Java?


Ans. A static method is a method that belongs to a class rather than an instance of a class.
• The static keyword makes a method/ function as a class function ( also known as static
method or function),
• They do not require an instance of class to be created.
• It can access only static methods of the class.

Example:
private static final int c_Year = current_year();
public static int current_year(){
return 2019;
}

6. What is the difference between a class variable and instance variable?


Ans.
Class variable Instance variable
1. A data member which is declared only for 1. The data member which is created for
a class and all the objects of this class every object of the class and is not shared
shares this data member is known as class is known as instance variable.
variable.
2. A single copy of the variable is accessible 2. Each object holds its own copy of this
to all objects. variable.

3. The keyword static is used make a 3. Only primitive data types are used to
variable as class variable. make a variable as instance variable.
4. Example. 4. Example.
class data class data
{ {
static int a; // ‘a’ is class variable. int p, q; // ‘p’ and ‘q’ are instance
} //variable.
}

2
7. What is composite data type? Give an example.
Ans. Group of more than one primitive data type or reference data type as a single unit (Class) is
known as composite data type. A class is known as composite data type.
Example.
class Demo
{
int x;
float m;
char ch;
}

8. What is static variable?


Ans. The static variable is also known as class variable and declared using keyword “static”.
Static variable are shared by all instances or objects of a class.
Example: static int m, n;

9. Why class is also called composite data type? Give examples.


Ans. The class is composed of multiple primitive data types that is why it is known as
composite data type.
Example:
class Tst{
int x, float y;
boolean result;
public int check()
{
System.out.println(result);
}
}

10. What are objects? Why are objects called instance of the class?
Ans. A Java object is a combination of data and procedures working on the available data. The
object has some characteristics and behaviour. An object is instantiated from a class hence it
is also referred to as instance of a class.

You might also like