0% found this document useful (0 votes)
105 views

Java Pool - 75 Questions

java from beginning to the end with all syntax to how to write code

Uploaded by

Nimisha Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

Java Pool - 75 Questions

java from beginning to the end with all syntax to how to write code

Uploaded by

Nimisha Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

1. What is the output of the following code snippet?

class Bill{
int itemPrice;
public Bill (int itemPrice) { this.itemPrice = itemPrice;}
void display() {
int itemPrice = 20;
System.out.println(itemPrice);
}
}
class Demo {
public static void main(String[] args) {
Bill billobj = new Bill (10);
System.out.println (billobj.itemPrice);
billobj.display();
}
}

A. 10
0

B. 10
20

C. 10
10

D. Error in the class as there is no default constructor defined

Ans:B

2. What will be the output of the following java program?

class Parent
{
int x=10;
}
class Child extends Parent
{
int x=20;
}
class Test
{
public static void main(String[] args)
{
Parent p=new Parent();
System.out.print(p.x+” ”);
Child c=new Child();
System.out.print(c.x+” ”);
Parent p1=new Child();
System.out.print(p1.x);
}
}

a)10 20 20
b)10 20 10
c)10 10 10
d)20 20 20

Ans: b

3. Consider the code given below.

Identify the code that needs to be filled in Line 1, 2, and 3 respectively such that:
• The student id is auto-generated starting from 501 in steps of 1
• The method 'getNoOfStudent' returns the total number of students enrolled at any given point.

class Student{
private int studentId;
private String studentName;
private int yearofEnrollment;
public static int counter;
static {
//Line 1
}
public Student (String name, int yearOfEnrollment) {
this.studentName=name;
this.yearOfEnrollment=yearofEnrollment;
// Line 2
}
public static int getNoOfStudent () {
// Line 3
}
}
A. Line 1: Student.counter=501;

Line 2: this.studentid=Student.counter++;

Line 3: return (Student.counter-500);

B. Line 1: Student.counter=501;

Line 2: this.studentid=++Student.counter;

Line 3: return (Student.counter-501);

C. Line 1: Student.counter=500;

Line 2: this.studentId=Student.counter++;

Line 3: return (Student.counter-500);

D. Line 1: Student.counter=500;

Line 2: this.studentid=++Student.counter;

Line 3: return (Student.counter-500);

Ans:D

4. Consider the code snippet given below:

class Customer {
public int custId;
public String custName;
}
public class Tester {
public static void main(String args[]) {
Customer obj = new Customer();
Customer objone = new Customer();
Customer objTwo;
Customer objThree = obj;
}
}
How many object and reference variables of class Customer will be created?

A. 3 objects and 1 reference variable

B. 2 objects and 4 reference variables

C. 4 objects and 4 reference variables

D. 2 objects and 3 reference variables

Ans:c

5. What is the output of the following code?

class Base {
private int fun() {
return 0;
}
public int run() {
return 3;
}
}
class Derived extends Base {
private int fun() {
return 1;
}
public int run() {
return fun();
}
}
class Derivedl extends Derived {
public int fun() {
return 2;
}
}
class Tester {
public static void main(String[] args) {
Base baseRef = new Derivedl();
System.out.println (baseRef.run());
}
}
A. 1
B. 2
C. 0
D. 3
Ans: A

6. What will be the output of the following Java program?

final class A
{
int i;
}

class B extends A
{
int j;
System.out.println(j + " " + i);
}

class inheritance
{
public static void main(String args[])
{
B obj = new B();
obj.display();
}
}

a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error

Ans: d

7. What will be the output of the following Java program?

class overload
{
int x;
double y;
void add(int a , int b)
{
x = a + b;
}
void add(double c , double d)
{
y = c + d;
}
overload()
{
this.x = 0;
this.y = 0;
}

class Overload_methods

public static void main(String args[])

overload obj = new overload();

int a = 2;

double b = 3.2;

obj.add(a, a);

obj.add(b, b);

System.out.println(obj.x + " " + obj.y);

a) 4 6.4
b) 6.4 6
c) 6.4 6.4
d) 6 6

