Sail E0 Webinar

MCQs

Total Questions : 84 | Page 1 of 9 pages
Question 1.


What is the output of this program?


class A {
int i;
public void display() {
System.out.println(i);
}
}
class B extends A {
int j;
public void display() {
System.out.println(j);
}
}
class Dynamic_dispatch {
public static void main(String args[])
{
B obj2 = new B();
obj2.i = 1;
obj2.j = 2;
A r;
r = obj2;
r.display();
}
}
  1.    1
  2.    2
  3.    3
  4.    4
 Discuss Question
Answer: Option B. -> 2

r is reference of type A, the program assigns a reference of object obj2 to r and uses that reference 

to call function display() of class B.
output:
$ javac Dynamic_dispatch.java
$ java Dynamic_dispatch
2


Question 2.


What is the output of this program?


class binary {
public static void main(String args[]) {
int num = 17;
System.out.print(Integer.toBinaryString(num));
}
}
  1.    1001
  2.    10011
  3.    11011
  4.    10001
 Discuss Question
Answer: Option D. -> 10001

None.
output:
$ javac binary.java
$ java binary
10001


Question 3.


What is the output of this program?


class A {
public void display() {
System.out.println("A");
}
}
class B extends A {
public void display() {
System.out.println("B");
}
}
class Dynamic_dispatch {
public static void main(String args[])
{
A obj1 = new A();
B obj2 = new B();
A r;
r = obj1;
r.display();
r = obj2;
r.display();
}
}
  1.    A B
  2.    B A
  3.    Runtime Error
  4.    Compilation Error
 Discuss Question
Answer: Option A. -> A B

Call to display function of class A and class B is made by using dynamic method dispatch, by 

using this method a call to an overridden function is resolved at run time, rather than at compilation time.
output:
$ javac Dynamic_dispatch.java
$ java Dynamic_dispatch
A B


Question 4.


What is the output of this program?


class area {
int width;
int length;
int volume;
area() {
width = 5;
length = 6;
}
void volume() {
volume = width * height * length;
}
}
class cons_method {
public static void main(String args[]) {
area obj = new area();
obj.volume();
System.out.println(obj.volume);
}
}
  1.    0
  2.    1
  3.    25
  4.    30
 Discuss Question
Answer: Option D. -> 30

 None.
output:
$ javac cons_method.java
$ java cons_method
30


Question 5.


What is the output of this program?


class area {
int width;
int length;
int volume;
area() {
width=5;
length=6;
}
void volume() {
volume = width*length*height;
}
}
class cons_method {
public static void main(String args[])
{
area obj = new area();
obj.volume();
System.out.println(obj.volume);
}
}
  1.    0
  2.    1
  3.    30
  4.    error
 Discuss Question
Answer: Option D. -> error

Variable height is not defined.
output:
$ javac cons_method.java
$ java cons_method
error: cannot find symbol height


Question 6.


What is the output of this program?


import java.io.*;
public class filesinputoutput {
public static void main(String[] args) {
String obj = "abc";
byte b[] = obj.getBytes();
ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
for (int i = 0; i < 2; ++ i) {
int c;
while ((c = obj1.read()) != -1) {
if (i == 0) {
System.out.print(Character.toUpperCase((char)c));
obj2.write(1);
}
}
System.out.print(obj2);
}
}
}
  1.    AaBaCa
  2.    ABCaaa
  3.    AaaBaaCaa
  4.    AaBaaCaaa
 Discuss Question
Answer: Option D. -> AaBaaCaaa

None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
AaBaaCaaa


Question 7.


What is the output of this program?


class output {
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello");
s1.deleteCharAt(1);
System.out.println(s1);
}
}
  1.    Hell
  2.    ello
  3.    Hllo
  4.    Helo
 Discuss Question
Answer: Option C. -> Hllo

None.
Output:
$ javac output.java
$ java output
Hllo


Question 8.


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 9.


What is the output of this program?


class isNaN_output {
public static void main(String args[]) {
Double d = new Double(1 / 0.);
boolean x = d.isNaN();
System.out.print(x);
}
}
  1.    0
  2.    1
  3.    true
  4.    false
 Discuss Question
Answer: Option D. -> false

isisNaN() method returns true is the value being tested is a number. 1/0. is infinitely large in 

magnitude, which cant not be defined as a number hence false is stored in x.
Output:
$ javac isNaN_output.java
$ java isNaN_output
false


Question 10.


What is the output of this program?


import java.io.*;
class Chararrayinput {
public static void main(String[] args) {
String obj = "abcdefgh";
int length = obj.length();
char c[] = new char[length];
obj.getChars(0, length, c, 0);
CharArrayReader input1 = new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c, 1, 4);
int i;
int j;
try {
while ((i = input1.read()) == (j = input2.read())) {
System.out.print((char)i);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
  1.    abc
  2.    abcd
  3.    abcde
  4.    None of the mentioned
 Discuss Question
Answer: Option D. -> None of the mentioned

No output is printed. CharArrayReader object input1 contains string "abcdefgh" whereas object 

input2 contains string "bcde", when while((i=input1.read())==(j=input2.read())) is executed the 

starting character of each object is compared since they are unequal control comes out of loop 

and nothing is printed on the screen.

Output:
$ javac Chararrayinput.java
$ java Chararrayinput


Latest Videos

Latest Test Papers