Professional Documents
Culture Documents
Class C {
}}
4.Runtime Exception
Ans: 2
Explanation:
2)
interface I{
void f1(); // 1
Explanation:
all methods declared within an interface are implicitly public, a weaker access
level can not be declared.
3)
class C{
int i;
int i; //1
System.out.println(a+b+c); //5
}}
4.prints 3
Answer 2
Explanation:
The access modifiers public, protected and private, can not be applied to
variables declared inside methods.
4)
class C {
}}
java command A B C
1.Prints: ABC
4.Runtime Exception
Answer 2
Explanation:
5)
class C{
static int s;
C obj=new C();
obj.m1();
System.out.println(s);
void m1();
int x=1;
m2(x);
System.out.println(x+"");
x=x*2;
s=x;
}}
1.prints 1,2
2.prints 2,0
3.prints 2,2
5.Noneofthe above
Answer: 1
Explanation:
Only objects and arrays are passed by reference.other are passed by value.s is
a static variable which is global to the class
6)
class C {
int i1=1;
switch(i1){
case 1:
System.out.println("one");
case 2:
System.out.println("two");
case 3:
System.out.println("three");
}}}
2.prints one
4.Runtime exceptionf
Answer: 1
Explanation:
7)
1.LinkedList
2.TreeMap
3.HashMap
4.HashSet
5.None of the above
Answer: 4
8)
class C1
static interface I
static class C2
System.out.println("object created");
3.Runtime Excepion
Answer: 1
Explanation:
9)
class C1
static class C2
System.out.println(C1.C2.i1);
1.prints 0
3.Runtime exception
Answer: 1
Explanation:
10)
A signed data type has an equal number of non-zero positive and negative values
available
1.true
2.false
Answer: 2
Explanation:
The range of negative numbers is greater by 1 than the range of positive numbers
11)
class C{
static int f1(int i) {
System.out.print(i + ",");
return 0;
}
public static void main (String[] args) {
int i = 0;
i = i++ + f1(i);
System.out.print(i);
}}
Prints: 0,0
Prints: 1,0
Prints: 0,1
Compile-time error
Explanation:
No Explanation Available
Ans: 2
12)
class C{
static String m(int i) {return "int";}
static String m(float i) {return "float";}
public static void main (String[] args) {
long a1 = 1; double b1 = 2;
System.out.print(m(a1)+","+ m(b1));
}}
Prints: float,double
Prints: float,float
Prints: double,float
Compile-time error
Explanation:
No Explanation Available
Ans: 4
13)
class C
{
public static void main(String a[])
{
C c1=new C();
C c2=m1(c1);
C c3=new C();
c2=c3; //6
anothermethod();
}
static C m1(C ob1){
ob1 =new C();
return ob1;
}
}
After line 6, how many objects are eligible for garbage collection?
Ans: 1
Ans: 2
Ans: 3
Ans: 4
Explanation:
No Explanation Available
Ans: 2
14)
Ans: 1
Ans: 2
Ans: 3
Explanation:
No Explanation Available
Ans: 4
15)
class c2
{
{
System.out.println("initializer");
}
public static void main(String a[])
{
System.out.println("main");
c2 ob1=new c2();
}
}
Explanation:
No Explanation Available
Ans: 1
16)
class c1
{
public static void main(String a[])
{
c1 ob1=new c1();
Object ob2=ob1;
System.out.println(ob2 instanceof Object);
System.out.println(ob2 instanceof c1);
}
}
Prints true,false
Print false,true
Prints true,true
Explanation:
No Explanation Available
Ans: 3
17)
Prints nothing
Prints: 1
Prints: 01
Compile-time error
Explanation:
a.run() method was called instead of a.start(); so the full program runs as a
single thread so a.run() is guaranteed to complete
Ans: 2
18)
class bike
{
}
class arr extends bike{
public static void main(String[] args) {
arr[] a1=new arr[2];
bike[] a2;
a2=a1; //3
arr[] a3;
a3=a1; //5
}}
Runtime exception
Explanation:
bike is the superclass of arr.so they are compatible(superobject=subobject)
but subobject=superobject not allowed
Ans: 4
19)
class C{
public static void main (String[] args) {
String s1="hjhh"; // 1
String s2="\u0002"; //2
String s3="'\\'"; //3
}}
Runtime exception
Explanation:
A String literal is a sequence of characters enclosed in double quotes
Ans: 5
20)
Which data type is wider for the purpose of casting: float or long?
float
long
Explanation:
float is wider than long, because the entire range of long fits within the
range of float.
Ans: 1
21)
class C{
public static void main (String[] args) {
byte b1=33; //1
b1++; //2
byte b2=55; //3
b2=b1+1; //4
System.out.println(b1+""+b2);
}}
prints 34,56
runtime exception
Explanation:
b1+1 returns an integer value which can not be assigned to a byte variable
Ans: 2
22)
import java.util.*;
class C
{
final Vector v;
C()
{
v=new Vector();
}
C(int i)
{
}
public void someMethod()
{
System.out.println(v.isEmpty());
}
}
runtime exception
Explanation:
No Explanation Available
Ans: 1
23)
class C1{
public void m1(){ // 1
}
}
class C2 extends C1{ //2
private void m1(){
}
}
Runtime exception
Explanation:
tending to assign weaker access not allowed
Ans: 2
24)
interface I{
int i; // line 1
}
class C implements I{
public static void main(String a[]){
System.out.println(i);
System.out.println(i++); //line 2
}
}
Runtime exception
Noneofthe above
Explanation:
interface constants are final so,they must be initialized when declaring it and
they can not be altered
Ans: 1
25)
true
true
Explanation:
An abstract without abstract methods is allowed
Ans: 2
26)
<![CDATA[
class C {
public static void main(String[] args) {
boolean b1;
b1=3<4<5; //1
System.out.println(b1); //2
}}
]]>
Runtime exception
Explanation:
<![CDATA[
3<4<5 evaulates to true<5 -->it's a wrong expression so it results in
compiletime error
]]>
Ans: 1
27)
class C{
public static void main(String[] args) {
try
{
int i1=3/0;
}
catch(Exception e)
{
System.out.println("exception1");
}
catch(NullPointerException e)
{
System.out.println("exception2");
}
finally
{
System.out.println("finally");
}
}}
runtime exception
Ans: 1
28)
class C {
public static void main(String[] args) {
char c1=65;
switch(c1){
case 'A':
System.out.println("one");
default:
System.out.println("two");
case 'b':
System.out.println("three");
}}}
Runtime exception
Explanation:
char is a legal value for switch clause
Ans: 1
29)
class c1{
void go(){}
}
class c2 extends c1
{
String go()
{
return null;
}
}
runtime exceptione
Explanation:
No Explanation Available
Ans: 1
30)
class base
{
base(int c)
{
System.out.println("base");
}
}
class Super extends base
{
Super()
{
System.out.println("super");
}
public static void main(String [] a)
{
base b1=new Super();
}
}
runtime exceptione
Explanation:
No Explanation Available
Ans: 1
31)
class c2{
final int i1;
c2()
{
i1=i1+1;
}
{
i1=2;
}
public static void main(String a[])
{
c2 ob1=new c2();
System.out.println(ob1.i1);
}
}
prints 3
prints 2
Explanation:
No Explanation Available
Ans: 1
32)
class C{
public static void main(String a[]) {
int i1=9;
int i2;
if(i1>3) {
i2=8;
}
System.out.println(i2);
}}
Runtim error
prints 8
prints 0
Explanation:
since variable i1 is not final the value is not known at compiletime itself.so
generate compile time error
Ans: 1
33)
Runtime Exception
Explanation:
If the start method is invoked on a thread that is already running, then an
IllegalThreadStateException will probably be thrown
Ans: 2
34)
class c1
{
static{
System.out.println("static");
}
public static void main(String a[])
{
System.out.println("main");
}
}
prints main
compiletime error
Explanation:
No Explanation Available
Ans: 1
35)
class A{
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
int a1 = 1; long b1 = 2;
System.out.print(m(a1)+","+ m(b1));
}}
prints float,foat
prints float,double
prints double,double
Explanation:
No Explanation Available
Ans: 1
36)
byte
int
long
Explanation:
The result of all arithmetic performed with the binary operators (not the
assignment operators) is an int, a long, a float, or a double. Here byte and
char are promoted to int, so the result is an int.
Ans: 2
37)
runtime Exception
Explanation:
No Explanation Available
Ans: 4
38)
void run()
Explanation:
No Explanation Available
Ans: 3
39)
class C{
public static void main(String args[]) {
int a = 1;
a += ++a + a++;
System.out.print(a);
}}
Runtime Exception
Prints 5
Prints 4
Explanation:
No Explanation Available
Ans: 3
40)
interface I { //1
public class Inner { ///2
Inner ( ) {
System .out . println ( "Inner Created" ) ;
}
};
};
Explanation:
No Explanation Available
Ans: 3
41)
Explanation:
since the class C2 is abstract it can contain abstract methods
Ans: 3
42)
interface I{
void f1(); // 1
public void f2(); // 2
protected void f3(); // 3
private void f4(); // 4
abstract void f5(); // 5
}
line 1,2,3,4
line 3,4
line 3
line 2,3,4
line 3,4,5
Explanation:
all methods declared within an interface are implicitly public, a weaker access
level can not be declared
Ans: 2
43)
}}
Runtime Exception
Explanation:
No Explanation Available
Ans: 2
44)
class command {
public static void main (String[] a1) {
System.out.println(a1.length()); //1
System.out.println(a1[0]); //2
System.out.println(a1); //3
//2
}}
Runtime exception
Explanation:
length is not a method. it's a variable
Ans: 1
45)
abstract class A {} // 1
transient class B {} // 2
private class C {} // 3
static class D {} // 4
Which of these declarations will not produce a compile-time error?
Ans: 1
Ans: 2
Ans: 3
Ans: 4
Explanation:
The modifiers, private and static, can be applied to a nested class, but can
not be applied to a class that is not nested transient is allowed only for
variables.
Ans: 1
46)
class c1
{
public void m1(Object o1)
{
System.out.println("object");
}
public void m1(String o1)
{
System.out.println("string");
}
public int m1(int c)
{
return c;
}
public static void main(String a[])
{
c1 ob1=new c1();
ob1.m1("hai");
}
}
print object
prints string
Explanation:
No Explanation Available
Ans: 2
47)
Degrees
Radians
Both
Explanation:
No Explanation Available
Ans: 2
48)
class base
{
base()
{
System.out.println("base");
}
base(int i1)
{
}
}
class Super extends base
{
Super()
{
System.out.println("super");
super(1);
}
public static void main(String [] a)
{
base b1=new Super();
}
}
Explanation:
No Explanation Available
Ans: 1
49)
The Throwable class is the superclass of all exceptions in the Java language.
true
false
Explanation:
No Explanation Available
Ans: 1
50)
class C {
private String get ( String str ) {
try
{
throw new Exception ( ) ;
return str ;
}
catch ( Exception e )
{
return null ;
}
}
public static void main ( String peace [ ] ) {
try
{
System.out.println ( ( new C ( ) ).get ( " C " ) ) ;
}
catch ( Exception e )
{
System.out.println( "Exception" ) ;
}
}
};
compile time error
prints Exception
Explanation:
No Explanation Available
Ans: 1
51)
Which of the following methods are static members of the Thread class?
join
run
sleep
start
wait
Explanation:
No Explanation Available
Ans: 3
52)
class C {
public static void main(String[] args) {
try
{
int i1=3/0;
}
System.out.println("hai");
catch(NullpointerException e)
{
}
finally
{
System.out.println("finally);
}
}}
Explanation:
No Explanation Available
Ans: 1
53)
class c1
{
public static void main(String a[])
{
System.out.println(Double.NaN==Double.NaN);
System.out.println(Double.NaN!=Double.NaN);
System.out.println(Float.NaN==Double.NaN);
}
}
compiletime error
Explanation:
No Explanation Available
Ans: 1
54)
class C {
public static void main ( String ka [ ] ) {
Thread t = Thread . currentThread ( ) ;
t . setPriority ( - 1 ) ;
System . out . println ( " Done ! " ) ;
}
};
Runtime Exception
Explanation:
No Explanation Available
Ans: 2
55)
class c1
{
public void m1()
{
System.out.println("m1 method in C1 class");
}
}
class c2
{
public c1 m1()
{
return new c1(){
public void m1()
{
System.out.println("m1 mehod in anonymous class");
}};}
public static void main(String a[])
{
Runtime error
Explanation:
the anonymous class overrides the m1 method in c1.so according to the dynamic
dispatch the m1 method in the anonymous is called
Ans: 2
56)
}
}
Runtime Exception
Explanation:
No Explanation Available
Ans: 1
57)
class C {
static void m1(Object x) {System.out.print("Object");}
static void m1(String x) {System.out.print("String");}
public static void main(String[] args) {
m1(null);
}}
Prints Object
Prints String
compiletime error
Explanation:
The more specific of the two, m(String x), is chosen
Ans: 2
58)
true
false
Explanation:
No Explanation Available
Ans: 1
59)
true
false
Explanation:
No Explanation Available
Ans: 1
60)
class C {
public static void main(String[] args) {
int x=2;
int y=3;
if((y==x++)|(x<++y)){
System.out.println(x+""+y);
}
}}
prints 34
prints 33
Runtime exception
Explanation:
No Explanation Available
Ans: 1
61)
interface I{
final class C1 { //1
static int i=9;; //2
}
}
class C2 implements I{
public static void main(String a[]){
System.out.println(I.C1.i); ///3
}}
prints 9
Runtime exception
Explanation:
interfaces classes are by default static,final. so,no compile time errors are
genearated
Ans: 4
62)
class C{
public static void main(String[] args) {
try
{
try
{
try
{
}
catch(RuntimeException e)
{
}
}
catch(Exception e)
{
}
}
catch(NullPointerException e)
{
}
finally
{
System.out.println("finally");
}
}}
Prints finally
Runtime Exception
Explanation:
No Explanation Available
Ans: 1
63)
class C {
public static void main ( String ka [ ] ) {
while ( false ) {
System.out.println ( "Value" ) ;
}
}
}
Runtime Exception
Explanation:
No Explanation Available
Ans: 1
64)
class C {
public static void main(String[] args) {
System.out.println(4+5+"String");
}}
prints 9string
prints 45string
compile time error
Runtime exception
Explanation:
arguments to the method are evalutated from left to right so 4+5+"string" ==>
9+string==>9string
Ans: 1
65)
class H {
public static void main (String[] args) {
String s1 = "HHH";
StringBuffer sb2 = new StringBuffer(s1);
System.out.print(sb2.equals(s1) + "," + s1.equals(sb2));
}}
Prints: false,false
Prints: true,false
Prints: false,true
Prints: true,true
Explanation:
s1 and sb2 are pointing to different object references
Ans: 1
66)
has-a
is -a
Explanation:
No Explanation available
Ans: 2
67)
Ans: 1
Ans: 2
Ans: 3
Explanation:
Main thread is a thread and calling run methods will not creat thread.
Ans: 1
68)
true
false
Explanation:
No Explanation available
Ans: 1
69)
Which of the following modifiers can be applied to a class that is not a nested
class?
public
protected
private
static
Explanation:
No Explanation Available
Ans: 1
70)
if(0.0 == -0.0) {
System.out.println("true");
}
else{
System.out.println("false");
}
prints false
prints true
Explanation:
No Explanation available
Ans: 2
71)
class C {
public static void main(String[] args) {
double d1 = Math.floor(0.5);
double d2 = Math.floor(1.5);
System.out.print(d1 + "," + d2);
}}
Prints: 0.0,1.0
Prints: 0.0,2.0
Prints: 1.0,1.0
Prints: 1.0,2.0
Explanation:
No Explanation Available
Ans: 1
72)
System.out.println("String".substring(0,4));
This statement will Print
Explanation:
No Explanation Available
Ans: 2
73)
if("String".replace('t','T') == "String".replace('t','T'))
System.out.println("Equal");
else
System.out.println("Not Equal");
Explanation:
No Explanation Available
Ans: 2
74)
Which of the following classes will not allow unsynchronized read operations
by multiple threads?
Vector
TreeMap
TreeSet
HashMap
HashSet
Explanation:
No Explanation Available
Ans: 1
75)
true
false
Explanation:
No Explanation available
Ans: 2