Sail E0 Webinar

MCQs

Total Questions : 51 | Page 4 of 6 pages
Question 31.


What is the output of this program?


class comma_operator {
public static void main(String args[])
{
int sum = 0;
for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
sum += i;
System.out.println(sum);
}
}
  1.    5
  2.    6
  3.    14
  4.    compilation error
 Discuss Question
Answer: Option B. -> 6

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


Question 32.


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();
}
}
  1.    2 3
  2.    3 3
  3.    Runtime Error
  4.    Compilation Error
 Discuss Question
Answer: Option B. -> 3 3

None.
output:
$ javac access_specifier.java
$ java access_specifier
3 3


Question 33.

Which of the following is/are legal method declarations?

     1. protected abstract void m1();

     2. static final void m1(){}

     3. synchronized public final void m1() {}

     4. private native void m1();



  1.    1 and 3
  2.    2 and 4
  3.    1 only
  4.    All of them are legal declarations
 Discuss Question
Answer: Option D. -> All of them are legal declarations

All the given statements are legal declarations.

Question 34.


What is the output of this program?


class selection_statements {
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
System.out.print(var2);
else
System.out.print(++var2);
}
}
  1.    1
  2.    2
  3.    3
  4.    4
 Discuss Question
Answer: Option B. -> 2

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


Question 35.

What is the most restrictive access modifier that will allow members of one class to 

have access to members of another class in the same package?


  1.    public
  2.    abstract
  3.    protected
  4.    synchronized
  5.    default access
 Discuss Question
Answer: Option E. -> default access

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.


Question 36.

Which of these statement is incorrect?


  1.    switch statement is more efficient than a set of nested ifs.
  2.    two case constants in the same switch can have identical values.
  3.    switch statement can only test for equality, whereas if statement can evaluate any type of boolean expression.
  4.    it is possible to create a nested switch statements.
 Discuss Question
Answer: Option B. -> two case constants in the same switch can have identical values.

No two case constants in the same switch can have identical values.


Question 37.


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;
}
}
class access_specifier {
public static void main(String args[])
{
access obj = new access();
obj.cal(2, 3);
System.out.println(obj.x + " " + obj.y);
}
}
  1.    3 3
  2.    2 3
  3.    Runtime Error
  4.    Compilation Error
 Discuss Question
Answer: Option C. -> Runtime Error

None.
output:
$ javac access_specifier.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field access.y is not visible


Question 38.
public class Test { }


What is the prototype of the default constructor?


  1.    Test( )
  2.    Test(void)
  3.    public Test( )
  4.    public Test(void)
 Discuss Question
Answer: Option C. -> public Test( )

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.

Question 39.

Which of the following statements are incorrect?


  1.    public members of class can be accessed by any code in the program.
  2.    private members of class can only be accessed by other members of the class.
  3.    private members of class can be inherited by a sub class, and become protected members in sub class.
  4.    protected members of a class can be inherited by a sub class, and become private members of the sub class.
 Discuss Question
Answer: Option C. -> private members of class can be inherited by a sub class, and become protected members in sub class.

private members of a class can not be inherited by a sub class.


Question 40.

Which of these jump statements can skip processing remainder of code in its body

for a particular iteration?


  1.    break
  2.    return
  3.    exit
  4.    continue
 Discuss Question
Answer: Option D. -> continue

None.


Latest Videos

Latest Test Papers