Sail E0 Webinar

MCQs

Total Questions : 51 | Page 3 of 6 pages
Question 21.
class A
{
protected int method1(int a, int b)
{
return 0;
}
}


Which is valid in a class that extends class A?


  1.    public int method1(int a, int b) {return 0; }
  2.    private int method1(int a, int b) { return 0; }
  3.    public SHORT method1(int a, int b) { return 0; }
  4.    static protected int method1(int a, int b) { return 0; }
 Discuss Question
Answer: Option A. -> public int method1(int a, int b) {return 0; }

Option A is correct - because the class that extends A is just simply overriding

 method1.

Option B is wrong - because it can't override as there are less access privileges

in the subclass method1.

Option C is wrong - because to override it, the return type needs to be an integer.

 The different return type means that the method is not overriding but the same

 argument list means that the method is not overloading. Conflict - compile time

 error.

Option D is wrong - because you can't override a method and make it a class

method i.e. using static.


Question 22.


What is the widest valid returnType for methodA in line 3?


public class ReturnIt
{
returnType methodA(byte x, double y) /* Line 3 */
{
return (long)x / y * 2;
}
}
  1.    int
  2.    byte
  3.    long
  4.    double
 Discuss Question
Answer: Option D. -> double

However A, B and C are all wrong. Each of these would result in a narrowing

 conversion. Whereas we want a widening conversion, therefore the only correct

 answer is D. Don't be put off by the long cast, this applies only to the variable x

 and not the rest of the expression. It is the variable y (of type double) that forces

 the widening conversion to double.

Java's widening conversions are:

- From a byte to a SHORT, an int, a long, a float, or a double.

- From a short, an int, a long, a float, or a double.

- From a char to an int, a long, a float, or a double.

- From an int to a long, a float, or a double.

- From a long to a float, or a double.

- From a float to a double.



Question 23.


What is the output of this program?


class Output {
public static void main(String args[])
{
int a = 5;
int b = 10;
first: {
second: {
third: {
if (a == b >> 1)
break second;
}
System.out.println(a);
}
System.out.println(b);
}
}
}
  1.    5 10
  2.    10 5
  3.    5
  4.    10
 Discuss Question
Answer: Option D. -> 10

b >> 1 in if returns 5 which is equal to a i:e 5, therefore body
of if is executed and block second

is exited. Control goes to end of
the block second executing the last print statement, printing 10.
output:
$ javac Output.java
$ java Output
10


Question 24.


What is the output of this program?


class test {
int a;
int b;
test(int i, int j) {
a = i;
b = j;
}
void meth(test o) {
o.a *= 2;
O.b /= 2;
}
}
class Output {
public static void main(String args[])
{
test obj = new test(10 , 20);
obj.meth(obj);
System.out.println(obj.a + " " + obj.b); }
}
  1.    10 20
  2.    20 10
  3.    20 40
  4.    40 20
 Discuss Question
Answer: Option B. -> 20 10

 class objects are always passed by reference, therefore changes done are reflected back 

on original arguments. obj.meth(obj) sends object obj as parameter whose variables a & b 

are multiplied and divided by 2 respectively by meth() function of class test. a & b becomes 

20 & 10 respectively.
output:
$ javac Output.java
$ java Output
20 10


Question 25.


What is the output of this program?


class Output {
public static void main(String args[])
{
int x, y = 1;
x = 10;
if (x != 10 && x / 0 == 0)
System.out.println(y);
else
System.out.println(++y);
}
}
  1.    1
  2.    2
  3.    Runtime error owing to division by zero in if condition.
  4.    Unpredictable behavior of program.
 Discuss Question
Answer: Option B. -> 2

Operator short circuit and, &&, skips evaluating right hand
operand if left hand operand is false thus
division by zero in if
condition does not give an error.
output:
$ javac Output.java
$ java Output
2


Question 26.

Which of these access specifier must be used for class so that it can be inherited by 

another sub class?


  1.    public
  2.    private
  3.    protected
  4.    None of the mentioned
 Discuss Question
Answer: Option A. -> public

None.


Question 27.


What is the output of this program?


class jump_statments {
public static void main(String args[])
{
int x = 2;
int y = 0;
for ( ; y < 10; ++y) {
if (y % x == 0)
continue;
else if (y == 8)
break;
else
System.out.print(y + " ");
}
}
}
  1.    1 3 5 7
  2.    2 4 6 8
  3.    1 3 5 7 9
  4.    1 2 3 4 5 6 7 8 9
 Discuss Question
Answer: Option C. -> 1 3 5 7 9

Whenever y is divisible by x remainder body of loop is skipped by
continue statement, therefore

if condition y == 8 is never true as when y
is 8, remainder body of loop is skipped by continue

statements of first
if. Control comes to print statement only in cases when y is odd.
output:
$ javac jump_statments.java
$ java jump_statments
1 3 5 7 9


Question 28.


What is the output of this program?


class static_out {
static int x;
static int y;
void add(int a, int b){
x = a + b;
y = x + b;
}
}
class static_use {
public static void main(String args[])
{
static_out obj1 = new static_out();
static_out obj2 = new static_out();
int a = 2;
obj1.add(a, a + 1);
obj2.add(5, a);
System.out.println(obj1.x + " " + obj2.y);
}
}
  1.    7 7
  2.    6 6
  3.    7 9
  4.    9 7
 Discuss Question
Answer: Option C. -> 7 9

None.
output:
$ javac static_use.java
$ java static_use
6 6.4


Question 29.

Which three are valid method signatures in an interface?

     1. private int getArea();

     2. public float getVol(float x);

     3. public void main(String [] args);

     4. public static void main(String [] args);

     5. boolean setFlag(Boolean [] test);


  1.    1 and 2
  2.    2, 3 and 5
  3.    3, 4, and 5
  4.    2 and 4
 Discuss Question
Answer: Option B. -> 2, 3 and 5

(2), (3), and (5). These are all valid interface method signatures.

(1), is incorrect because an interface method must be public; if it is not explicitly

 declared public it will be made public implicitly. (4) is incorrect because interface

 methods cannot be static.


Question 30.

You want a class to have access to members of another class in the same package.

 Which is the most restrictive access that accomplishes this objective?


  1.    public
  2.    private
  3.    protected
  4.    default access
 Discuss Question
Answer: Option D. -> default access

The only two real contenders are C and D. Protected access Option C makes a

 member accessible only to classes in the same package or subclass of the class.

 While default access Option D makes a member accessible only to classes in the

 same package.

Latest Videos

Latest Test Papers