which answer most closely indicates the behavior of the program?
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 ");
}
}
}
Answer
What will be the output of the 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");
}
}
}
Answer
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");
}
Answer
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");
Answer
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);
Answer
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);
}
}
Answer
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() );
}
}
Answer
Which three are valid method signatures in an interface?
Which cause a compiler error?
AnswerWhat two statements are true about the result obtained from calling Math.random()?
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 ");
}
}
Answer
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);
Answer
Which statement is true?
Answerwhich line is an example of an inappropriate use of assertions?
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);
}
}
Answer
What will be the output of the program (when you run with the -ea option) ?
public class Test
{
public static void main(String[] args)
{
int x = 0;
assert (x > 0) : "assertion failed"; /* Line 6 */
System.out.println("finished");
}
}
Answer
Which statement is true?
AnswerWhich statement is true?
What will be the output of the program?
class Test116
{
static final StringBuffer sb1 = new StringBuffer();
static final StringBuffer sb2 = new StringBuffer();
public static void main(String args[])
{
new Thread()
{
public void run()
{
synchronized(sb1)
{
sb1.append("A");
sb2.append("B");
}
}
}.start();
new Thread()
{
public void run()
{
synchronized(sb1)
{
sb1.append("C");
sb2.append("D");
}
}
}.start(); /* Line 28 */
System.out.println (sb1 + " " + sb2);
}
}
Answer
What will be the output of the program?
public class SyncTest
{
public static void main (String [] args)
{
Thread t = new Thread()
{
Foo f = new Foo();
public void run()
{
f.increase(20);
}
};
t.start();
}
}
class Foo
{
private int data = 23;
public void increase(int amt)
{
int x = data;
data = x + amt;
}
}
and assuming that data must be protected from corruption, what—if anything—can you add to the preceding code to ensure the integrity of data?
AnswerWhich of the following will not directly cause a thread to stop?
Answer© 2017 QuizBucket.org