Ans: a

8. What will be the output of the following Java program?


class A

int i;

class B extends A

int j;

void display()

super.i = j + 1;

System.out.println(j + " " + i);

class inheritance

public static void main(String args[])

B obj = new B();

obj.i=1;

obj.j=2;

obj.display();

}
a) 2 2
b) 3 3
c) 2 3
d) 3 2

Ans: c

9. What will be the output of the following Java program?

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

Ans: a

10. What will be the output of the following Java code?

class A

public int i;

private int j;

class B extends A

void display()

super.j = super.i + 1;
System.out.println(super.i + " " + super.j);

class inheritance

public static void main(String args[])

B obj = new B();

obj.i=1;

obj.j=2;

obj.display();

a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error

Ans:d

11. What will be the output of the following Java code?

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
Ans:a

12. What will be the output of the following Java code?

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

Ans:c

13. What will be the output of the following Java code?

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

Ans:a

14. What is the output of the below Java program with an Interface?

interface Worm
{
int teeth=2;
}
class BookWorm implements Worm
{
int teeth=4;
void show()
{
teeth= 5;
System.out.println("Teeth: " + teeth);
}
}
public class InterfaceTest4
{
public static void main(String[] args)
{
new BookWorm().show();
}
}

A) Teeth: 4
B) Teeth: 5
C) Compiler error as teeth is a constant in Worm interface.
D) None of the above

Ans:b

15. Which is the missing java code in the class implementing an Interface below?

interface Linein
{ void addInput(); }

interface Lineout
{ void addOutput(); }

class Speaker implements Linein, Lineout


{
//MISSING CODE
}

A)

class Speaker implements Linein, Lineout


{
@Override
public void addOutput() { }

@Override
public void addInput() { }
}

B)

class Speaker implements Linein, Lineout


{
@Override
public void addOutput() { }
}
C)

class Speaker implements Linein, Lineout


{
@Override
public void addInput() { }
}

D) All the above

Ans:a

16. Which is the missing code to successfully compile the below Java program with abstract
classes and Interfaces?

interface A
{ void a(); }

abstract class B implements A


{ abstract void b(); }

class C extends B
{
//Missing methods
}

A)

@Override
public void a() { }

@Override
void b() {}

B)

@Override
public void a() { }

C)

@Override
void b() {}
D) All the above

Ans:a

17. What is the output of this program?

interface calculate
{
void cal(int item);
}
class display implements calculate
{
int x;
public void cal(int item)
{
x = item * item;
}
}
class Main
{
public static void main(String args[])
{
display arr = new display();
arr.x = 0;
arr.cal(2);
System.out.print(arr.x);
}
}

A. 0
B. 2
C. 4
D. None of the mentioned

Ans:c

18. What is the output of the following program?

public class Test


{
public Test()
{
System.out.printf("1");
new Test(10);
System.out.printf("5");
}
public Test(int temp)
{
System.out.printf("2");
new Test(10, 20);
System.out.printf("4");
}
public Test(int data, int temp)
{
System.out.printf("3");

public static void main(String[] args)


{
Test obj = new Test();

}
a) 12345
b) Compilation error '
c) 15
d) Runtime error

Ans:a

19. What is the output of the following program?

class Base
{
public static String s = " Super Class ";
public Base()
{
System.out.printf("1");
}
}
public class Derived extends Base
{
public Derived()
{
System.out.printf("2");
super();
}
public static void main(String[] args)
{
Derived obj = new Derived();
System.out.printf(s);
}
}
a) 21 Super Class
b) Super Class 21
c) Compilation error
d) 12 Super Class

Ans. c

20. What is the output of the following program?

import java.io.IOException;
import java.util.EmptyStackException;

public class newclass


