MCQs
Using comma operator , we can include more than one statement in the
initialization and
iteration portion of the for loop. Therefore both ++i
and j = i + 1 is executed i gets the value
– 0,1,2,3,4 & j gets the
values -0,1,2,3,4,5.
output:
$ javac comma_operator.java
$ java comma_operator
6
What is the output of this program?
class access{
public int x;
private int y;
void cal(int a, int b){
x = a + 1;
y = b;
}
void print() {
system.out.println(" " + y);
}
}
class access_specifier {
public static void main(String args[])
{
access obj = new access();
obj.cal(2, 3);
System.out.println(obj.x);
obj.print();
}
}
None.
output:
$ javac access_specifier.java
$ java access_specifier
3 3
All the given statements are legal declarations.
var2 is initialised to 1. The conditional statement returns false and the else part gets executed.
output:
$ javac selection_statements.java
$ java selection_statements
2
default access is the "package oriented" access modifier.
Option A and C are wrong because public and protected are less restrictive.
Option B and D are wrong because abstract and synchronized are not access
modifiers.
No two case constants in the same switch can have identical values.
None.
output:
$ javac access_specifier.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field access.y is not visible
Option A and B are wrong because they use the default access modifier and
the access modifier for the class is public (remember, the default constructor
has the same access modifier as the class).
Option D is wrong. The void makes the compiler think that this is a method
specification - in fact if it were a method specification the compiler would spit
it out.
private members of a class can not be inherited by a sub class.
None.