Beruflich Dokumente
Kultur Dokumente
What will happen when you attempt to compile and run the following code? (Assume that the code is
compiled and run with assertions enabled.)
public class AssertTest
{
public void methodA(int i)
{
assert i >= 0 : methodB();
System.out.println(i);
}
public void methodB()
{
System.out.println("The value must not be negative");
}
public static void main(String args[])
{
AssertTest test = new AssertTest();
test.methodA(-10);
}
}
It will print -10
It will result in Assertion Error showing the message -"The value must not be negative".
The code will not compile.
None of these
Question 2:-What will happen when you attempt to compile and run the following code?
public class Static
{
static
{
int x = 5;
}
static int x,y;
public static void main(String args[])
{
x--; myMethod();
System.out.println(x + y + ++x);
}
Question 3
Given the following code, what will be the output?
class Value
{
public int i = 15;
}
public class Test
{
public static void main(String argv[])
{
Test t = new Test();
t.first();
}
public void first()
{
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i);
}
public void second(Value v, int i)
{
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + " " + i);
}
}
15 0 20 0
15 0 15
20 20
Question 4
What will happen when you attempt to compile and run the following code?
class MyParent
{
int x, y;
MyParent(int x, int y)
{
this.x = x;
this.y = y;
}
public int addMe(int x, int y)
{
return this.x + x + y + this.y;
}
public int addMe(MyParent myPar)
{
return addMe(myPar.x, myPar.y);
}
}
class MyChild extends MyParent
{
in z;
MyChild (int x, int y, int z)
{
super(x,y);
this.z = z;
}
public int addMe(int x, int y, int z)
{
return this.x + x + this.y + y + this.z + z;
}
public int addMe(MyChild myChi)
{
return addMe(myChi.x, myChi.y, myChi.z);
}
public int addMe(int x, int y)
{
return this.x + x + this.y + y;
}
}
public class MySomeOne
{
public static void main(String args[])
{
MyChild myChi = new MyChild(10, 20, 30);
MyParent myPar = new MyParent(10, 20);
int x = myChi.addMe(10, 20, 30);
int y = myChi.addMe(myChi);
int z = myPar.addMe(myPar);
System.out.println(x + y + z);
}
}
300 240
120 180
Compilation Error None of the above
Question 5
The class AssertionError has "is - a" relationship with these classes:
RuntimeException
Error
VirtualMachineError
IllegalAccessException
Throwable
Question 6
What will be the result of executing the following code?
1. boolean a = true;
2. boolean b = false;
3. boolean c = true;
4. if (a == true)
5. if (b == true)
6. if (c == true) System.out.println("Some things are true in this world");
7. else System.out.println("Nothing is true in this world!");
8. else if (a && (b = c)) System.out.println("It's too confusing to tell what is true and what is false");
9. else System.out.println("Hey this won't compile");
The code won't compile
"Some things are true in this world" will be printed
"Hey this won't compile" will be printed
None of these
Question 7
What will happen when you attempt to compile and run the following code?
interface MyInterface
{
}
public class MyInstanceTest implements MyInterface
{
static String s;
public static void main(String args[])
{
MyInstanceTest t = new MyInstanceTest();
if(t instanceof MyInterface)
{
System.out.println("I am true interface");
}
else
{
System.out.println("I am false interface");
}
if(s instanceof String)
{
System.out.println("I am true String");
}
else
{
System.out.println("I am false String");
}
}
}
Compile time error
Runtime error
Prints : "I am true interface" followed by " I am true String"
Prints : "I am false interface" followed by " I am false String"
Prints : "I am true interface" followed by " I am false String"
Prints : "I am false interface" followed by " I am true String"
Question 8
What results from attempting to compile and run the following code?
int a = 5;
Prints: Value is - 9
Prints: Value is - 5
Compilation error
None of these
Question 9
In the following pieces of code, A and D will compile without any error. True/False?
C: byte b = 255;
D: int x = 0x1234;
E: float fl = 1.2;
Question 10
Considering the following code, which variables may be referenced correctly at line 12?
public class Outer
public int a = 1;
private int b = 2;
int d = 3;
class Inner
ab
cd
e
Question 11
What will be the result of executing the following code?
switch (digit)
case 'x' :
int j = 0;
System.out.println(j);
default :
int j = 100;
System.out.println(j);
}
int i = j;
System.out.println(i);
100 will be printed 11 times. 100 will be printed 10 times and then there will be a runtime exception.
The code will not compile because the variable i cannot be declared twice within the main() method.
The code will not compile because the variable j cannot be declared twice within the switch statement.
None of these.
Question 12
Which of the following collection classes from java.util package are Thread safe?
Vector ArrayList
HashMap Hashtable
Question 13
What will happen when you attempt to compile and run the following code?
System.out.println("MyThread: run()");
}
public void start()
System.out.println("MyThread: start()");
System.out.println("MyRunnable: run()");
System.out.println("MyRunnable: start()");
myThread.start();
thread.start();
Question 14
// Filename; SuperclassX.java
package packageX;
int superclassVarX;
// Filename SubclassY.java
1. package packageX.packageY;
2.
4. {
7. void subclassMethodY()
8. {
9. objY.superclassMethodX();
10. int i;
11. i = objY.superclassVarX;
12. }
13. }
----------------------------------------------------------------------
1. DrivingUtilities du;
2. FourWheeler fw;
4. du = (DrivingUtilities)myTruck;
5. fw = new Crane();
6. fw = du;
Line 4 will not compile because an interface cannot refer to an object. The code will compile and run.
The code will not compile without an explicit cast at line 6, because going down the hierarchy without
casting is not allowed. The code at line 4 will compile even without the explicit cast.
The code will compile if we put an explicit cast at line 6 but will throw an exception at runtime.
Question 16
1. class MyClass
2. {
6. {
8. char ch = 'c';
9. obj.myMethod(ch);
10. }
11. }
What is the result when you compile and run the following code?
System.out.println("Inside throwMethod.");
try
throwMethod();
catch (IllegalAccessException e)
}
}
class X
Y b = new Y();
X()
System.out.print("X");
class Y
Y()
System.out.print("Y");
}
}
Y y = new Y();
Z()
System.out.print("Z");
new Z();
Z
YZ
XYZ
YXYZ
Question 19
What will happen when you attempt to compile and run the following code snippet?
else
Question 20
What is the result when you compile and run the following code?
System.out.print(i);
System.out.print(i);
0122
0123
Compilation error
None of these
Question 21
What will happen when you attempt to compile and run the following code?
boolean b1 = false;
else
{
System.out.println("Not equal! " + Output);
3. if (t1[0] == null)
4. {
10. }
11. System.out.println(t1[0]);
12. System.out.println(t2[1][0]);
The code will not compile because the array t2 is not initialized in an unconditional statement before
use.
The code will compile but a runtime exception will be thrown at line 12.
The code will compile but a runtime exception will be thrown at line 11.
None of these.
Question 23
What will happen when you attempt to compile and run the following code?
class Base
int i = 99;
System.out.println("Base.amethod()");
Base()
amethod();
{
int i = -1;
System.out.println(b.i);
b.amethod();
System.out.println("Derived.amethod()");
Derived.amethod()
-1
Derived.amethod()
Derived.amethod()
99
Derived.amethod()
99
Derived.amethod()
Question 24
System.out.print(t.myString);
Compilation Error
Prints : Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes and so on.
Prints : No No No No No No No No No No and so on.
Prints : Yes No Yes No Yes No Yes No Yes No and so on.
The Output cannot be determined
Question 25
Multiple objects of MyClass (given below) are used in a program that uses multiple Threads to create
new integer count. What will happen when other threads use the following code?
class MyClass
int yourNumber;
return ++myCount;
}
public void getYourNumber()
yourNumber = nextCount();
Question 26
2. {
5. {
8. System.out.println(s1 == s2);
9. System.out.println(s1.equals(s2));
11. System.out.println(s3.equals(s1));
13. }
14. }
15.
17. {
19. }
Lines 10 and 12
Line 12 only
Lines 8 and 10
None of these
Question 27
System.out.println("Same");
if (s1.equals(s2))
System.out.println("Equals");
Same
Equals
The code compiles, but nothing is displayed upon execution.
The code fails to compile.
Question 28
class Parent
System.out.println("Parent's method1()");
System.out.println("Parent's method2()");
method1();
}
System.out.println("Child's method1()");
p.method2();
What will happen when you attempt to compile and run the following code snippet?
if(str.equals(buffer))
else
Question 30
What will happen when you attempt to compile and run the following code?
String myName;
MyThread(String name)
myName = name;
System.out.println(myName);
try
// XXX
mt2.start();
catch(InterruptedException ex)