{
public static void main(String[] args)
{
try
{
System.out.printf("%d", 1);
throw(new Exception());
}
catch(IOException e)
{
System.out.printf("%d", 2);
}
catch(EmptyStackException e)
{
System.out.printf("%d", 3);
}
catch(Exception e)
{
System.out.printf("%d", 4);
}
finally
{
System.out.printf("%d", 5);
}
}
}

a) 12345
b) 15
c) 135
d) 145

Ans: d

21. Which of the following is FALSE about abstract classes in Java.


A If we derive an abstract class and do not implement all the abstract methods, then the
derived class should also be marked as abstract using 'abstract' keyword
B Abstract classes can have constructors
C A class can be made abstract without any abstract method
D A class can inherit from multiple abstract classes.
Answer : A

22. Which of the following is true about interfaces in java.


1) An interface can contain following type of members.
....public, static, final fields (i.e., constants)
....default and static methods with bodies
2) An instance of interface can be created.
3) A class can implement multiple interfaces.
4) Many classes can implement the same interface.
A 1, 3 and 4
B 1, 2 and 4
C 2, 3 and 4
D 1, 2, 3 and 4
Answer: A

23. Predict the output of the following program.


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
Answer : C

24. Which of the following statement(s) with regard to an abstract class in JAVA is/are TRUE ?
I. An abstract class is one that is not used to create objects.
II. II. An abstract class is designed only to act as a base class to be inherited by other
classes.
A Only I
B Only II
C Neither I nor II
D Both I and II
Answer: D

25. Which of the following is used to make an Abstract class?


A Making atleast one member function as pure virtual function
B Making atleast one member function as virtual function
C Declaring as Abstract class using virtual keyword
D Declaring as Abstract class using static keyword
Answer: A

26. What’s wrong with the following code?


interface Vehicle {
void start();
void run();
void stop();
}
class Car implements Vehicle {
public void start() {
}
}
A Compilation error
B Runtime error
C An exception is thrown
D Source code is correct

Answer: A

27. What is the output of the following code?


interface A
{
int var = 2;
}
class B implements A
{
void show()
{
var = 3;
System.out.println("var = "+var);
}
}
public class Main {
public static void main(String[] args) {
B obj = new B();
obj.show();
}
}
A2
B3
C5
D Compilation error
Answer: D

28. What is the output of the following code?


interface A
{
void show();
}
class B implements A
{
public void show()
{
System.out.println("Welcome To StackHowTo!");
}
}
public class Main {
public static void main(String[] args) {
A obj = new B();
obj.show();
}
}
A We cannot create the object ‘obj’ from the interface ‘A’.
B Welcome To StackHowTo!
C Compilation error
D All the answers are false.
Answer: B

29. What is the output of the following code?


interface A
{
void show();
}
class B implements A
{
public void show()
{
System.out.println("Welcome To StackHowTo!");
}
}
public class Main {
public static void main(String[] args) {
A obj = new B();
obj.show();
}
}
A We cannot create the object ‘obj’ from the interface ‘A’.
B Welcome To StackHowTo!
C Compilation error
D All the answers are false.

Answer: B

30. What is the output of this program?


class MyClass
{
int width;
int height;
int length;
}
public class MainClass
{
public static void main(String args[])
{
MyClass objA = new MyClass();
MyClass objB = new MyClass();
objA.height = 1;
objA.length = 2;
objA.width = 1;
objB = objA;
System.out.println(objB.height);
}
}
A Runtime error
B Compilation error
C1
D2
Answer: C

31. Output of following Java Program?


