You are on page 1of 31

What will be the output of the program?

class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); Thread x = new Thread(t); x.start(); /* Line 7 */ } public void run() { for(int i = 0; i < 3; ++i) { System.out.print(i + ".."); } } }

public class Test178 { public static void main(String[] args) { String s = "foo"; Object o = (Object)s; if (s.equals(o)) { System.out.print("AAA"); } else { System.out.print("BBB"); } if (o.equals(s)) { System.out.print("CCC"); } else { System.out.print("DDD"); } } }

public class SqrtExample { public static void main(String [] args) { double value = -9.0; System.out.println( Math.sqrt(value)); } }

String s = "ABC"; s.toLowerCase(); s += "def"; System.out.println(s); public class HorseTest

{ public static void main (String [] args) { class Horse { public String name; /* Line 7 */ public Horse(String s) { name = s; } } /* class Horse ends */ Object obj = new Horse("Zippo"); /* Line 13 */ Horse h = (Horse) obj; /* Line 14 */ System.out.println(h.name); } } /* class HorseTest ends */

public class MyOuter { public static class MyInner { public static void foo() { } } } which statement, if placed in a class other than MyOuter or MyInner, instantiates an instance of the nested class? A. MyOuter.MyInner m = new MyOuter.MyInner();

B.

MyOuter.MyInner mi = new MyInner();

MyOuter m = new MyOuter(); C. MyOuter.MyInner mi = m.new MyOuter.MyInner(); D. MyInner mi = new MyOuter.MyInner();

Which statement is true about a static nested class? A. You must have a reference to an instance of the enclosing class in order to instantiate it.

B.

It does not have access to nonstatic members of the enclosing class.

C.

It's variables and methods must be static.

D.

It must extend the enclosing class.

String a = "newspaper"; a = a.substring(5,7); char b = a.charAt(1); a = a + b; System.out.println(a);

WRONG

What will be the output of the program?

class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); Thread x = new Thread(t); x.start(); /* Line 7 */ } public void run() { for(int i = 0; i < 3; ++i) { System.out.print(i + ".."); } } } Compilation fails. 1..2..3..

A.

B.

C.

0..1..2..3..

D.

0..1..2..

Answer: Option D Explanation: The thread MyThread will start and loop three times (from 0 to 2). Option A is incorrect because the Thread class implements the Runnable interface; therefore, in line 7, Thread can take an object of type Thread as an argument in the constructor. Option B and C are incorrect because the variable i in the for loop starts with a value of 0 and ends with a value of 2.

class MyThread extends Thread { MyThread() {} MyThread(Runnable r) {super(r); } public void run() { System.out.print("Inside Thread "); } } class MyRunnable implements Runnable { public void run() { System.out.print(" Inside Runnable"); } } class Test { public static void main(String[] args) { new MyThread().start(); new MyThread(new MyRunnable()).start(); } } Prints "Inside Thread Inside Thread"

A.

B.

Prints "Inside Thread Inside Runnable"

C.

Does not compile

D.

Throws exception at runtime

Answer: Option A Explanation: If a Runnable object is passed to the Thread constructor, then the run method of the Thread class will invoke the run method of the Runnable object.

In this case, however, the run method in the Thread class is overridden by the run method in MyThread class. Therefore the run() method in MyRunnable is never invoked. Both times, the run() method in MyThread is invoked instead.

public class WaitTest { public static void main(String [] args) { System.out.print("1 "); synchronized(args) { System.out.print("2 "); try { args.wait(); /* Line 11 */ } catch(InterruptedException e){ } } System.out.print("3 "); } } It fails to compile because the IllegalMonitorStateException of wait()is not dealt with in line 11.

A.

B.

123

C.

13

D.

12

Answer: Option D Explanation: 1 and 2 will be printed, but there will be no return from the wait call because no other thread will notify the main thread, so 3 will never be printed. The program is essentially frozen at line 11. A is incorrect; IllegalMonitorStateException is an unchecked exception so it doesn't have to be dealt with explicitly. B and C are incorrect; 3 will never be printed, since this program will never terminate because it will wait forever.

class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); t.start(); System.out.print("one. "); t.start(); System.out.print("two. "); } public void run() { System.out.print("Thread "); } } Compilation fails

A.

B.

An exception occurs at runtime.

C.

It prints "Thread one. Thread two."

D.

The output cannot be determined.

Answer: Option B Explanation: When the start() method is attempted a second time on a single Thread object, the method will throw an IllegalThreadStateException (you will not need to know this exception name for the exam). Even if the thread has finished running, it is still illegal to call start() again.

