The document contains 20 multiple choice questions about Java concepts such as constructors, methods, variables, access modifiers, and object passing. Each question is accompanied by a short explanation of the answer. The questions cover topics like default constructors having no parameters, instance methods being associated with individual objects, private instance variables being accessible within the class, and static variables being shared among all class instances.
Salient Features of India constitution especially power and functions
Question 1How many parameters does a default constructor haveAn.pdf
1. Question 1
How many parameters does a default constructor have?
Ans) It never has any
Explanation:
The default constructor doesn’t have any parameters.Thats why it is also called as Zero-
Argumented constructor.
Even The default constructor doesn’t return anything even void also.
___________________________________________________________________
Question 2
Which of the following statements is true about the class shown below?
class Quiz1A
{
private int[] array = new int[10];
public int[] getArray()
{
return array;
}
}
Ans) It is immutable
Explanation:
Here the getArray() method is not doing any operations inside it.It just returning an array.So it is
also called as getter method.
______________________________________________________________________
Question 3
A method that is associated with an individual object is called __________.
Ans) an instance method
Explanation:
For every class we can create many objects.If we want to call a method on object, we have to call
a method by using the reference of it.That method is confined to that object only.
______________________________________________________________________
Question 4
Given the declaration Circle[] x = new Circle[10], which of the following statement is most
accurate?
Ans) x contains a reference to an array and each element in the array can hold a reference to a
2. Circle object.
Explanation:
This array will hold the references of Circle class objects.It will not store objects.
______________________________________________________________________
Question 5
What is the output of the program shown below?
public class Quiz1B
{
public static void main(String[] args)
{
Count count = new Count();
int times = 0;
for (int i = 0; i < 100; i++)
increment(count, times);
System.out.println("times = " + times + " count = " + count.times);
}
public static void increment(Count count, int times)
{
count.times++;
times++;
}
}
class Count
{
int times;
public Count()
{
times = 1;
}
}
Ans) times = 100 count = 101
3. Explanation:
This for loop in the main method will be executed for 100 times.So the value of the variable of
main method times will increased by 1 for each iteration.As the initial value of times is 0 and
finally it will become 100.after 100 iterations
As the initial value of instance variable of Count class is 1.After 100 iterations its value will
become 101
______________________________________________________________________
Question 6
When calling a method with an object argument, ___________ is passed.
Ans) the reference of the object
Explanation:
We have to pass the reference of an object as argument while calling.not the copy or objects
contents or else.
______________________________________________________________________
Question 7
Which of the following is not a property of a constructor?
Ans) The name of a constructor can be chosen by the programmer
Explanation:
We can overload the constructor.
The constructor is called and executed when we are creating an object by using new operator.
The name of the constructor should be the name of the class.We cannot choose the name of the
constructor.
______________________________________________________________________
Question 8
What is the output of the program shown below?
public class Quiz1C
{
public static void main(String[] args)
{
int i = 1;
StringBuilder s = new StringBuilder("s");
String t = "t";
someMethod(i, s, t);
System.out.print("i = " + i + ", ");
4. System.out.print("s = " + s + ", ");
System.out.println("t = " + t);
}
private static void someMethod(int i, StringBuilder s, String t)
{
i++;
s.append("s");
t += "t";
}
}
Ans) i = 2, s = ss, t = tt
Explanation:
We are calling the method someMethod(int i, StringBuilder s, String t) by passing the int
variable,references of StringBuilder and String
Inside that i value will be incremented by 1
Appending s to the content of StringBuilder Object.
Concatenating t to the String “t”
______________________________________________________________________
Question 9
Which of the following statements is true about the order of methods in a class.
Ans) They can be listed in any order
Explanation:
We can declare the methods inside the class in any order .there is no restrictions on the order of
declaration of methods.
______________________________________________________________________
Question 10
Which of the following statements about constructors is true?
Ans) A default constructor is provided automatically if no constructors are explicitly declared in
the class
Explanation:
If we didnt provided any constructor in a class,the java compiler will provide the code for default
constructor inside the class while compiling the class.
______________________________________________________________________
Question 11
Which of the following is true about the program shown below?
5. class Quiz1D
{
private int x;
public Quiz1D(int x)
{
this.x = x;
}
}
Ans) If the this qualifier is removed, it will compile but won't initialize the instance variable
Explanation:
This refers to the current class instance variable.Even If we didn’t provide ‘this’ also our
program will compile but the instance variable will not get initialized.So the instance variable
will be initialized with the default value.
______________________________________________________________________
Question 12
Which of the following statements about local variables is true?
Ans) A compilation error results when a local variable is accessed before it is initialized.
Explanation:
Even if we didn’t initialize the during compilation instance variables will be initialized with their
default values.
But if we want to use local variables compulsory we have to initialize it.If not we will get
compilation error.
______________________________________________________________________
Question 13
Analyze the code shown below and indicate which of the following is true:
class Quiz1E
{
private double variable;
public Quiz1E(double variable)
{
variable = variable;
}
6. }
Ans)
The program will compile, but you cannot create an object of Quiz1E with a specified value for
variable
Explanation:
As we didn’t provide ‘this’ so the variable will not get initialized.
We wont get any compilation error.But the instance variable value of Quiz1E class will be
initialized with the default values by the java compiler.So as it is double data type it will be
initialized with 0.0
______________________________________________________________________
Question 14
Which of the following is true of public methods?
Ans) They can only be accessed by methods in a different package
Explanation:
The scope of the public access specifier is global.So we can access public methods from
anywhere like with in the class or from with in the package or from outside the package.
______________________________________________________________________
Question 15
Which of the following statements is true about an immutable object?
Ans) The contents of an immutable object cannot be modified
Explanation:
Immutable:
the state or contents of an object cannot be changed or modified.best example for this is String
class .We cannot change the contents of the String class Object contents.
______________________________________________________________________
Question 16
What is the output of the program shown below?
public class Quiz1F
{
private static int i = 0;
public static void main(String[] args)
{
{
int i = 3;
7. }
System.out.println("i is " + i);
}
}
Ans) i is 0
Explanation:
The scope of the local variable is confined only to the method itself in which it has been
declared.
So if we print ‘i’ 0 will be displayed as output.which is the value of the static variable i.as we
declared private static int i = 0;
______________________________________________________________________
Question 17
Which of the following statements is true of the reserved word this?
Ans) It can be only used in instance methods and constructors
Explanation:
This refer to the current class instance variables.We can use ‘this’ to initialize the instance
variables.
______________________________________________________________________
Question 18
Which of the following statements is true about the class shown below?
public class Quiz1G
{
private void method()
{
}
public static void main(String[] args)
{
method();
}
}
Ans) It won't compile because method needs to be static
Explanation:
First the program wont compile.because we didn’t declare that method as static.To solve the
error we have to declare the method as static.
8. privatestaticvoid method()
{
System.out.println("x");
}
______________________________________________________________________
Question 19
Which of the following statements is true of private instance variables?
Ans) They can accessed in any instance method in the same class
Explanation:
The scope of the private is confined to the class itself.We can access the private variables only
with in the class in which they have been declared.We can access private instance variables in
any method of the same class.
______________________________________________________________________
Question 20
Which of the following statements about class (static) variables is correct?
Ans) There is only one copy of each class variable that is shared by all instances of the class
Explanation:
Static variable:Only one copy of static variable is shared by all the instances of the class.if we
modified the value of static variable in one object then it will reflects in other objects of the same
class also.
______________________________________________________________
Solution
Question 1
How many parameters does a default constructor have?
Ans) It never has any
Explanation:
The default constructor doesn’t have any parameters.Thats why it is also called as Zero-
Argumented constructor.
Even The default constructor doesn’t return anything even void also.
___________________________________________________________________
Question 2
Which of the following statements is true about the class shown below?
9. class Quiz1A
{
private int[] array = new int[10];
public int[] getArray()
{
return array;
}
}
Ans) It is immutable
Explanation:
Here the getArray() method is not doing any operations inside it.It just returning an array.So it is
also called as getter method.
______________________________________________________________________
Question 3
A method that is associated with an individual object is called __________.
Ans) an instance method
Explanation:
For every class we can create many objects.If we want to call a method on object, we have to call
a method by using the reference of it.That method is confined to that object only.
______________________________________________________________________
Question 4
Given the declaration Circle[] x = new Circle[10], which of the following statement is most
accurate?
Ans) x contains a reference to an array and each element in the array can hold a reference to a
Circle object.
Explanation:
This array will hold the references of Circle class objects.It will not store objects.
______________________________________________________________________
Question 5
What is the output of the program shown below?
public class Quiz1B
{
public static void main(String[] args)
{
Count count = new Count();
10. int times = 0;
for (int i = 0; i < 100; i++)
increment(count, times);
System.out.println("times = " + times + " count = " + count.times);
}
public static void increment(Count count, int times)
{
count.times++;
times++;
}
}
class Count
{
int times;
public Count()
{
times = 1;
}
}
Ans) times = 100 count = 101
Explanation:
This for loop in the main method will be executed for 100 times.So the value of the variable of
main method times will increased by 1 for each iteration.As the initial value of times is 0 and
finally it will become 100.after 100 iterations
As the initial value of instance variable of Count class is 1.After 100 iterations its value will
become 101
______________________________________________________________________
Question 6
When calling a method with an object argument, ___________ is passed.
Ans) the reference of the object
Explanation:
We have to pass the reference of an object as argument while calling.not the copy or objects
11. contents or else.
______________________________________________________________________
Question 7
Which of the following is not a property of a constructor?
Ans) The name of a constructor can be chosen by the programmer
Explanation:
We can overload the constructor.
The constructor is called and executed when we are creating an object by using new operator.
The name of the constructor should be the name of the class.We cannot choose the name of the
constructor.
______________________________________________________________________
Question 8
What is the output of the program shown below?
public class Quiz1C
{
public static void main(String[] args)
{
int i = 1;
StringBuilder s = new StringBuilder("s");
String t = "t";
someMethod(i, s, t);
System.out.print("i = " + i + ", ");
System.out.print("s = " + s + ", ");
System.out.println("t = " + t);
}
private static void someMethod(int i, StringBuilder s, String t)
{
i++;
s.append("s");
t += "t";
}
}
Ans) i = 2, s = ss, t = tt
12. Explanation:
We are calling the method someMethod(int i, StringBuilder s, String t) by passing the int
variable,references of StringBuilder and String
Inside that i value will be incremented by 1
Appending s to the content of StringBuilder Object.
Concatenating t to the String “t”
______________________________________________________________________
Question 9
Which of the following statements is true about the order of methods in a class.
Ans) They can be listed in any order
Explanation:
We can declare the methods inside the class in any order .there is no restrictions on the order of
declaration of methods.
______________________________________________________________________
Question 10
Which of the following statements about constructors is true?
Ans) A default constructor is provided automatically if no constructors are explicitly declared in
the class
Explanation:
If we didnt provided any constructor in a class,the java compiler will provide the code for default
constructor inside the class while compiling the class.
______________________________________________________________________
Question 11
Which of the following is true about the program shown below?
class Quiz1D
{
private int x;
public Quiz1D(int x)
{
this.x = x;
}
}
Ans) If the this qualifier is removed, it will compile but won't initialize the instance variable
Explanation:
13. This refers to the current class instance variable.Even If we didn’t provide ‘this’ also our
program will compile but the instance variable will not get initialized.So the instance variable
will be initialized with the default value.
______________________________________________________________________
Question 12
Which of the following statements about local variables is true?
Ans) A compilation error results when a local variable is accessed before it is initialized.
Explanation:
Even if we didn’t initialize the during compilation instance variables will be initialized with their
default values.
But if we want to use local variables compulsory we have to initialize it.If not we will get
compilation error.
______________________________________________________________________
Question 13
Analyze the code shown below and indicate which of the following is true:
class Quiz1E
{
private double variable;
public Quiz1E(double variable)
{
variable = variable;
}
}
Ans)
The program will compile, but you cannot create an object of Quiz1E with a specified value for
variable
Explanation:
As we didn’t provide ‘this’ so the variable will not get initialized.
We wont get any compilation error.But the instance variable value of Quiz1E class will be
initialized with the default values by the java compiler.So as it is double data type it will be
initialized with 0.0
______________________________________________________________________
Question 14
Which of the following is true of public methods?
14. Ans) They can only be accessed by methods in a different package
Explanation:
The scope of the public access specifier is global.So we can access public methods from
anywhere like with in the class or from with in the package or from outside the package.
______________________________________________________________________
Question 15
Which of the following statements is true about an immutable object?
Ans) The contents of an immutable object cannot be modified
Explanation:
Immutable:
the state or contents of an object cannot be changed or modified.best example for this is String
class .We cannot change the contents of the String class Object contents.
______________________________________________________________________
Question 16
What is the output of the program shown below?
public class Quiz1F
{
private static int i = 0;
public static void main(String[] args)
{
{
int i = 3;
}
System.out.println("i is " + i);
}
}
Ans) i is 0
Explanation:
The scope of the local variable is confined only to the method itself in which it has been
declared.
So if we print ‘i’ 0 will be displayed as output.which is the value of the static variable i.as we
declared private static int i = 0;
______________________________________________________________________
Question 17
15. Which of the following statements is true of the reserved word this?
Ans) It can be only used in instance methods and constructors
Explanation:
This refer to the current class instance variables.We can use ‘this’ to initialize the instance
variables.
______________________________________________________________________
Question 18
Which of the following statements is true about the class shown below?
public class Quiz1G
{
private void method()
{
}
public static void main(String[] args)
{
method();
}
}
Ans) It won't compile because method needs to be static
Explanation:
First the program wont compile.because we didn’t declare that method as static.To solve the
error we have to declare the method as static.
privatestaticvoid method()
{
System.out.println("x");
}
______________________________________________________________________
Question 19
Which of the following statements is true of private instance variables?
Ans) They can accessed in any instance method in the same class
Explanation:
The scope of the private is confined to the class itself.We can access the private variables only
with in the class in which they have been declared.We can access private instance variables in
any method of the same class.
16. ______________________________________________________________________
Question 20
Which of the following statements about class (static) variables is correct?
Ans) There is only one copy of each class variable that is shared by all instances of the class
Explanation:
Static variable:Only one copy of static variable is shared by all the instances of the class.if we
modified the value of static variable in one object then it will reflects in other objects of the same
class also.
______________________________________________________________