class Base {
public 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
Answer: A

32.
class Base {
final public void show() {
System.out.println("Base::show() called");
}
}

class Derived extends Base {


public void show() {
System.out.println("Derived::show() called");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
A. Base::show() called
B. Derived::show() called
C. Compiler Error
D. Runtime Error
Answer: C

33.
class Base {
public static void show() {
System.out.println("Base::show() called");
}
}
class Derived extends Base {
public static void show() {
System.out.println("Derived::show() called");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();
b.show();
}
}
A. Base::show() called
B. Derived::show() called
C. Compiler Error
Answer: A

34.
Which of the following is true about inheritance in Java?
1) Private methods are final.
2) Protected members are accessible within a package and
inherited classes outside the package.
3) Protected methods are final.
4) We cannot override private methods.
A 1, 2 and 4
B Only 1 and 2
C 1, 2 and 3
D 2, 3 and 4
Answer: A

35.
Output of following Java program?
class Base {
public void Print() {
System.out.println("Base");
}
}

class Derived extends Base {


public void Print() {
System.out.println("Derived");
}
}

class Main{
public static void DoPrint( Base o ) {
o.Print();
}
public static void main(String[] args) {
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}
A. Base
Derived
Derived

B. Base
Base
Derived

C. Base
Derived
Base

D. Compiler Error
Answer: A

36.
Predict the output of following program.
class Base {
public void foo() { System.out.println("Base"); }
}

class Derived extends Base {


private void foo() { System.out.println("Derived"); }
}

public class Main {


public static void main(String args[]) {
Base b = new Derived();
b.foo();
}
}
A. Base
B. Derived
C. Compiler Error
D. Runtime Error
Answer: C

37.
Which of the following is true about inheritance in Java.
1) In Java all classes inherit from the Object class directly or indirectly. The Object class is root
of all classes.
2) Multiple inheritance is not allowed in Java.
3) Unlike C++, there is nothing like type of inheritance in Java where we can specify whether the
inheritance is protected, public or private.
A. 1, 2 and 3
B. 1 and 2
C. 2 and 3
D. 1 and 3
Answer: A

38.
Predict the output of following Java Program
// filename Main.java
class Grandparent {
public void Print() {
System.out.println("Grandparent's Print()");
}
}
class Parent extends Grandparent {
public void Print() {
System.out.println("Parent's Print()");
}
}

class Child extends Parent {


public void Print() {
super.super.Print();
System.out.println("Child's Print()");
}
}

public class Main {


public static void main(String[] args) {
Child c = new Child();
c.Print();
}
}
A. Compiler Error in super.super.Print()
B. Grandparent's Print()
Parent's Print()
Child's Print()
C. Runtime Error
Answer: A

39.
final class Complex {
private final double re;
private final double im;

public Complex(double re, double im) {


this.re = re;
this.im = im;
}

public String toString() {


return "(" + re + " + " + im + "i)";
}
}

class Main {
public static void main(String args[])
{
Complex c = new Complex(10, 15);
System.out.println("Complex number is " + c);
}
}
A. Complex number is (10.0 + 15.0i)
B. Compiler Error
C. Complex number is SOME_GARBAGE
D. Complex number is Complex@8e2fb5
Here 8e2fb5 is hash code of c
Answer: A

40. What should be the execution order, if a class has a method, static block, instance block, and
constructor, as shown below?

public class First_C {


public void myMethod()
{
System.out.println("Method");
}

{
System.out.println(" Instance Block");
}

public void First_C()


{
System.out.println("Constructor ");
}
static {
System.out.println("static block");
}
public static void main(String[] args) {
First_C c = new First_C();
c.First_C();
c.myMethod();
}
}
a. Instance block, method, static block, and constructor
b. Method, constructor, instance block, and static block
c. Static block, method, instance block, and constructor
d. Static block, instance block, constructor, and method

Answer: D

41. What will be the output of the following program?


public class MyFirst {
public static void main(String[] args) {
MyFirst obj = new MyFirst(n);
}
static int a = 10;
static int n;
int b = 5;
int c;
public MyFirst(int m) {
System.out.println(a + ", " + b + ", " + c + ", " + n + ", " + m);
}
// Instance Block
{
b = 30;
n = 20;
}
// Static Block
static
{
a = 60;
}
}
a. 10, 5, 0, 20, 0
b. 10, 30, 20
c. 60, 5, 0, 20
d. 60, 30, 0, 20, 0
Answer: D

