Introduction To Methods And Streams(Computer Science > Java Program ) Questions and Answers
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();
}
}
Explanation:-
Answer: Option B. -> 2r 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
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();
}
}
Explanation:-
Answer: Option A. -> A BCall 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
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);
}
}
Explanation:-
Answer: Option D. -> 30 None.
output:
$ javac cons_method.java
$ java cons_method
30
Explanation:-
Answer: Option D. -> errorVariable height is not defined.
output:
$ javac cons_method.java
$ java cons_method
error: cannot find symbol height
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);
}
}
}
Explanation:-
Answer: Option D. -> AaBaaCaaaNone.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
AaBaaCaaa
Explanation:-
Answer: Option B. -> 20 10class 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
Explanation:-
Answer: Option D. -> falseisisNaN() 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
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();
}
}
}
Explanation:-
Answer: Option D. -> None of the mentionedNo 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