ACADEMIC YEAR 2020-2021 [ODD SEMESTER]
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Faculty Name : Dr.K.Kartheeban Year /Sec: II/ B
Subject Name with Code : Java Programming / CSE18R272 Unit: I, II, III, IV, V
QUIZ
1. Predict the output of following Java Programs
class A
{
public A(String s)
{
System.out.print("A");
}
}
public class B extends A
{
public B(String s)
{
System.out.print("B");
}
public static void main(String[] args)
{
new B("C");
System.out.println(" ");
}
}
a. B
b. C
c. A
d. Error
2. Predict the output of following Java Programs
class array_output
{
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = i;
System.out.print(array_variable[i] + " ");
i++;
}
}
}
a) 0 2 4 6 8
b) 1 3 5 7 9
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
3. Predict the output of following Java Programs
class multidimention_array
{
public static void main(String args[])
{
int arr[][] = new int[3][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
arr[i][j] = j + 1;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
sum + = arr[i][j];
System.out.print(sum);
}
}
a) 11
b) 10
c) 13
d) 14
4. Predict the output of following Java Programs
class evaluate
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System.out.println(arr[n] / 2);
}
}
a) 3
b) 0
c) 6
d) 1
5. Predict the output of following Java Programs
package pkg;
class display
{
int x;
void show()
{
if (x > 1)
System.out.print(x + " ");
}
}
class packages
{
public static void main(String args[])
{
display[] arr=new display[3];
for(int i=0;i<3;i++)
arr[i]=new display();
arr[0].x = 0;
arr[1].x = 1;
arr[2].x = 2;
for (int i = 0; i < 3; ++i)
arr[i].show();
}
}
Note : packages.class file is in directory pkg;
a) 0
b) 1
c) 2
d) 0 1 2
6. Predict the output of following Java Programs
class A
{
public int i;
protected int j;
}
class B extends A
{
int j;
void display()
{
super.j = 3;
System.out.println(i + " " + j);
}
}
class Output
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 1 2
b) 2 1
c) 1 3
d) 3 1
7. Predict the output of following Java Programs
class A
{
int i;
void display()
{
System.out.println(i);
}
}
class B extends A
{
int j;
void display()
{
System.out.println(j);
}
}
class method_overriding
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 0
b) 1
c) 2
d) Compilation Error
8. Predict the output of following Java Programs
class A
{
public int i;
public int j;
A()
{
i = 1;
j = 2;
}
}
class B extends A
{
int a;
B()
{
super();
}
}
class super_use
{
public static void main(String args[])
{
B obj = new B();
System.out.println(obj.i + " " + obj.j)
}
}
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error
9. Predict the output of following Java Programs
class newthread extends Thread
{
Thread t;
newthread()
{
t1 = new Thread(this,"Thread_1");
t2 = new Thread(this,"Thread_2");
t1.start();
t2.start();
}
public void run()
{
t2.setPriority(Thread.MAX_PRIORITY);
System.out.print(t1.equals(t2));
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}
a) true
b) false
c) truetrue
d) falsefalse
10. Predict the output of following Java Programs
class newthread extends Thread
{
Thread t;
String name;
newthread(String threadname)
{
name = threadname;
t = new Thread(this,name);
t.start();
}
public void run()
{
}
}
class multithreaded_programing
{
public static void main(String args[])
{
newthread obj1 = new newthread("one");
newthread obj2 = new newthread("two");
try
{
System.out.print(obj1.t.equals(obj2.t));
}
catch(Exception e)
{
System.out.print("Main thread interrupted");
}
}
}
a) true
b) false
c) Main thread interrupted
d) None of the mentioned
11. Predict the output of following Java Programs
class newthread extends Thread
{
Thread t;
String name;
newthread(String threadname)
{
name = threadname;
t = new Thread(this,name);
t.start();
}
public void run()
{
}
}
class multithreaded_programing
{
public static void main(String args[])
{
newthread obj1 = new newthread("one");
newthread obj2 = new newthread("two");
try
{
Thread.sleep(1000);
System.out.print(obj1.t.isAlive());
}
catch(InterruptedException e)
{
System.out.print("Main thread interrupted");
}
}
}
a) true
b) false
c) Main thread interrupted
d) None of the mentioned
12. Predict the output of following Java Programs
class newthread extends Thread
{
Thread t;
String name;
newthread(String threadname)
{
name = threadname;
t = new Thread(this,name);
t.start();
}
public void run()
{
}
}
class multithreaded_programing
{
public static void main(String args[])
{
newthread obj1 = new newthread("one");
newthread obj2 = new newthread("two");
try
{
obj1.t.wait();
System.out.print(obj1.t.isAlive());
}
catch(Exception e)
{
System.out.print("Main thread interrupted");
}
}
}
a) true
b) false
c) Main thread interrupted
d) None of the mentioned
13. Predict the output of following Java Programs
class output
{
public static void main(String args[])
{
char c[]={'a','1','b',' ','A','0'];
for (int i = 0; i < 5; ++i)
{
if(Character.isDigit(c[i]))
System.out.println(c[i]" is a digit");
if(Character.isWhitespace(c[i]))
System.out.println(c[i]" is a Whitespace character");
if(Character.isUpperCase(c[i]))
System.out.println(c[i]" is an Upper case Letter");
if(Character.isUpperCase(c[i]))
System.out.println(c[i]" is a lower case Letter");
i = i + 3;
}
}
}
a) a is a lower case Letter
is White space character
b) b is a lower case Letter
is White space characte
c) a is a lower case Letter
A is a upper case Letter
d) a is a lower case Letter
0 is a digit
14. Predict the output of following Java Programs
class Input_Output
{
public static void main(String args[]) throws IOException
{
char c;
BufferedReader obj = new BufferedReader(new
InputStreamReader(System.in));
do
{
c = (char) obj.read();
System.out.print(c);
} while(c!='\'');
}
}
a) abc’
b) abcdef/’
c) abc’def/’egh
d) abcqfghq
15. Predict the output of following Java Programs
class Input_Output
{
public static void main(String args[]) throws IOException
{
char c;
BufferedReader obj = new BufferedReader(new
InputStreamReader(System.in));
do
{
c = (char) obj.read();
System.out.print(c);
} while(c != 'q');
}
}
a) abcqfgh
b) abc
c) abcq
d) abcqfghq
16. Predict the output of following Java Programs
import java.io.*;
class Chararrayinput
{
public static void main(String[] args)
{
String obj = "abcdef";
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, 0, 3);
int i;
try
{
while ((i = input1.read()) != -1)
{
System.out.print((char)i);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
a) abc
b) abcd
c) abcde
d) abcdef
17. Predict the output of following Java Programs
import java.io.*;
class Chararrayinput
{
public static void main(String[] args)
{
String obj = "abcdef";
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, 0, 3);
int i;
try
{
while ((i = input2.read()) != -1)
{
System.out.print((char)i);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
a) abc
b) abcd
c) abcde
d) abcdef
18. Predict the output of following Java Programs
import java.io.EOFException;
import java.io.IOException;
public class Test
{
public static void main(String[] args)
{
try
{
System.out.printf("1");
int value = 10 / 0;
throw new IOException();
}
catch(EOFException e)
{
System.out.printf("2");
}
catch(ArithmeticException e)
{
System.out.printf("3");
}
catch(NullPointerException e)
{
System.out.printf("4");
}
catch(IOException e)
{
System.out.printf("5");
}
catch(Exception e)
{
System.out.printf("6");
}
}
}
a) 1346
b) 136726
c) 136
d) 13
19. Predict the output of following Java Programs
public class Test
{
public static void main(String[] args)
{
try
{
System.out.printf("1");
int data = 5 / 0;
}
catch(ArithmeticException e)
{
Throwable obj = new Throwable("Sample");
try
{
throw obj;
}
catch (Throwable e1)
{
System.out.printf("8");
}
}
finally
{
System.out.printf("3");
}
System.out.printf("4");
}
}
a) Compilation error
b) Runtime error
c) 1834
d) 134
20. Predict the output of following Java Programs
public class Test
{
public static void main(String[] args)
{
try
{
System.out.printf("1");
int data = 5 / 0;
}
catch(ArithmeticException e)
{
System.out.printf("2");
System.exit(0);
}
finally
{
System.out.printf("3");
}
System.out.printf("4");
}
}
a) 12
b) 1234
c) 124
d) 123
21. Predict the output of following Java Programs
public class Test
{
private void m1()
{
m2();
System.out.printf("1");
}
private void m2()
{
m3();
System.out.printf("2");
}
private void m3()
{
System.out.printf("3");
try
{
int sum = 4/0;
System.out.printf("4");
}
catch(ArithmeticException e)
{
System.out.printf("5");
}
System.out.printf("7");
}
public static void main(String[] args)
{
Test obj = new Test();
obj.m1();
}
}
a) 35721
b) 354721
c) 3521
d) 35
22. Predict the output of following Java Programs
public class Test
{
public static void main(String[] args)
{
try
{
System.out.printf("1");
int sum = 9 / 0;
System.out.printf("2");
}
catch(ArithmeticException e)
{
System.out.printf("3");
}
catch(Exception e)
{
System.out.printf("4");
}
finally
{
System.out.printf("5");
}
}
}
a) 1325
b) 1345
c) 1342
d) 135
23. Predict the output of following Java Programs
import java.util.*;
public class genericstack <E>
{
Stack <E> stk = new Stack <E>();
public void push(E obj)
{
stk.push(obj);
}
public E pop()
{
E obj = stk.pop();
return obj;
}
}
class Output
{
public static void main(String args[])
{
genericstack <String> gs = new genericstack<String>();
gs.push("Dr.K.K");
System.out.println(gs.pop());
}
}
a) K
b) Dr.K.K
c) Runtime Error
d) Compilation Error
24. Predict the output of following Java Programs
abstract class demo
{
public int a;
demo()
{
a = 10;
}
abstract public void set();
abstract final public void get();
}
class Test extends demo
{
public void set(int a)
{
this.a = a;
}
final public void get()
{
System.out.println("a = " + a);
}
public static void main(String[] args)
{
Test obj = new Test();
obj.set(20);
obj.get();
}
}
(A) a = 10
(B) a = 20
(C) Compilation error
(D) a = 40
25. Predict the output of following Java Programs
class Base
{
public final void show()
{
System.out.println("Base::show() called");
}
}
class Derived extends Base
{
public void show()
{
System.out.println("Derived::show() called");
}
}
public class Main
{
public static void main(String[] args)
{
Base b = new Derived();
b.show();
}
}
(A) Derived - show() called
(B) Base - show() called
(C) Compiler Error
(D) Exception
26. Predict the output of following Java Programs
class Main
{
public static void main(String args[])
{
final int i;
i = 20;
i = 30;
System.out.println(i);
}
}
(A) 30
(B) Compiler Error
(C) Garbage value
(D) 0
27. Predict the output of following Java Programs
interface MyInterface
{
void display();
}
interface MySubInterface extends MyInterface
{
void display();
}
public class Test implements MySubInterface
{
public void display()
{
System.out.print("Dr.K.K Welcome to java Programming.");
}
public static void main(String args[])
{
Test t = new Test();
t.display();
}
}
A. The code will lead to a compilation error as declaration of the display method has
been provided in two interface.
B. The code will lead to a compilation error due to public modifier while declaring the
display method.
C. The code will compile and execute successfully showing the output Dr.K.K
Welcome to java Programming.
D. The code will lead to a compilation error as the display method is not declared as
abstract.
E. None of these
28. Predict the output of following Java Programs
class equality
{
int x;
int y;
boolean isequal()
{
return(x == y);
}
}
class Output
{
public static void main(String args[])
{
equality obj = new equality();
obj.x = 5;
obj.y = 5;
System.out.println(obj.isequal);
}
}
a) false
b) true
c) 0
d) 1
29. Predict the output of following Java Programs
class box
{
int width;
int height;
int length;
int volume;
void volume()
{
volume = width * height * length;
}
void volume(int x)
{
volume = x;
} }
class Output
{
public static void main(String args[])
{
box obj = new box();
obj.height = 1;
obj.length = 5;
obj.width = 5;
obj.volume(5);
System.out.println(obj.volume);
}
}
a) 0
b) 5
c) 25
d) 26
30. Predict the output of following Java Programs
class area
{
int width;
int length;
int height;
area()
{
width = 5;
length = 6;
height = 1;
}
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);
}
}
a) 0
b) 1
c) 25
d) 30