42. What will be the output of the following program?

public class Test2 {


public static void main(String[] args) {
StringBuffer s1 = new StringBuffer("Complete");
s1.setCharAt(1,'i');
s1.setCharAt(7,'d');
System.out.println(s1);
}
}
a. Complete
b. Iomplede
c. Cimpletd
d. Coipletd
Answer: C

43. Given that Student is a class, how many reference variables and objects are created by the
following code?
Student studentName, studentId;
studentName = new Student();
Student stud_class = new Student();
a. Three reference variables and two objects are created.
b. Two reference variables and two objects are created.
c. One reference variable and two objects are created.
d. Three reference variables and three objects are created.
Answer: A

44. What will be the output of the following program?

abstract class MyFirstClass


{
abstract num (int a, int b) { }
}

What is the result of the following program?

public static synchronized void main(String[] args) throws


InterruptedException {
Thread f = new Thread();
f.start();
System.out.print("A");
f.wait(1000);
System.out.print("B");
}
a. It prints A and B with a 1000 seconds delay between them
b. It only prints A and exits
c. It only prints B and exits
d. A will be printed, and then an exception is thrown.
Answer: D

45. What is the result of the following program?


public static synchronized void main(String[] args) throws
InterruptedException {
Thread f = new Thread();
f.start();
System.out.print("A");
f.wait(1000);
System.out.print("B");
}
a. It prints A and B with a 1000 seconds delay between them
b. It only prints A and exits
c. It only prints B and exits
d. A will be printed, and then an exception is thrown.
Answer: D

46. What is the output of the following Java code?


class Person
{
private int age;

private Person()
{
age = 24;
}
}
public class Test
{
public static void main(String[] args)
{
Person p = new Person();
System.out.println(p.age);
}
}
A 24
B Compilation error
C Runtime error
D None of the above
Answer: B

47. What is the output of the following program?


public class Test implements Runnable
{
public void run()
{
System.out.printf(" Thread's running ");
}

try
{
public Test()
{
Thread.sleep(5000);
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}

public static void main(String[] args)


{
Test obj = new Test();
Thread thread = new Thread(obj);
thread.start();
System.out.printf("MT-1");
}
}

a) MT-1 Thread’s running b) Thread’s running MT c) Compilation error d) Runtime error

Ans. (c)

48. What is the output of the following program?


class Temp
{
private Temp(int data)
{
System.out.printf(" Constructor called ");
}
protected static Temp create(int data)
{
Temp obj = new Temp(data);
return obj;
}
public void myMethod()
{
System.out.printf(" Method called ");
}
}

public class Test


{
public static void main(String[] args)
{
Temp obj = Temp.create(20);
obj.myMethod();
}
}
a) Constructor called Method called b) Compilation error c) Runtime error d) None of the above

Ans. (a)
49. What is the output of the following program?
class Derived
{
public void getDetails(String temp)
{
System.out.println("Derived class " + temp);
}
}

public class Test extends Derived


{
public int getDetails(String temp)
{
System.out.println("Test class " + temp);
return 0;
}
public static void main(String[] args)
{
Test obj = new Test();
obj.getDetails("PSIT");
}
}
a) Derived class PSIT
b) Test class PSIT
c) Compilation error
d) Runtime error

Ans. (c)

50. 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) Compilation fails
b) B
c) C
d) None

Ans. (a)

51. Predict the output:

public class Juggler extends Thread


{
public static void main(String[] args)
{
try
{
Thread t = new Thread(new Juggler());
Thread t2 = new Thread(new Juggler());
}
catch (Exception e)
{
System.out.print("e ");
}
}
public void run()
{
for (int i = 0; i < 2; i++)
{
try
{
Thread.sleep(500);
}
catch (Exception e)
{
System.out.print("e2 ");
}
System.out.print(Thread.currentThread().getName()+ " ");
}
}
}
a) e
b) e2
c) No Output
d) None