public class SqrtExample { public static void main(String [] args) { double value = -9.0; System.out.println( Math.sqrt(value)); } } 3.0

A.

B.

-3.0

C.

NaN

D.

Compilation fails.

Answer: Option C Explanation: The sqrt() method returns NaN (not a number) when it's argument is less than zero.

String a = "newspaper"; a = a.substring(5,7); char b = a.charAt(1); a = a + b; System.out.println(a); apa

A.

B.

app

C.

apea

D.

apep

Answer: Option B Explanation: Both substring() and charAt() methods are indexed with a zero-base, andsubstring() returns a String of length arg2 - arg1.

class A { public A(int x){} } class B extends A { } public class test { public static void main (String args []) { A a = new B(); System.out.println("complete"); } } It compiles and runs printing nothing

A.

B.

Compiles but fails at runtime

C.

Compile Error

D.

Prints "complete"

Answer: Option C Explanation: No constructor has been defined for class B therefore it will make a call to the default constructor but since class B extends class A it will also call the Super() default constructor. Since a constructor has been defined in class A java will no longer supply a default constructor for class A therefore when class B calls class A's default constructor it will result in a compile error.

17. What will be the output of the program?


class Super { public int i = 0; public Super(String text) /* Line 4 */ { i = 1; } } class Sub extends Super { public Sub(String text) { i = 2; } public static void main(String args[]) { Sub sub = new Sub("Hello"); System.out.println(sub.i); } } 0 1

A.

B.

C.

D.

Compilation fails.

Answer: Option D Explanation: A default no-args constructor is not created because there is a constructor supplied that has an argument, line 4. Therefore the sub-class constructor must explicitly make a call to the super class constructor:

