Questions
What will be the output of the following program?
class Exc0 extends Exception { }
class Exc1 extends Exc0 { } /* Line 2 */
public class Test
{
public static void main(String args[])
{
try
{
throw new Exc1(); /* Line 9 */
}
catch (Exc0 e0) /* Line 11 */
{
System.out.println(Ex0 caught);
}
catch (Exception e)
{
System.out.println(exception caught);
}
}
}
- Ex0 caught
- exception caught
- Compilation fails because of an error it line 2
- Compilation fails because of an error in line 9
Which two can be used to create a new Thread?
- Extend java.lang.Thread and override the run() method.
- Extend java.lang.Runnable and override the start() method.
- Implement java.lang.Thread and implement the run() method.
- Implement java.lang.Runnable and implement the run() method.
- Implement java.lang.Thread and implement the start() method.
- 1 and 2
- 2 and 3
- 1 and 4
- 3 and 4
The following block of code creates a Thread using a Runnable target:
Runnable target = new MyRunnable();
Thread myThread = new Thread(target);
Which of the following classes can be used to create the target, so that the preceding code compiles correctly?
- public class MyRunnable extends Runnable{public void run(){}}
- public class MyRunnable extends Object{public void run(){}}
- public class MyRunnable implements Runnable{public void run(){}}
- public class MyRunnable implements Runnable{void run(){}}
What will be the output of the program?
public class ThreadDemo
{
private int count = 1;
public synchronized void doSomething()
{
for (int i = 0; i < 10; i++)
System.out.println(count++);
}
public static void main(String[] args)
{
ThreadDemo demo = new ThreadDemo();
Thread a1 = new A(demo);
Thread a2 = new A(demo);
a1.start();
a2.start();
}
}
class A extends Thread
{
ThreadDemo demo;
public A(ThreadDemo td)
{
demo = td;
}
public void run()
{
demo.doSomething();
}
}
- It will print the numbers 0 to 19 sequentially.
- It will print the numbers 1 to 20 sequentially.
- It will print the numbers 1 to 20, but the order cannot be determined.
- The code will not compile.
What will be the output of the program?
int i = l, j = -1;
switch (i)
{
case 0, 1: j = 1; /* Line 4 */
case 2: j = 2;
default: j = 0;
}
System.out.println(j = + j);
- j = -1
- j = 0
- j = 1
- compilation fails
What will be the output of the program?
class Tree { }
class Pine extends Tree { }
class Oak extends Tree { }
public class Forest1
{
public static void main (String [] args)
{
Tree tree = new Pine();
if( tree instanceof Pine )
System.out.println ("Pine");
else if( tree instanceof Tree )
System.out.println ("Tree");
else if( tree instanceof Oak )
System.out.println ( "Oak" );
else
System.out.println ("Oops ");
}
}
- Pine
- Tree
- Forest
- Oops
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) && (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
- 5 2
- 5 3
- 6 3
- 6 4
What will be the output of the program?
String x = "xyz";
x.toUpperCase(); /* Line 2 */
String y = x.replace('Y', 'y');
y = y + "abc";
System.out.println(y);
- abcXyZ
- abcxyz
- xyzabc
- XyZabc
Which statement is true?
- Assertions can be enabled or disabled on a class-by-class basis.
- Conditional compilation is used to allow tested classes to run at full speed.
- Assertions are appropriate for checking the validity of arguments in a method.
- The programmer can choose to execute a return statement or to throw an exception if an assertion fails.
Which cause a compiler error?
- int[ ] scores = {3, 5, 7};
- int [ ][ ] scores = {2,7,6}, {9,3,45};
- String cats[ ] = {"Fluffy", "Spot", "Zeus"};
- boolean results[ ] = new boolean [] {true, false, true};
- Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)};
What two statements are true about the result obtained from calling Math.random()?
- The result is less than 0.0.
- The result is greater than or equal to 0.0..
- The result is less than 1.0.
- The result is greater than 1.0.
- The result is greater than or equal to 1.0.
- 1 and 2
- 2 and 3
- 3 and 4
- 4 and 5
Which three are valid method signatures in an interface?
- private int getArea();
- public float getVol(float x);
- public void main(String [ ] args);
- public static void main(String [ ] args);
- boolean setFlag(Boolean [ ] test);
- 1 and 2
- 2, 3 and 5
- 3, 4, and 5
- 2 and 4
Which interface provides the capability to store objects using a key-value pair?
- Java.util.Map
- Java.util.Set
- Java.util.List
- Java.util.Collection
public class Test2
{
public static int x;
public static int foo(int y)
{
return y * 2;
}
public static void main(String [] args)
{
int z = 5;
assert z > 0; /* Line 11 /
assert z > 2: foo(z); / Line 12 /
if ( z < 7 )
assert z > 4; / Line 14 */
switch (z)
{
case 4: System.out.println("4 ");
case 5: System.out.println("5 ");
default: assert z < 10;
}
if ( z < 10 )
assert z > 4: z++; /* Line 22 */
System.out.println(z);
}
}
which line is an example of an inappropriate use of assertions?
- Line 11
- Line 12
- Line 14
- Line 22
What will be the output of the program?
class Super
{
public Integer getLength()
{
return new Integer(4);
}
}
public class Sub extends Super
{
public Long getLength()
{
return new Long(5);
}
public static void main(String[] args)
{
Super sooper = new Super();
Sub sub = new Sub();
System.out.println(
sooper.getLength().toString() + "," + sub.getLength().toString() );
}
}
- 4, 4
- 4, 5
- 5, 4
- Compilation fails.
What will be the output of the program?
boolean bool = true;
if(bool = false) /* Line 2 /
{
System.out.println("a");
}
else if(bool) / Line 6 /
{
System.out.println("b");
}
else if(!bool) / Line 10 /
{
System.out.println("c"); / Line 12 */
}
else
{
System.out.println("d");
}
- a
- b
- c
- d
What will be the output of the program?
for(int i = 0; i < 3; i++)
{
switch(i)
{
case 0: break;
case 1: System.out.print(one );
case 2: System.out.print(two );
case 3: System.out.print(three );
}
}
System.out.println(done);
- done
- one two done
- one two three done
- one two three two three done
Which of the following statements about the hashcode() method are incorrect?
- The value returned by hashcode() is used in some collection classes to help locate objects.
- The hashcode() method is required to return a positive int value.
- The hashcode() method in the String class is the one inherited from Object.
- Two new empty String objects will produce identical hashcodes.
- 1 and 2
- 2 and 3
- 3 and 4
- 1 and 4
Which class or interface defines the wait(), notify(),and notifyAll() methods?
- Object
- Thread
- Runnable
- Class
Which of the following will directly stop the execution of a thread?
- wait()
- notify()
- notifyall()
- exits synchronized code
What will be the output of the program?
public class TestObj
{
public static void main (String [] args)
{
Object o = new Object() /* Line 5 /
{
public boolean equals(Object obj)
{
return true;
}
} / Line 11 */
System.out.println(o.equals("Fred"));
}
}
- It prints "true".
- It prints "Fred".
- An exception occurs at runtime.
- Compilation fails
public class MyProgram
{
public static void throwit()
{
throw new RuntimeException();
}
public static void main(String args[])
{
try
{
System.out.println("Hello world ");
throwit();
System.out.println("Done with try block ");
}
finally
{
System.out.println("Finally executing ");
}
}
}
which answer most closely indicates the behavior of the program?
- The program will not compile.
- The program will print Hello world, then will print that a RuntimeExceptionhas occurred, then will print Done with try block, and then will print Finally executing.
- The program will print Hello world, then will print that a RuntimeExceptionhas occurred, and then will print Finally executing.
- The program will print Hello world, then will print Finally executing, then will print that a RuntimeException has occurred.
What will be the output of the program?
int i = (int) Math.random();
- i = 0
- i = 1
- value of i is undetermined
- Statement causes a compile error
public class Myfile
{
public static void main (String[] args)
{
String biz = args[1];
String baz = args[2];
String rip = args[3];
System.out.println("Arg is " + rip);
}
}
Select how you would start the program to cause it to print: Arg is 2
- java Myfile 222
- java Myfile 1 2 2 3 4
- java Myfile 1 3 2 2
- java Myfile 0 1 2 3
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
- Compiles but fails at runtime
- Compile Error
- Prints "complete"