Ans. (c)

52. Predict the output


class Grandparent
{
public void Print()
{
System.out.println("Grandparent's Print()");
}
}

class Parent extends Grandparent


{
public void Print()
{
System.out.println("Parent's Print()");
}
}

class Child extends Parent


{
public void Print()
{
super.super.Print();
System.out.println("Child's Print()");
}
}

public class Main


{
public static void main(String[] args)
{
Child c = new Child();
c.Print();
}
}

a) Compiler Error in super.super.Print()


b) Parent's Print()
c) Child's Print()
d) None
Ans. (a)
53. Given:
13. public class Pass {
14. public static void main(String [] args) {
15. int x 5;
16. Pass p = new Pass();
17. p.doStuff(x);
18. System.out.print(” main x = “+ x);
19. }
20.
21. void doStuff(int x) {
22. System.out.print(” doStuff x = “+ x++);
23. }
24. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. doStuffx = 6 main x = 6
D. doStuffx = 5 main x = 5

Answer: D

54. Given:
1. interface TestA { String toString(); }
2. public class Test {
3. public static void main(String[] args) {
4. System.out.println(new TestA() {
5. public String toString() { return “test”; }
6. });
7. }
8. }
What is the result?
A. test
B. null
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 1.

Answer: A

55. Given:
55. int []x= {1, 2,3,4, 5};
56. int y[] =x;
57. System.out.println(y[2]); Which is true?

A. Line 57 will print the value 2.


B. Line 57 will print the value 3.
C. Compilation will fail because of an error in line 55.
D. Compilation will fail because of an error in line 56.

Answer: B

56. Given:
10.int x=0;
11.int y 10;
12. do {
13. y--;
14. ++x;
15. } while (x < 5);
16. System.out.print(x + “,“ + y); What is the result?
A. 5,6
B. 5,5
C. 6,5
D. null

Answer: B

57. Given:
35. int x= 10;
36. do {
37. x--;
38. } while(x< 10);
How many times will line 37 be executed?
A. ten times
B. zero times
C. one to me times
D. more than ten times

Answer: D

58. Given:
11. public static void main(String[] args) {
12. try {
13. args=null;
14. args[0] = “test”;
15. System.out.println(args[0]);
16. } catch (Exception ex) {
17. System.out.println(”Exception”);
18. } catch (NullPointerException npe) {
19. System.out.println(”NullPointerException”);
20. }
21. }
What is the result?
A. test
B. Exception
C. Compilation fails.
D. NullPointerException

Answer: C

59. In Java arrays are


A. objects
B. object references
C. primitive data type
D. None of the above

Answer: A

60. What will be the output?


public class Test{
public static void main(String[] args){
int[] x = new int[3];
System.out.println("x[0] is " + x[0]);
}
}
A. The program has a compile error because the size of the array wasn't specified when declaring
the array.
B. The program has a runtime error because the array elements are not initialized.
C. The program runs fine and displays x[0] is 0.
D. The program has a runtime error because the array element x[0] is not defined.

Answer: C
61. Which of the modifier can't be used for constructors?
A. public
B. private
C. static
D. protected

Answer: C

62. Determine output of the following program code?


public class Test{
public static void main(String args[]){
int i;
try{
i = calculate();
System.out.println(i);
}catch(Exception e){
System.out.println("Error occured");
}
}
static int calculate(){
return (7/2);
}
}
A. 3
B. 3.5
C. Error occured
D. Compilation Error

Answer: A

63. What will be the output?


interface A{
public void method();
}
class One{
public void method(){
System.out.println("Class One method");
}
}
class Two extends One implements A{
public void method(){
System.out.println("Class Two method");
}
}
public class Test extends Two{
public static void main(String[] args){
A a = new Two();
a.method();
}
}
A. will print Class One method
B. will print Class Two method
C. compiles fine but print nothing
D. Compilation Error

Answer: B

64. Runnable is a _____ .


A. class
B. abstract class
C. interface
D. vaiable

Answer: C

65. What will be the output of the following program code?


class LogicalCompare{
public static void main(String args[]){
String str1 = new String("OKAY");
String str2 = new String(str1);
System.out.println(str1 == str2);
}
}
A. true
B. false
C. 0
D. 1

Answer: B

66. What will be the output?


1. public class Test{
2. public static void main(String args[]){
3. Object myObj = new String[]{"one", "two", "three"};
4. {
5. for(String s : (String[])myObj)
6. System.out.print(s + ".");
7. }
8. }
9. }
A. one.two.three.
B. Compilation fails because of an error at line 3
C. Compilation fails because of an error at line 5
D. An exception is thrown at runtime.

Answer: A

67. When a class extends the Thread class ,it should override ............ method of Thread class to
start that thread.
A. start()
B. run()
C. init()
D. go()

Answer: B

68. What will be the output?


class A extends Thread{
public void run(){
for(int i=0; i<2; i++){
System.out.println(i);
}
}
}
public class Test{
public static void main(String argv[]){
Test t = new Test();
t.check(new A(){});
}
public void check(A a){
a.start();
}
}
A. 0 0
B. Compilation error, class A has no start method
C. 0 1
D. Compilation succeed but runtime exception

Answer: C

69. Given:
11. public static void main(String[] args) {
12. Object obj =new int[] { 1,2,3 };
13. int[] someArray = (int[])obj;
14. for (int i: someArray) System.out.print(i +“ “)
15. }
‘What is the result?

A. 1 2 3
B. Compilation fails because of an error in line 12.
C. Compilation fails because of an error in line 13.
D. Compilation fails because of an error in line 14.

Answer: A

Answer: C

70. A programmer has an algorithm that requires a java.util.List that provides an efficient
implementation of add(0,object), but does NOT need to support quick random access. What
supports these requirements?
A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList

Answer: D

71. Which is true?


A. A finalizer may NOT be invoked explicitly.
B. The finalize method declared in class Object takes no action.
C. super.finalize() is called implicitly by any overriding finalize method.
D. The order in which finalize will be called on two objects is based on the order in which the
two objects became finalizable.
Answer: B

72. Which statement is true?

A. It is possible for more than two threads to deadlock at once.


B. The JVM implementation guarantees that multiple threads cannot enter into a deadlocked
state.
C. Deadlocked threads release once their sleep() method's sleep duration has expired.
D. Deadlocking can occur only when the wait(), notify(), and notifyAll() methods are used
incorrectly.

Answer: A

73. What will be the output?


interface A{
public void method();
}
class One{
public void method(){
System.out.println("Class One method");
}
}
class Two extends One implements A{
public void method(){
System.out.println("Class Two method");
}
}
public class Test extends Two{
public static void main(String[] args){
A a = new Two();
a.method();
}
}
A. will print Class One method
B. will print Class Two method
C. compiles fine but print nothing
D. Compilation Error
Answer: Option B

74. What will be the output?


1. public interface InfA{
2. protected String getName();
3. }
public class Test implements InfA{
public String getName(){
return "test-name";
}
public static void main (String[] args){
Test t = new Test();
System.out.println(t.getName());
}
}
A. test-name
B. Compilation fails due to an error on lines 2
C. Compilation fails due to an error on lines 1
D. Compilation succeed but Runtime Exception
Answer: Option B

75. Given:
12. public class Yippee2 {
13.
14. static public void main(String [] yahoo) {
15. for(int x= 1; x<yahoo.length; x++) {
16. System.out.print(yahoo[x] + “ “);
17. }
18. }
19. }
and the command line invocation:
java Yippee2 a b c
What is the result?
A.a b
B.b c
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: B

You might also like