public Sub(String text) { super(text); // this must be the first line constructor i = 2; }

Learn more problems on : Declarations and Access Control Discuss about this problem : Discuss in Forum

18. What will be the output of the program?


public class HorseTest { public static void main (String [] args) { class Horse { public String name; /* Line 7 */ public Horse(String s) { name = s; } } /* class Horse ends */ Object obj = new Horse("Zippo"); /* Line 13 */ Horse h = (Horse) obj; /* Line 14 */ System.out.println(h.name); } } /* class HorseTest ends */ An exception occurs at runtime at line 10.

A.

B.

It prints "Zippo".

C.

Compilation fails because of an error on line 7.

D.

Compilation fails because of an error on line 13.

Answer: Option B Explanation: The code in the HorseTest class is perfectly legal. Line 13 creates an instance of the methodlocal inner class Horse, using a reference variable declared as type Object. Line 14 casts the Horse object to a Horse reference variable, which allows line 15 to compile. If line 14 were removed, the HorseTest code would not compile, because class Objectdoes not have a name variable.

ALL
1.
Which one is a valid declaration of a boolean? A. boolean b1 = 0;

B.

boolean b2 = 'false';

C.

boolean b3 = false;

D.

boolean b4 = Boolean.false();

E.

boolean b5 = no;

Answer: Option C Explanation: A boolean can only be assigned the literal true or false. Learn more problems on : Language Fundamentals Discuss about this problem : Discuss in Forum

2.

Which three statements are true? 1. Assertion checking is typically enabled when a program is deployed. 2. It is never appropriate to write code to handle failure of an assert statement. 3. Assertion checking is typically enabled during program development and testing. 4. Assertion checking can be selectively enabled or disabled on a per-package basis, but not on a per-class basis. 5. Assertion checking can be selectively enabled or disabled on both a per-package basis and a per-class basis.

A.

1, 2 and 4

B.

2, 3 and 5

C.

3, 4 and 5

D.

1, 2 and 5

Answer: Option B Explanation: (1) is wrong. It's just not true. (2) is correct. You're never supposed to handle an assertion failure. (3) is correct. Assertions let you test your assumptions during development, but the assertion codein effectevaporates when the program is deployed, leaving behind no overhead or debugging code to track down and remove. (4) is wrong. See the explanation for (5) below. (5) is correct. Assertion checking can be selectively enabled or disabled on a per-package basis. Note that the package default assertion status determines the assertion status for classes initialized in the future that belong to the named package or any of its "subpackages". The assertion status can be set for a named top-level class and any nested classes contained therein. This setting takes precedence over the class loader's default assertion status, and over any applicable per-package default. If the named class is not a top-level class, the change of status will have no effect on the actual assertion status of any class. Learn more problems on : Assertions Discuss about this problem : Discuss in Forum

3.

switch(x) { default: System.out.println("Hello"); } Which two are acceptable types for x? 1. 2. 3. 4. byte long char float

5. Short 6. Long

A.

1 and 3

B.

2 and 4

C.

3 and 5

D.

4 and 6

Answer: Option A Explanation:

Switch statements are based on integer expressions and since both bytes and chars can
implicitly be widened to an integer, these can also be used. Also shorts can be used.Short and Long are wrapper classes and reference types can not be used as variables. Learn more problems on : Flow Control Discuss about this problem : Discuss in Forum

4.

Which three statements are true? 1. 2. 3. 4. 5. The default constructor initialises method variables. The default constructor has the same access as its class. The default constructor invokes the no-arg constructor of the superclass. If a class lacks a no-arg constructor, the compiler always creates a default constructor. The compiler creates a default constructor only when there are no other constructors for the class.

A.

1, 2 and 4

B.

2, 3 and 5

C.

3, 4 and 5

D.

1, 2 and 3

Answer: Option B Explanation: (2) sounds correct as in the example below

class CoffeeCup { private int innerCoffee; public CoffeeCup() { }

public void add(int amount) { innerCoffee += amount; } //... } The compiler gives default constructors the same access level as their class. In the example above, class CoffeeCup is public, so the default constructor is public. IfCoffeeCup had been given package access, the default constructor would be given package access as well. (3) is correct. The Java compiler generates at least one instance initialisation method for every class it compiles. In the Java class file, the instance initialisation method is named "<init>." For each constructor in the source code of a class, the Java compiler generates one <init>() method. If the class declares no constructors explicitly, the compiler generates a default no-arg constructor that just invokes the superclass's no-arg constructor. As with any other constructor, the compiler creates an <init>() method in the class file that corresponds to this default constructor. (5) is correct. The compiler creates a default constructor if you do not declare any constructors in your class. Learn more problems on : Declarations and Access Control Discuss about this problem : Discuss in Forum

5.

Which method registers a thread in a thread scheduler? A. run(); B. construct();

C.

start();

D.

register();

Answer: Option C Explanation: Option C is correct. The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. Option A is wrong. The run() method of a thread is like the main() method to an application. Starting the thread causes the object's run method to be called in that separately executing thread. Option B is wrong. There is no construct() method in the Thread class. Option D is wrong. There is no register() method in the Thread class. Learn more problems on : Threads Discuss about this problem : Discuss in Forum

6.

What will be the output of the program?

class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); Thread x = new Thread(t); x.start(); /* Line 7 */ } public void run() { for(int i = 0; i < 3; ++i) { System.out.print(i + ".."); } } } Compilation fails. 1..2..3..

A.

B.

C.

0..1..2..3..

D.

0..1..2..

Answer: Option D Explanation: The thread MyThread will start and loop three times (from 0 to 2). Option A is incorrect because the Thread class implements the Runnable interface; therefore, in line 7, Thread can take an object of type Thread as an argument in the constructor. Option B and C are incorrect because the variable i in the for loop starts with a value of 0 and ends with a value of 2. Learn more problems on : Threads Discuss about this problem : Discuss in Forum

7.

What will be the output of the program?

class MyThread extends Thread { MyThread() {} MyThread(Runnable r) {super(r); }

public void run() { System.out.print("Inside Thread "); } } class MyRunnable implements Runnable { public void run() { System.out.print(" Inside Runnable"); } } class Test { public static void main(String[] args) { new MyThread().start(); new MyThread(new MyRunnable()).start(); } } Prints "Inside Thread Inside Thread"

A.

B.

Prints "Inside Thread Inside Runnable"

C.

Does not compile

D.

Throws exception at runtime

Answer: Option A Explanation: If a Runnable object is passed to the Thread constructor, then the run method of theThread class will invoke the run method of the Runnable object. In this case, however, the run method in the Thread class is overridden by the run method in MyThread class. Therefore the run() method in MyRunnable is never invoked. Both times, the run() method in MyThread is invoked instead. Learn more problems on : Threads Discuss about this problem : Discuss in Forum

8.

What will be the output of the program?

public class WaitTest { public static void main(String [] args) { System.out.print("1 "); synchronized(args) { System.out.print("2 "); try { args.wait(); /* Line 11 */ } catch(InterruptedException e){ } } System.out.print("3 "); } } It fails to compile because the IllegalMonitorStateException of wait()is not dealt with in line 11.

A.

B.

123

C.

13

D.

12

Answer: Option D Explanation: 1 and 2 will be printed, but there will be no return from the wait call because no other thread will notify the main thread, so 3 will never be printed. The program is essentially frozen at line 11. A is incorrect; IllegalMonitorStateException is an unchecked exception so it doesn't have to be dealt with explicitly. B and C are incorrect; 3 will never be printed, since this program will never terminate because it will wait forever. Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

9.

What will be the output of the program?

class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); t.start(); System.out.print("one. "); t.start(); System.out.print("two. "); } public void run() { System.out.print("Thread "); } } Compilation fails

A.

B.

An exception occurs at runtime.

C.

It prints "Thread one. Thread two."

D.

The output cannot be determined.

Answer: Option B Explanation: When the start() method is attempted a second time on a single Thread object, the method will throw an IllegalThreadStateException (you will not need to know this exception name for the exam). Even if the thread has finished running, it is still illegal to call start() again. Learn more problems on : Threads Discuss about this problem : Discuss in Forum

10. What will be the output of the program?


class s1 extends Thread {

public void run() { for(int i = 0; i < 3; i++) { System.out.println("A"); System.out.println("B"); } } } class Test120 extends Thread { public void run() { for(int i = 0; i < 3; i++) { System.out.println("C"); System.out.println("D"); } } public static void main(String args[]) { s1 t1 = new s1(); Test120 t2 = new Test120(); t1.start(); t2.start(); } } Compile time Error There is no start() method

A.

B.

Will print in this order AB CD AB...

C.

Will print but not be able to predict the Order

D.

Will print in this order ABCD...ABCD...

Answer: Option C Explanation: We cannot predict the order in which threads are going to run. Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

11.
import java.awt.*; class Ticker extends Component { public static void main (String [] args) { Ticker t = new Ticker(); /* Missing Statements ? */ } } which two of the following statements, inserted independently, could legally be inserted into missing section of this code? 1. 2. 3. 4. boolean boolean boolean boolean test test test test = = = = (Component instanceof t); (t instanceof Ticker); t.instanceof(Ticker); (t instanceof Component);

A.

1 and 4

B.

2 and 3

C.

1 and 3

D.

2 and 4

Answer: Option D Explanation: (2) is correct because class type Ticker is part of the class hierarchy of t; therefore it is a legal use of the instanceof operator. (4) is also correct because Component is part of the hierarchy of t, because Ticker extends Component. (1) is incorrect because the syntax is wrong. A variable (or null) always appears before the instanceof operator, and a type appears after it. (3) is incorrect because the statement is used as a method (t.instanceof(Ticker);), which is illegal. Learn more problems on : Operators and Assignments Discuss about this problem : Discuss in Forum

12. What will be the output of the program?


public class Test178 { public static void main(String[] args)

{ String s = "foo"; Object o = (Object)s; if (s.equals(o)) { System.out.print("AAA"); } else { System.out.print("BBB"); } if (o.equals(s)) { System.out.print("CCC"); } else { System.out.print("DDD"); } } } AAACCC

A.

B.

AAADDD

C.

BBBCCC

D.

BBBDDD

Answer: Option A Learn more problems on : Java.lang Class Discuss about this problem : Discuss in Forum

13. What will be the output of the program?


public class SqrtExample { public static void main(String [] args) { double value = -9.0; System.out.println( Math.sqrt(value));

} } 3.0

A.

B.

-3.0

C.

NaN

D.

Compilation fails.

Answer: Option C Explanation: The sqrt() method returns NaN (not a number) when it's argument is less than zero. Learn more problems on : Java.lang Class Discuss about this problem : Discuss in Forum

14. What will be the output of the program?


String s = "ABC"; s.toLowerCase(); s += "def"; System.out.println(s); ABC

A.

B.

abc

C.

ABCdef

D.

Compile Error

Answer: Option C

Explanation:

String objects are immutable. The object s above is set to "ABC". Now ask yourself if this object
is changed and if so where - remember strings are immutable. Line 2 returns a string object but does not change the originag string object s, so after line 2 s is still "ABC". So what's happening on line 3? Java will treat line 3 like the following:

s = new StringBuffer().append(s).append("def").toString();
This effectively creates a new String object and stores its reference in the variable s, the old String object containing "ABC" is no longer referenced by a live thread and becomes available for garbage collection. Learn more problems on : Java.lang Class Discuss about this problem : Discuss in Forum

15. What will be the output of the program?


String a = "newspaper"; a = a.substring(5,7); char b = a.charAt(1); a = a + b; System.out.println(a); apa

A.

B.

app

C.

apea

D.

apep

Answer: Option B Explanation: Both substring() and charAt() methods are indexed with a zero-base, andsubstring() returns a String of length arg2 - arg1.

Learn more problems on : Java.lang Class Discuss about this problem : Discuss in Forum

16. What will be the output of the program?


class A { public A(int x){} } class B extends A { } public class test { public static void main (String args []) { A a = new B(); System.out.println("complete"); } } It compiles and runs printing nothing

A.

B.

Compiles but fails at runtime

C.

Compile Error

D.

Prints "complete"

Answer: Option C Explanation: No constructor has been defined for class B therefore it will make a call to the default constructor but since class B extends class A it will also call the Super() default constructor. Since a constructor has been defined in class A java will no longer supply a default constructor for class A therefore when class B calls class A's default constructor it will result in a compile error. Learn more problems on : Java.lang Class Discuss about this problem : Discuss in Forum

17. What will be the output of the program?


class Super { public int i = 0; public Super(String text) /* Line 4 */ { i = 1; } } class Sub extends Super { public Sub(String text) { i = 2; } public static void main(String args[]) { Sub sub = new Sub("Hello"); System.out.println(sub.i); } } 0 1

A.

B.

C.

D.

Compilation fails.

Answer: Option D Explanation: A default no-args constructor is not created because there is a constructor supplied that has an argument, line 4. Therefore the sub-class constructor must explicitly make a call to the super class constructor:

public Sub(String text) { super(text); // this must be the first line constructor i = 2; }

Learn more problems on : Declarations and Access Control Discuss about this problem : Discuss in Forum

18. What will be the output of the program?


public class HorseTest { public static void main (String [] args) { class Horse { public String name; /* Line 7 */ public Horse(String s) { name = s; } } /* class Horse ends */ Object obj = new Horse("Zippo"); /* Line 13 */ Horse h = (Horse) obj; /* Line 14 */ System.out.println(h.name); } } /* class HorseTest ends */ An exception occurs at runtime at line 10.

A.

B.

It prints "Zippo".

C.

Compilation fails because of an error on line 7.

D.

Compilation fails because of an error on line 13.

Answer: Option B Explanation: The code in the HorseTest class is perfectly legal. Line 13 creates an instance of the methodlocal inner class Horse, using a reference variable declared as type Object. Line 14 casts the Horse object to a Horse reference variable, which allows line 15 to compile. If line 14 were removed, the HorseTest code would not compile, because class Objectdoes not have a name variable.

Learn more problems on : Inner Classes Discuss about this problem : Discuss in Forum

19.

public class MyOuter { public static class MyInner { public static void foo() { } } } which statement, if placed in a class other than MyOuter or MyInner, instantiates an instance of the nested class? A. MyOuter.MyInner m = new MyOuter.MyInner();

B.

MyOuter.MyInner mi = new MyInner();

MyOuter m = new MyOuter(); C. MyOuter.MyInner mi = m.new MyOuter.MyInner(); D. MyInner mi = new MyOuter.MyInner();

Answer: Option A Explanation:

MyInner is a static nested class, so it must be instantiated using the fully-scoped name of MyOuter.MyInner.
Option B is incorrect because it doesn't use the enclosing name in the new. Option C is incorrect because it uses incorrect syntax. When you instantiate a nested class by invoking new on an instance of the enclosing class, you do not use the enclosing name. The difference between Option A and C is that Option C is calling new on an instance of the enclosing class rather than just new by itself. Option D is incorrect because it doesn't use the enclosing class name in the variable declaration. Learn more problems on : Inner Classes Discuss about this problem : Discuss in Forum

20. Which statement is true about a static nested class?

A.

You must have a reference to an instance of the enclosing class in order to instantiate it.

B.

It does not have access to nonstatic members of the enclosing class.

C.

It's variables and methods must be static.

D.

It must extend the enclosing class.

Answer: Option B Explanation: Option B is correct because a static nested class is not tied to an instance of the enclosing class, and thus can't access the nonstatic members of the class (just as a static method can't access nonstatic members of a class). Option A is incorrect because static nested classes do not need (and can't use) a reference to an instance of the enclosing class. Option C is incorrect because static nested classes can declare and define nonstatic members. Option D is wrong because it just is. There's no rule that says an inner or nested class has to extend anything.

You might also like