`
happyjeef18
  • 浏览: 14217 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

scjp面试题

    博客分类:
  • java
 
阅读更多
转自:
http://www.blogjava.net/xxxzheng/archive/2005/11/30/22007.aspx

PART 1

1.public static void main(String args[]) {
Boolean a[]=new Boolean[4];
int I= 1;
System.out.println(a[I]); }
What will be printed?
Compilation Error in Line 2
Compilation Error in line 4
Exception in Line 4
Will print true
Will print false
Will print null

Ans:F
2.
public static void main(String args[]) {
Integer b= new Integer(10);
Add(b);
System.out.println(b.intvalue());
}
void Add(Integer b)
{
int I= b.intvalue();
I+=3;
b= new Integer(I)
}

What will be printed out?

Will print 13
Will print 10
Compilation Error in Line 4 ?. implicit conversion to Integer to
String is not possible
Compilation Error in line 10 you can't re initialize a Wrapper
class
Exception in Line 10
Ans:b

class text{
public static void main(String args[])
{
String a =args[1];
String b =args[2];
String c =args[3];
}
}
if you will execute java text cat dog sheep what will be the value of
the 'c' ?
cat
dog
sheep
Compilation Error
Exception will occur
Ans:E

4. public static void main(String args[])
{
Float f=new Float(4.2f);
Float c;
Double d=new Double(4.2);
float fl=4.2f;
c=f;
}
which will return true?. Select all
f.equls(d)
c==f
c==d
c.equls(f)

Ans:B,D

5. public static void main(String args[])
{
String s;
System.out.println("s = "+s);
}
what will be printed out?
Compilation Error
An Exception will occur
Will print s= null
Will print s=

Ans:A
6. class s extends Thread{int j=0;
public void run() {
try{Thread.sleep(5000);}
catch(Exception e){}

j=100;
}
public static void main(String args[])
{
s t1=new s();
t1.start();
System.out.println(t1.j);
}
}

what you have to do to ensure that 'j' will print 100

you have make t1 as Daemon Thread
You have join the t1 to main
You have to suspend the main when the thread starts and resume it after
the value of 'j' is set to 100
You have to interrupt the main thread

Ans:B
7. What will happen if you compile/run this code?

1: public class Q1 implements Runnable
2: {
3: public void run(String s)
4: {
5: System.out.println("Before start Thread :"+s);
6:
7: System.out.println("After stop of Thread :"+s);
8: }
9:
10: public static void main(String[] args)
11: {
12: Q1 a = new Q1();
13: Thread t=new Thread(a);
14: t.start();}
15: }

A) Compilation error at line 1
B) Runtime exception at line 13.
C) Compilation error at line 14
D) Prints "Before start of Thread "
After Start of Thread

Ans:A
8. class s implements Runnable{
int x=0,y=0;
int addX(){x++; return x;}
int addY(){y++; return y;}

public void run()
{
for(int i=0;i<10;i++)
System.out.println(addX()+""+addY());
}

public static void main(String args[])
{
s run=new s();
Thread t1=new Thread(run);
Thread t2=new Thread(run);
t1.start();
t2.start();
}
}

Compile time Error There is no start method
Will print in this order 11 22 33厖
Will print but not exactly in an order (eg: 123124 3厖)
Will print in this order 12 3厖123厖
Will print in this order 123厖?.

Ans:C
9.
class s implements Runnable{
int x=0,y=0;
synchronized void addX(){x++; }
synchronized void addY(){y++; }
void addXY(){x++;y++;}
boolean check() { return (x>y)? true:false;)

public void run()
{
////
System.out.println(check()); }
public static void main(String args[])
{ s run=new s();
Thread t1=new Thread(run);
Thread t2=new Thread(run);
t1.start();
t2.start();
}
}
If this methods are called in which order the check will return true?
Select all that apply
call addX() and addY() simultaneously for number of times in run()
call addY() and addX() simultaneously for number of times in run()
all addXY() for number of times in run()

Ans:B,C

10. What is the name of the method used to start a thread execution?
A. init();
B. start();
C. run();
D. resume();
Ans:B

11. Which two methods may not directly cause a thread to stop
executing?
A. sleep();
B. stop();
C. yield();
D. wait();
E. notify();
F. notifyAll()
G. synchronized()
Ans:EF

12. class Outer{
class Inner{}
}
How will you create an instance of Inner Class out side? Select 2
Inner a= new Inner();
Outer o= new Outer();Inner a= new o.Inner();
Outer o= new Outer();Outer.Inner a= new o.Inner();
Outer.Inner a= new Outer().new Inner();
Ans:CD

13. What a static inner class can access select one
A. Any variables in the enclosing scope
B. Final variables in the enclosing scope
C. Static variables declared in side the method
D. Static variables declared in side the outer class
Ans:D

14. What is true about inner class? Select one
an Inner class can access all variables in the any enclosing scope
an Inner class can access all variables in the enclosing scope
an inner class can be declared as private
an Anonymous inner class can be extended from class and can implement
an interface

Ans:C

15. A ----- is a class that is a collection which cannot contain any
duplicate elements which maintains its elements in ascending order.
SortedSet
Set
Sorted Map
Collection
TreeSet

Ans: A

16. What is true about Map select one
A map will order the elements in an order according the key
A map will use unique key to store value
A map will use unique key to identify value inside the map
Ans:C
17. Which Method Returns the parent part of the pathname of this File
object, or null if the name has no parent part?
getParent()
getParentDir()
getParentDirectory()
parentDirectory()

Ans:a

18. Which class should be used in situations that require writing
characters rather than bytes.
LineNumberWriter
PrintWriter
PrintStream
PrintOutputReader
Ans:B

19. To Write End of a File f=new File(C:\\hello.txt");
new RandomAccessFile(f,"rw").writeByte(?;
FileInputStream fis=new FileInputStream(f,true);
DataInputStream d=new DatainputStream(fis);
d.writeBytes(?
FilterInputStream fis=new FilterInputStream(f,true);
DataInputStream d=new DatainputStream(fis);
d.writeBytes(?
D. FileOutputStream fis=new FileOutputStream(f);
DataOutputStream d=new DataOutputStream(fis);
d.writeBytes(?
E. FilterOutputStream fis=new FilterOutputStream(f);
DataOutputStream d=new DataOutputStream(fis);
d.writeBytes(?

Ans:C
//Random access file needs to use seek() in order to write last

20. Which of the following is the correct form of constructor for
PrintWriter?
new PrintWriter(new OutputStreamReader(new File(".\hai.txt");
new PrintWriter(new File(".\hai.txt");
new PrintWriter(new OutputStream());
21. What is the return-type of the getSource of ActionEvent?
A. boolean
B. Boolean
C. void
D. ID of the event
E. Object

Ans:E
22. To Retrieve the cosine which method you will use?
cos
cosine
getCos
getCosine

Ans:A
23. Which class is handling Events for MouseMotionListener?
MouseMotionEvent
MouseEvent
MotionEvent
ActionEvent
Ans:B

24. Which of the following will create an empty file. select 2
FileInputStream
RandomAccessFile is constructed as "rw".
FileOutputStream.
Ans:A,B

25. import java.util.Stack;
public class aClass
{
public static void main(String []agrs)
{
Stack st1 = new Stack();
Stack st2 = new Stack();
new aClass().Method(st1, st2);
System.out.println("st1 has: " + st1);
System.out.println("st2 has: " + st2);
}
private void Method(Stack st1,Stack st2)
{
st2.push(new Integer(100));
st1 = st2;
}
}
A. print
st1 has: []
st2 has: [100]
B. error in st1 = st2
C. error in ("st1 has: " + st1)
D. print
st1 has: [100]
st2 has: [100]
Ans:A

26. class Super
{
int i=0;
Super(String s)
{
i=10;
}
}
class Sub extends Super
{
Sub(String s)
{ i=20; }
public static void main(String args[])
{
Sub b=new Sub("hello");
System.out.println(i);
}

What will be the value of i.?
A. Compilation Error
B. Runtime Error
C. 0
D. 10
E. 20
Ans:E

27. String s= "ABCD";
s.concat("E");
s.replace('C','F');
System.out.println(s);

What will be printed out?
A. Compilation Error says that String is immutable
B. ABFDE
C. ABCDE
D. ABCD
Ans:D

28. class sree
{
fun(){
static int I =0;
I++;
}
public static void main(String args[])
{
sree obj=new sree();
obj.fun();
obj.fun();
}
what will be the value of I ?
A. Compilation error
B. Run time Error
C. 1
D. 2
Ans:A
//you cannot declare a static variable inside the mehtod

29. which are the correct forms of Overloading Constructors for the
class hai?
public void hai(int a)
hai(int a,int b)
public hai(int a)
int hai(int c, int d)
int hai()
int hai(String s)
Ans:BC
//contstructors will not return any value

Which are the correct forms of overriding for the method void hai(int
a,int b)
public void hai(int a,int b)
protected void hai(int a,int b)
public hai(int a)
int hai(int c, int d)
int Hai()
int hai(String s)
Ans:A,B

31. public static void main(String args[])
{ try{
System.out.println("1");
/////AA
}
catch (RuntimeException x){
System.out.println("2");
return;
}
catch(Exception x){
System.out.println("3");
return;
}
finally{
System.out.println("4");
}
System.out.println("5");
}
if there is no Exception in the try block what will be the output?
Select all that apply?
A. 1
B. 2
C. 3
D. 4
E. 5
Ans:ade

32. class Sree
{
public static void main(String args[])
{ try{
return ;
}
catch(Exception e)
{System.out.println("Exception");}
finally{System.out.println("Finally");}
}
}
What will be the out put?
A. Compilation Error void main Can't return any value.
B. Prints Exception
C. Prints Exception Finally
D. Prints Finally
Ans:D

//If there is any value with return then result will be compilation
error

class Sree
{
public static void main(String args[])
{
System.out.println(hai());
}

static int hai()
{
try{
return 1;
}
catch(Exception e)
{System.out.println("Exception");}
finally{System.out.println("Finally");
return 2;}

}
}
What will be the out put?
A. Compilation Error void main Can't return any value.
B. Prints Exception
C. Prints Exception Finally
D. Prints Finally and 1
E.Prints Finally and 2
Ans:E


33. class Jith
{
public static void main(String args[])
{
try{
mymethod();
}
catch(Exception e)
{System.out.println("Exception");}
}
}
How will you declare mymethod so that it will throw a Exception? Select
one?
A. In method declaration give throw Exception and inside the method
give throws Exception
B. In method declaration give throws Exception and inside the method
give throw Exception
C. In method declaration give throw Exception and inside the method
give throw Exception
D. In method declaration give throws Exception and inside the method
give throws Exception
Ans:A
34. class Sreejith
{
public static void main(String args[])
{
try{
///ProtocolException
}
catch(IOException e)
{System.out.println("I");}
catch(Exception e)
{System.out.println("E");}
finally{System.out.println("F");}
}
}
//Exception is the super class of IOException and IOException is the
super class of ProtocolException

if a ProtocolException occurs in try block what will be the out put?
Select all
A. I
B. E
C. F
D. Compilation Error says that there is no matching catch block
Ans:AC

35.public static void main(String args[])
{
int a=10;
int b=20;
if(a=b)
System.out.println("Not Equal");
else
System.out.println("Equal");
}

What will be the output?
A. Equal
B. Not Equal
C. Compilation Error
D. An Exception will occurs
Ans:C
//because if will return a number

36. public static void main(String args[])
{

int t=0;
while(1)
{
if(t++<10)
break;
}
}
What will be the value of 't 'after the while loop ?
A. 11
B. 9
C. 10
D. Compilation Error
E. An Exception will occurs
Ans:D

37. Which of the following will give correct declaration statement for
float select 3
A. float f= 1.0;
B. float f= 3.222e+1;
C. float f= -1;
D. float f= 1;
E. float f=1.0f;

Ans:cde

38. public static void main(String args[])
{
int t=0xffff1;
int j=~t;
System.out.println(j);
}

What will be the output?
A. -1048562
B. -10
C. Exception
D. Compilation Error

Ans:A
//when you will use ~ then the +ive will become -ve

39. package foo;
class a{int c}
class b{private int d}
class c{public int e}
which can class access all the variables in the limit foo?

A. class a
B. class b
C. class c
D. none
Ans:B

40.
1.interface foo
2. {
3. int I=0;
4. }
5. class sreejith implements foo
6. {
7.public static void main(String args[])
8.{sreejith s =new sreejith();
9. int j=0;
10.j=s.I;
11.j=sreejith.I;
12. j=foo.I;
13. s.I=2;
}
}
Where will compilation Fail?
A.10
B. 11
C.12
D.13
E. No error
Ans:D
//variables inside the interface are by default public static final

41. Which of the following will create an array that can be used for 50
Strings? select 2

A. char a[][]
B. String a[]
C. String[]a;
D. String a[50];

Ans: BC

42. Where will be a 'is a ' relationship will occur select one?

A. interface Person
class Employee implements Person
B. interface Shape
interface Rectangle extends Shape
C. interface Component
class Objects implement Component
{Component [] Component;}

Ans:B

43. What will be the return type of the
(short)10/10.2*2;
A. short
B. int
C. double
D. float
E. Compilation Error
Ans:C

44.Write the value for the following?
6^3
Ans: 5

45. What is the modifier is used for a variable in the class to be
accessed in subclass only?
A. public
B. none
C. protected
D. private
Ans:C

46. What is the modifier is used to make a method to be overridden
in subclass?
A. final
B. abstract
C. static
D. volatile

Ans:B
47. class sreejith
{
String s;
public static void main(String args[])
{
sreejith ks=new sreejith();
int j,i;
i=ks.hai();
j=ks.hello();
System.out.println(i+" "+j);
}
int hai()
{
if (s==null)|| (s.length()==0))
return 10;
else return 0;
}
int hello()
{
if (s==null)| (s.length()==20))
return 10;
else return 0;
}
}
what will be printed out?
A. 10 10
B. 10 null
C. 0 0
D. Compilation Error
E. An Exception in hai
F. An Exception in hello
Ans:F

48. Which are keywords in Java? Select 2
A. transient
B. true
C. sizeof
D. implement
E. synchronize
Ans:AB
49. 1. class sreejith
2. {
3. public static void main(String args[])
4. {
5. String s="hello";
6. String s1="hello";
7. System.out.println(s1);
8. String s3=s1;
9. s1=null;
10. s=null;
11. }
Which line the garbage collector will invoke first?

Ans: never invoke in this method

50. To Read a File f=new File(C:\\hello.txt"); select all;

new RandomAccessFile(f,"rw").readByte(?;
FileInputStream fis=new FileInputStream(f);
DataInputStream d=new DatainputStream(fis);
d.readBytes(?
FilterInputStream fis=new FilterInputStream(f,true);
DataInputStream d=new DatainputStream(fis);
d.readBytes(?
D. FileOutputStream fis=new FileOutputStream(f);
DataOutputStream d=new DataOutputStream(fis);
d.readBytes(?
E. FilterOutputStream fis=new FilterOutputStream(f,true);
DataOutputStream d=new DataOutputStream(fis);
d.readBytes(?
Ans:AD

51.
public static void main(String args[])
{
int j=10;
int I=1;
do
{
if (I++>--j)
continue;
}while(I<5);
}
what will be the value of I and j after the loop?
Ans: I=5 J=6

52.
What should you use to position a Button within an application frame so
that the vertical of the Button is only affected when you will resize
the window?
A. FlowLayout
B. GridLayout
C. Center area of a BorderLayout
D. East or West of a BorderLayout
E. North or South of a BorderLayout
Ans: D

53.
class c extends Frame
{
c(){
setLayout(new BorderLayout());
Panel p1,p2;
p1=new Panel();
p2=new Panel();
p1.add(new Button("B1");
p2.add(new Button("B2");
add(new Button("B3"));
setsize(300,300);
setVisible(true);
}
public static void main(String args[])
{
c o = new c();
}
how will be buttons displayed?

Ans: Only button 3 will displayed and will occupy entire frame

54.
public static void main(String a[])
{
StringBuffer s=new StringBuffer("Java");
String c=new String("Java");
Hello(s,c);
System.out.println(s+c);

}
public static Hello(StringBuffer s, String c)
{
s.append("C");
c.repalce('A','D')
}
Write the Output?
Ans: JavaCJava

55.
class Outer{
class static Inner{}
}
How will you create an instance of static Inner Class? Select 2
Inner a= new Inner();
Outer o= new Outer();Outer.Inner a= new o.Inner();
Outer.Inner a= new Outer.new Inner();
Outer.Inner a= new Outer.Inner();

Ans: BD
56. . Write the range of int in values

-2147483648 TO + 2147483647

57. which will contain the body of the thread
run()
start()
stop()
main()
init()
Ans: A


57.Which of the following will give equal results?
3/2 b.3<2 c. 3*4 d. 3<<2
Ans: cd

58.
StringBuffer b=new StringBuffer();
try{
b.append("1");
method();
b.append("2");
}
catch(Exception a){
b.append("3");

}
finally{
b.append("4");}
b.append("5");
what will be the value of ' b' if the method throws
NullPointerException?

Ans: 1345

59.
if a method throws an ProtocolException(subclass of IOException) which
of the following can be used in declaring that method
a. public void method() throws Exception
b. public void method() throw Exception
c. public throws IOException void method()
d. public void method() throws IOException.
e. public void method() throws ProtocolException.

Ans:DE

60.What is the output when following code is run ?
1. class ForLoop{
2. public static void main(String args[]){
3. int i=0,j=5;
4. st: for(;;i++){
5. for(;;j--){
6. if(i>j)
7. break st;
8. }
9. }
10. System.out.println("i="+i+" j="+j);
11. }
12. }

1. i=0 j=-1
2. i=1 j=0
3. i=0 j=1
4. i=1 j=-1
5. Compiler error at line 4.

Ans : 1


61.What is the output when folllowing code is run?

1. class OuterOne{
2. class InnerOne extends OuterOne{}
3. static void thisMethod(){
4. Object o=(Object)new OuterOne();
5. OuterOne foo=(OuterOne)o;
6. }
7. public static void main(String args[]){
8. thisMethod();
9. }
10. }

1. Will compile fine,but at runtime a ClassCastException is thrown
2. Will compile and run fine
3. Compiler error at line 4
4. Compiler error at line 5

Ans: 2

62.public class Outer{
//////
}

What should be inserted at ///// so that the code will compile
fine?(Choose two)

1. private class Inner{
static int k;
}

2. static class Inner{
static int k;
}

3. public class Inner{
static int k;
}

4. public abstract class Inner{
public abstract void method();
}

Ans: 2,4



63.What will be the output when following code is run?

1. class ThisClass{
2. static void foo() throws Exception{
3. throw new Exception();
4. }
5. public static void main(String args[]){
6. try{
7. foo();
8. }catch(Exception e){
9. System.exit(0);
10. }
11. finally{
12. System.out.println("In finally");
13. }
14. }
15. }

1. Compiler error.
2. Code will compile fine and when run prints In finally.
3. Nothing will be printed.
4. Runtime exception.


Ans 2

64.import java.awt.*;
class X extends Frame{
X(){
setLayout(new BorderLayout());
setSize(600,600);
Panel p=new Panel();
Button b1=new Button("Button1");
p.add(b1);
Button b2=new Button("Button2");
add(p,BorderLayout.NORTH);
add(b2,BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String args[]){
new X();
}
}

How the components are displayed on the frame?

1. Components will be of equal size.

2. Button1 will be placed at top center and Button2 will occupy
rest of the area.

3. Button2 will be placed at top center of the frame and Button1
will be placed at bottom center of the frame.

4. Button2 will be placed at top center of the frame and Button1
will be placed at bottom of the frame from left end to right end.

5. Button1 will be placed at top center of the frame and Button2
will be placed at bottom of the frame from left end to right end.

Ans 5



65.File f=new File("aa.txt");
Suppose this file exists in your directory.
Which of the following can be used to write a line of text to the
end of this file?

1. RandomAccessFile f1=new RandomAccessFile(f,"r");
2. RandomAccessFile f1=new RandomAccessFile(f,"a");
3. RandomAccessFile f1=new RandomAccessFile(f,"rw");
4. RandomAccessFile f1=new RandomAccessFile(f,"w");
5. RandomAccessFile f1=new RandomAccessFile(f,"wr");

Ans 3



66.What is the value of j when printed?

class Foo{
public static void main(String args[]){
int x=4,j=0;
switch(x){
case 1:j++;
case 2:j++;
case 3:j++;
case 4:j++;
case 5:j++;
break;
default:j++;
}
System.out.println(j);
}
}

1. 1
2. Compiler error
3. 6
4. 3
5. 2

Ans 2

67.What will be the content of baz after the the following code is run?

class A{
public static void main(String args[]){
String bar=new String("blue");
String baz=new String("green");
String var=new String("red");
String c=baz;
baz=var;
bar=c;
baz=bar;
System.out.println(baz);
}
}

1. red
2. Compiler error
3. blue
4. null
5. green

Ans 5

68.import java.io.*;
class Excep{
static void method() throws Exception{
throw new EOFException();
}

public static void main(String args[]){
try{
method();
}catch(////){}
}
}

Which of the following should be placed at //// so that the code will
compile without errors?

1. IOException e
2. EOFException e
3. MalformedURLException e
4. NullPointerException e
5. Exception e
6. FileNotFoundException

Ans 5

69. What will be the output when the following code is run?
1. class ThisClass{
2. public static void main(String args[]){
3. Object o=(Object)new ThisClass();
4. Object s=new Object();
5. if(o.equals(s))
6. System.out.println("true");
7. }
8. }

1. Prints true
2. Nothing will be printed
3. Compiler error at line 3
4. Compiler error at line 5
5. ClassCastException will be thrown

Ans 2

70.Which of the following interfaces that ArrayList directly
implements?

1. Set
2. Map
3. SortedSet
4. List
5. Collection

Ans 4

71.What will be the output when following code runs?

1. class C1{
2. static int j=0;
3. public void method(int a){
4. j++;
5. }
6. }
7. class Test extends C1{
8. public int method(){
9. return j++;
10. }
11.
12. public void result(){
13. method(j);
14. System.out.println(j+method());
15. }
16. public static void main(String args[]){
17. new Test().result();
18. }
19. }

1. 3
2. 2
3. Compiler error at line 8
4. 1
5. 0

Ans 2

72.What will be printed when following code runs?
1. class Foo{
2. static void change(String s){
3. s=s.replace('j','l');
4. }
5.
6. public static void main(String args[]){
7. String s="java";
8. change(s);
9. System.out.println(s);
10. }
11. }

1. Compiler error
2. Will print lava
3. Runtime exception
4. Will print java

Ans 2

########

74. 1.public static void main(String args[]){
2. byte b=127;
3. byte c=126;
4. byte d=b-c;
5. System.out.println(d);
6.}

a) Compilation succeeds and prints 1.
b) Compilation succeeds but there will a Runtime Exception.
c) Compilation error at line no 4.
d) Compilation error at line no 5.

Ans c

75. Which is the valid identifier?

a) 123_hello
b) _12345
c) !hello
d) &sample

Ans B

76. Which two values are equal?

a) 16<<2
b) 16/3*2
c) 16>>2
d) 16<<<2
e) 16>>>2
f) 16/2

Ans CE

77. What is true about anonymous inner class?

a) Anonymous inner class can be declared as public.
b) Anonymous inner class can be declared as private.
c) Anonymous inner class can be declared as static.
d) Anonymous inner class can extend an abstract class.
e) Anonymous inner class can be declared as protected.

Ans D

78. Which will return an int value very nearer to and not greater than
the given double value?

a) int a=(int)Math.max(double)
b) int a=(int)Math.min(double)
c) int a=(int)Math.ceil(double)
d) int a=(int)Math.floor(double)
e) int a=(int)Math.round(double)

Ans d

79. 1.import java.io.IOException;
2.class Happy {
3. public static void main(String args[]) {
4. try {
5. method1();
6. }
7. catch(IOException e) {
8. System.out.println("Catch IOException");
9. }
10. catch(ArithmeticException e) {
11. System.out.println("Catch ArithmeticException");
12. }
13. finally {
14. System.out.println("Catch Finally");
15. }
16. }
17.
18. public void method1() throws IOException{
19. throw new IOException();
20. }
21.}

a) Compilation succeeds and prints "Catch IOException" and "Catch
Finally".
b) Compilation succeeds but there will be a run time exception.
c) Compilation error at line no.10
d) Compilation error at line no.5
e) Compilation error at line no.19

Ans D

80. Which method is used to construct and execute the thread?

a) execute()
b) run()
c) build()
d) start()

Ans D


81. import java.io.IOException;
class Happy {
public static void main(String args[]) {
try {
//code which throws an IOException
}
catch(IOException e) {
System.out.println("Catch IOException");
}
catch(Exception e) {
System.out.println("Catch Exception");
}
}
}

a) Compilation error complaining about class Excepion have not been
imported.
b) Clean compilation but run time Exception is thrown.
c) Execute fine.

Ans C
// Code runs wih out any problem

82. switch(s) {
default: System.out.println("Best Wishes");
}
Which datatype can 's' be?

a) byte
b) long
c) float
d) double

Ans A

83. public static void main(String args[]) {
int i=1;
int j=10;
do{
if(i>j)
continue;
j - -;
} while(++i<6);
System.out.println("i= "+i+" j= "+j);
}
What will be the output?

a) i=4 , j=5
b) i=5 , j=6
c) i=5 , j=5
d) i=4 , j=6
e) i=6 , j=5

Ans e

84. 1.public static void main(String args[]) {
2. Object o=new Float(12.4f);
3. Object []oa=new Object[1];
4. oa[0]=o;
5. o=null;
6. oa[0]=null;
7. return 0;
8.}

Where will be the garbage collecter may invoke?

a) just after line no.5
b) just after line no.6
c) just after line no.7
d) never invokes
e) compilation error
Ans B

85. Which will be used to resize only the horizontal length?

a) FlowLayout
b) GridBagLayout
c) BorderLayout
d) GridLayout
e) both b and d
f) both c and b
Ans C


86. Which of the following are valid declarations? (Select two)

a) char c= ' \' '
b) char c= "cafe"
c) char c= '\ucafe'
d) char c= '\u01001'
e) char c= '0x001'

Ans AC

87. Which of the following will not directly cause a thread to stop
?(Select two)

a) notify()
b) wait()
c) exits synchronized code
d) setPriority
e) InputSteam access
f) sleep()

Ans AC

88. What is the output?
class Happy {
public static void main(String args[]) {
default: for(int i=0;i<10;i++) {
mid: for(int j=0;j<5;j++) {
System.out.println(j);
}
}
}
}



Ans Error default is a key word

89. What is the value of char ?

a) -32768 - 32765
b) 0 - 65536
c) 0 - 32765
d) -65536 - 65535
e) 0 - 65535

Ans C

90. public class Happy {
public static void main(String args[]) {
int i=4;
int j=2;
methodA(i,j);
System.out.println(i);
}
static public void methodA(int i,int j) {
i<<=j;
}
}
What will be the output ?

a) compilation error
b) 16
c) 64
d) 4
f) runtime exception
Ans D


91. class Happy {
public static returntype methodA(double d) {
return (long)d;
}
public static void main(String args[]) {
methodA(4.426);
}
}
What should be the returntype in the methodA declaration?

a) void
b) int
c) long
d) double
e) nothing needed

Ans C

92. Which of the following is true of anonymous innerclass?

a) Anonymous inner class can be declared as private,protected or
public.
b) Anonymous inner class can implement multiple interfaces.
c) if not extended or implemented, an anonymous innerclass can become
immediate subclass of the outer class or implement an interface.
d) if not extended or implemented, an anonymous inner class can
become immediate subclass of outer class or implement mutiple interfaces.

Ans C

93. Which of the following are true about encapsulation ? (Select two)

a) Direct access to variables.
b) Access to variables through methods.
c) keeping methods protected.
d) use methods to access and modify the variable data.

Ans BD

94. 1.class Happy {
2. public static void main(String args[]) {
3. public class Sad {
4. public void methodA() {
5. System.out.println("inside sad");
6. }
7. }
8. System.out.println("inside happy");
9. }
10.}
What will be the output ?

a) compilation error at line no.3
b) compilation error at line no.4
c) compilation error at line no.5
d) compilation succeeds but runtime exception
e) clean compile and give the output "inside happy"
Ans A

95. 1.class Happy {
2. public static void main(String args[]) {
3. int x=4;
4. int y=2;
5. if(x/y) {
6. System.out.println(x+y);
7. }
8. }
9.}
What is the output?

a) prints 6
b) compilation error at line no.6
c) compilation error at line no.5
d) runtime exception occurs
e) prints 4
Ans C

96. How will you specify the access modifier for a method which should
be accessible only to the class in which it is defined?

a) protected
b) static
c) private
d) default
e) final
f) abstract

Ans C

97. class Happy {
public static int main(String args[]) {
Happy h=new Happy();
h.methodA();
}
public void methodA() {
System.out.println("Best Wishes");
}
}
What will the output ?

a)completime error
b)runtime excption
c)clean compile and output "Best Wishes"
Ans C

98. class Happy {
class Sad extends Exception {}
public void methodA() throws Sad {
System.out.println("Best Wishes");
}
public void methodB() // what exception is to be thrown {
methodA();
}
}

a) Exception
b) catch(Exception e)
c) Throwable
d) no code necessary
e) comple time error

Ans A

99. Which type of the event is raised when enter key is pressed on
java.awt.Component?

a) ClickEvent
b) ActionEvent
c) KeyEvent
d) ItemEvent
e) MouseEvent
ans C


100. What is true about constructors ? (Select two)

a) They initialize all the member variables
b) They initialize all the instance variables
c) If there is no constructor , the compiler includes default
constructor.
d) Compiler supplied constructor will invoke the default constructor
of the super class.

Ans CD


101.what is the output when you compile and run this code
int i=0;
while (i--<0)
{System.out.pintln("value of i is"+ i);}
System.out.pintln("the end");
}}

Ans the end

Write the answer
102. Write the range of char
0 -65535

103. Write the range of int in values
-2147483648- 2147483647
//study this one

104.what will be 's' after this code is run
String s="hello";
s.concat("mrs");
s.toUpperCase();
s+=" there";

hello there

105. which of these lines will compile if String s="example"
a. s>>=2; b. int i = s.length();
c. s+=3; d. char c= s[3]; e. s= s + "abcd";

Ans b,c,e

106. Under which conditions will a currently executing thread stop
a. when an interrupted exception occurs
b. when a thread of higher priority is ready(becomes runnable)
c. when the thread creates a new thread
d. when the stop() method is called.
ans: bd

107.which of the following statements are true
a. all the threads created in a class will come to an end at the same
time.
b. you can stop a thread indefinitely if you wish to.
c. you can start a thread only by extending the Thread class.
d. multiple threads accessing same variable will lead to producing
junk(not the exact word)data.
e.JVM exits after the main() thread is exited even if there might be
some threads running.

ans: bd

108. derived1 and derived2 are two different classes extending class
parent.
what will happen to this code
derived1 d1= new derived1();
parent p = new parent();
d1=(derived1)p;

Ans: valid at compile and may fail at run time


109. switch(x){
case 1: System.out.println("case 1");
case 2:
case 3: System.out.println("case 2");
break;
default : System.out.println("case 3");}
for what values of x will the output include case 3?

Ans:1,2,3

110. if(x>4){ System.out.println("case 1"); }
else
if(x>9){ System.out.println("case 2"); }
else{System.out.println("case 3");}
what value(s) of x will cause case 3 printed in the output

a. less than zero. b. less than 4 c. between 4 and 9 d.10 and
greater e. none
Ans B

111.
1. class A{
2 public static void main(String[] args) String s1="Hi";
3 String s2="There";
4
5 s2+=s1;
6 s1=null;
7 s1=s2;
8 System.out.println(s1);
9 } }
What is the earliest point where the memory space allocated to the
string in line 2(s1) can be released.
(A) before line 6
(B) before line 7
(C) before line 8
(D) before line 5

ans:A it will destroy the existing string and create new string
because strings are immutable


112. if only the height of the component need be changed what would you
use
a.FowLayout
b.GridBagLayout
c.center of BorderLayout
d.north and south of BorderLayout
e.east and west of BorderLayout

ans: e

113.which will contain the body of the thread
a.run() b.start() c.stop() d.main() e.init()
ans:a

114.what is the return type for the main() method which is the entry
point for the class
a.void b.int c.boolean d.string
ans: a
115.what is the return type for all the event listener methods
a.boolean b.depends on the listener c.void d.int e.event object
ans:c
116.what is the argument passed to the mouseListener method?

ans:MouseEvent

117.Which of the following will give equal results?
3/2 b.3<2 c. 3*4 d. 3<<2

ans:cd

118.select valid keywords
a. sizeof b. NULL c. instanceof d. implements e. extends

ans:de


119.class A {
String str;
int x;
int z;
public A(String s , int a)
{
str = s ;
x = a ;
}
public A(int a,String s){
///////}
class B extends A{
public B(String s, int a){
super(s,a);}
public B(String s, int a, int c) {
////A
z = c;}
What code can be inserted at /////A so that it fully implements
the existing code to initialize the values of x & str.

ans:this(s,a) is the perfect ans not super(s,a);

120.valid array declarations
a.int i[][]=new int[10,10];
b.int i[10][10]=new int[][];
c.int i [][]=new int[10][];
d.int[]i[]=new int[10][10];
e.int [][]i= new int[10][10];

ans: cde

121.if a method throws an ProtocolException(subclass of IOException)
which of the following can be used in declaring that method
a. public void method() throws Exception
b. public void method() throw Exception
c. public throws IOException void method()
d. public void method() throws IOException.
e. public void method() throws ProtocolException.

ans:you can use a base class or same class with declaration using
throws statement

122. if a classes should not be duplicated, ordered, and no special
search facility which of the following will you use.
a.list b.set c.map d.collection
ans:d

123.if a class is ordered, duplicated which would u use.
a.list b.set c.map d.collection e.enumeration.

Ans A

124.which of the following are valid array declaration for a string of
50 chars.
a. char c[][]; b.String []s; c.String s[]; d.String s[50]; e.Object
s[50];

Ans:bc

125.try{
method();
some code1
}
catch( IOException a){
some code2
}
finally{
some code3}
some code4
what will be excecuted if the method throws NullPointerException?

some code 3 will execute

126.
1.//
2 if(method()){
3 //}
4 else{
5 //
6 }
7 //
which line you insert the throw new Exception if the code should throw
the exception only if the method() returns true.
Line 3

127. if a variable should not be accessible in any way to code outside
the class, how will u declare the variable?
a. declare all variables as private
b. declare all methods as final
c. declare varicables as protected
Ans:a

128. what is valid declaration of a native method?
public native void me(){};
public void native me(){};
c. public native void me();
public void native me();
Ans:c

129. what keyword will you use so that the variable once declared
cannot be changed.
130. what keyword will you use so that the wait() and notify() can be
used.
131.outer: for(int i=1;i<3;i++)
inner: for(int j=1;j<3;j++)
{ if(j==2)
break outer;
System.out.println(i+" "+j);}

what will this code give as the output.
1 1



132.how do you represent 7 in octal form using not more than 4
characters.
07

133.what does the getId() method of events return?
ID of the event

134.what does >> and >>> represent?
right shift and unsigned right shift
135.which is possible parameter that can be passed into the construcor
of a FilterInputStream
a.File b.PrintStream c.FileOutputStream
d.FileInputStream e.RandomAccessFile
Ans D

136. which of the following cannot be added to a container
a.applet b.container c.menu item d.panel e.component
Ans:c
137. //
public class first{......}
which declarations are allowed in the //
a.class second{}
b.import java.util.*;
c.package def.exam;
d.public static final int i=10;
e./* this is a test program */

Ans:a,b,c,e

138.how will u assign String s=one if u type the following in the
command prompt for Animal class
java Animal one two three
public class Animal{
public static void main(String[]args){}
}
a.s=args[0] b.s=args[1] c.s=args[2] d.s=args[3] e.s=args[4]

Ans:a

139. String s=null.which of the following code not throw
NullPointerException
a. s!=null && s.length()=0 b. s!=null & s.length()=0
c s==null || s.length()=0 d s==null | s.length()=0
Ans:a,c


140. What is true about Garbage collector :
a. programmer can create and delete memory
b. is your program guaranteed that will run without memory leaks
c. programmer can make variables eligible for garbage collection

Ans:c

141.valid Inner class declaration select 2
a. private class c b. new simpleInterface() {
c. new somplexInterface(x){..} d. private final abstract class x{
e. new simpleclass(x) implements simpleinterface {

Ans:a,b

142. what is true about inner class select 2
a. inner class can be instantiated only in it's enclosing class
b. they can be accessed only within the enclosing class
c. they cannot implement interface
d. They can extend a class
e. they can access final varaibles within their declaration scope

Ans:d,e

143.Which of the following will describe a perfect encapsulation?
select 2
a. making all method private b. making all variables private
c. making class final d. making methods to public

e. making all variables private and access it through methods

Ans:be

144. what type of variables should be used for the following select all
Class Employee is a person, has a vector to store something,
has a number of dependents, has a date attribute
vector b. int c. person d. object e. Date

Ans: a,b,d

145.public class base{
public int b;
public float method(float f) {
int i;}}
public class sub extends base {
private float f1;
public static void main (String args[]) {
base b=new base();
float g;
int j;
//
}} what is valid at //
a. g=f1; b. g=b.f; c. j=b.i; d. b.method(10.0f) e. g=b.b
Ans:de

146. class base{long l=10
public void main(String args[])
{base x, y,z;
long l=10;
x=new base();
y=new base();
z=y }}
which will return true
a. x==y b. l==x c. y==z d. l==10.0
Ans:c,d

147. public class base {
String name;
String value;
public base(String n,String m) {
name=n;
value=m; }
public String toString() {
return name;}}
public class sub extends base {
public String toString() {
return name+":"+value;}
public sub(String n, String m){
super(n,m); }
public static void main(String args[]) {
base b=new base("first","1st");
base b1=new sub("second","2nd");
System.out.println(b.toString()+b1.toString());
}}
what will be the output
a. compilation error
b. first second : 2nd
c. second : 2 second : 2nd

Ans:b
148. int i[] =new int[10]
what is the value of i[5]
149. what is the output when you compile and run the following code?
public class Sup{
static StringBuffer sb1=new StringBuffer("Hello");
static StringBuffer sb2=new StringBuffer("Hello");
public static void main(String args[]){
aMethod(sb1,sb2);
System.out.println("sb1 is"+sb"\n"+"sb2 is"+sb2);
}
public static aMethod(StringBuffer sb1,StringBuffer sb2){
sb2.append(" there");
sb1=sb2;
}}
(a) compilation error
(b) invalid argument to System.out.println
(c) compile and output is
sb1 is Hello sb2 is Hello there
(d) compile and output is
sb1 is Hello there sb2 is Hello
(e) compile and output is
sb1 is Hello there sb2 is Hello there

Ans:c
here you will have two pointer which is pointing to hello( sb1, and a
methods local variable sb1 ) you are changing the reference of local
sb1 so it will not affect the orginal one in case of sb2 both local and
original are pointing to same as in the case of sb1 but here you are
changing the contents so it will affect the original.

150. class Happy {
public static void main(String args[]) {
float [] [] f1={ {1.2f,2.3f},{4.5f,5.6f}};
Object oo=f1;
f1[1] =oo;
System.out.println("Best Wishes "+f1[1] );
}
}
What will the output ?

a) {4.5,5.6}
b) 4.5
c) compilation error in line no.5
d) exception

Ans C


151.outer:
for(i=1;i<3;i++)
{ for(j=1;j<3;j++)
{
if(j==2)
continue;
System.out.println("i is"+ i " j is" + j);}}
152)polygon is Drawable and has information vertices stored in vector,
a color and fill flag(which is true or false)
Which of the following felds you will include in class Polygon.
Select most appropriate.
[a] vector [b] Color [c] boolean [d] Boolean [e] Object [f] String

Ans:a,b,c
153.What is the valid declaration of native method
[a] public native void method(); [b] public void native method();
[c] public void native method(){} [d] public native void method(){}

Ans:a

154. class Happy {
static StringBuffer sb1=new StringBuffer("A");
static StringBuffer sb2=new StringBuffer("B");
public static void main(String args[]) {
Happy h=new Happy();
h.methodA(sb1,sb2);
System.out.println(sb1+" , "+sb2);
}
public void methodA(StringBuffer x,StringBuffer y) {
y.append(x);
x=y;
}
}
What will the output ?

a) BA,BA
b) A,AB
c) BA,AB
d) A,BA
e) A,A
f) compilation error

Ans D


155.
1Class X {
2 Frame f = new Frame(); String s;
3 Button b=new Button("Click me");
4 Add(b);
5 b.addActionListener(new ActionListener(){
6public void actionPerformed(ActionEvent ae){
5 s= "message";
6System.out.println("message is "+s);
7}})}
What happens when the button ("click me" ) is pressed.
[a] compilation error at line 1 [b] prints message is click me.
[c] compilation error at line 5 [d] print the message is message

Ans:D

156. class Happy {
String s="Hello";
public static void main(String args[]) {
Happy h=new Happy();
h.methodA(s);
System.out.println(h.s);
}
public static void methodA(String s) {
s.replace('a','e');
s+=" World !!!" ;
}
}
What will the output ?

a) compile error because string is not static.
b) prints "Hello World !!!"
c) prints "Hello"
d) prints " World !!!"
e) Runtime exception occurs

Ans A



157. An application consists of a set of user interfaces & the various
operations are to be performed based on the various user invoked
events. Which of the following are true for the above application
(a) Extending Adapter classes is not suitable
(b) Some listener interface must be implemented
(c) If a particular event listener is implemented only the methods of
interest to us need to be implemented
(d) Multiple event listeners can be added to a single component
(e) The order in which the events are processed is the order in which
the listeners are added

Ans:a,d


158. class Happy {
public int getLength() {
System.out.println("int version");
}
}
class Life extends Happy {
public long getLength() {
System.out.println("long version");
}
public static void main(String args[]) {
Happy e=new Life();
e.getLength();
}
}
Which method gets executed?

a) int version
b) long version
c) compile time error
d) run time exception
e) clean compile but no output
Ans C

160. class Happy {
public static void main(String args[]) {
int i=1;
int j=i++;
if ((i == (++j) ) & ( (i++) = = j ) {
i+=j;
}
}
}
What will the value of i after this code execution ?

a) 4
b) 5
c) 2
d) 3
e) compilation error

Ans B


161. class Happy extends Frame{
Happy() {
setLayout("FlowLayout");
add(new Button("hello1");
add(new Button("hello2");
pack();
show();
}
public static void main(String args[]) {
Happy h=new Happy();
}
}

a) only one button will be shown with label "hello2".
b) only one button will be shown with label "hello1".
c) two buttons will be shown .
d) compilation error.

Ans D

162. class Happy implements Runnable {
private int x;
private int y;
public void run() {
//some code
}
public static void main(String args[]) {
Happy h=new Happy();
new Thread(h).start();
new Thread(h).start();
}
void setX(int i) {
x=i;
}
void setY(int j) {
y=j;
}
synchronized void setXY(int i) {
setX(i);
setY(i);
}
synchronized boolean check() {
return x!=y;
}
}
Select a true statement from the following.

a) check() never returns true.
b) multiple threads accessing setX(),setY() can result in check()
returning true.
c) multiple threads accessing setXY() can result in check()
returning true.
d) compilation error because there is no start() .
Ans B


163) What will you write in the palce of 'xxx' to make instantce of
inner class
class OuterOne {
class InnerOne {}
}
class Test {
OuterOne o=new OuterOne();
//write code to create an instance of the inner class
}

a)o.InnerOne in = o.new InnerOne();
b)InnerOne in=new o.new InnerOne();
c)OuterOne.InnerOne in =new o. InnerOne();
d)OuterOne.InnerOne in = o.new InnerOne();


Ans D


164)Examine the code below and select the best answer
class OuterOne {
static class InerOne{}
}

a)we can't make instantce of innerclass outside class OuterOne
b) ' InnerOne in=new Inner()' will make instance of inner class in
another class
c) ' OuterOne.InnerOne in=new OuterOne.Inner()'
d) ' InnerOne in=new OuterOne().new Inner()' will make instance of
inner class in another class


Ans C

165)
1) class Foo {
2) public static void main(String args[]) {
3) int s=10;
4) s>>=10;
5) System.out.println("s=" +s);
6) }
7) }

a) compileerror at line 4
b) clean compile but exception at runtime
c) 0
d) 1
Ans C

166)
1) class Foo {
2) public String toString(){
3) return ""+3;
4) }}
5) class FooTest extends Foo {
6) public static void main(String as[]){
7) FooTest f=new FooTest ();
8) System.out.println(f);
9) }
10) public String toString(){
11) return super.toString()+4;
12) }
13) }

a) compile error
b) clean compile but exception at runtime
c) 43
d)34
e) 7

Ans 34

167) write output
int i=0
int j=5
out: for( ; ; i++)
for( ; ; j--)
if( i > j)
break out;
System.out.println(i+" " +j)
AnS 0 -1

168) what will get printed
1) class Foo {
2) public static void main(string as []) {
3) int j=1;
4) int x=4;
5) switch(x) {
6) case 1: j++;
7) case 2: j++;
8) case 3: j++;
9) case 4: j++;
10) case 5: j++;
11) default : j++;
12) }
13) System.out.println(x+j);
14) }
15) }

Ans 8

169) which of the following can put in the place of xxx to indicate
myMethod may throw an exception
class Foo {
void myMethod()//xxxxx {......}
}

a)Exception
b)Error
c)RuntimeException
d)Object
e)Event
f)Throwable

Ans A

170) which is true about the following code
1) class Foo extends Frame {
2) Foo() {
3) Panel p=new Panel();
4) p.add(new Button("One"));
5) add("North",p);
6) add("South",new Button("Two"));
7) pack();
8) setVisible(true);
9) }
10) public static void main(String args[]) {
11) Foo f=new Foo();
12) }
13) }

a)when the frame is resized both button's size is affected
b)when the frame is resized both button's size is unaffected
c)button "Two" get resized
d)button "Owo" get resized

Ans C

171)
1)class Foo {
2) static void main(String as[]) {
3) unsigned int a=1,b=2,c;
4) c=a+b;
5) System.out.println("c=" +c);
6) }
7) }

a)compiler error
b)clean compile buy exception at runtime
c)will print 3

Ans A

172)
you want to check whether a directory exists in 'c:\' named 'sam'

a) File f=new File("\\","sam")
RandomAccessFile raf=new RandomAccessFile(f)
if(raf.Directory())
System.out.println("True");
b) File f=new File("\\","sam")
FileOutputStream fos=new FileOutputStream(f)
if(fos.Directory())
System.out.println("True");
c) File f=new File("\\","sam")
if(f.exists())
if(fos.Directory())
System.out.println("True");

Ans C

173)
Boolean b[]=new Boolean[10]
if(b[1])
System.out.println("true");
else
System.out.println("false");

a)copile error
b)clean compile buy exception at runtime
c)will print "true"
d)will print "false"

Ans A

174)
byte b=127;
byte c=126;
byte a=c-b;

a)compile error
b)clean compile buy exception at runtime
c)clean compile and run
Ans A


175)
wht will happen if you try to compile and run the following code
1)class Father {
2) int var1=10;
3) void amethod() {
4) System.out.println("father method ");
5) }
6)}
7) class Son extends Father {
8) int var1=20;
9) void amethod() {
10) System.out.println("father is over ridden ");
11) }
12) void bmethod() {
13) System.out.println("son's method ");
14) }
15) public static void main(String as[]) {
16) Father f=new Son();
17) System.out.println(f.var1);
18) f.bmethod();
19) f.amethod();
20) }
21) }

a)compile error
b)clean compile but may throw exception
c)will print 10,son's method,father is overidden
d)will print 20,son's method,father is overidden
e)will print 10,son's method,father method

Ans A

176)
RandomAccessFile raf=new RandomAccessFile("sam","rw");
raf.close();

a)compile error
b)clean compile but may throw exception if a file names 'sam' not
there
c)a file of 0 length is created
d)if a file already exists then no change is occured

Ans C

#177
class Foo {

4 public static void main(String args[]) {
5 Object o=new Object();
6 Object oa[]=new Object[1];
7 o=4;
8 oa[0]=4;
9 o=null;
10 oa=null;
11 return oa[];
}
};
At which line garbage collector will invoke
a)after line 8
b)after line 9
c)after line 10
d)will never invoke
Ans b

#178
class Foo {
public static void main(String args[]) {
int i=10;
int j=0;
tp:for(;;i--){
for(;;j++){
if(i>j) break tp;
}
}
System.out.println(i+" "+j);
}
};
What will be the output ?
a)10,0
b)10,1
c)10,-1
d)9,-1

Ans A

#179
class Foo{
public int getLength(int a,int b,float c){
for(int i=0;i<100;i++){
if(i==j)continue;
}
}


};
class Bar extends Foo {
public void getLength(int a,int b,float c){
for(int i=0;i<100;i++){
if(i==j)continue;
}
};
What will be result in compiling an running ?
a)0 to 99
b)0 to 100
c)Exception
d)compile time error

Ans D

#180
class Foo{
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();
System.out.println (sb1+" "+sb2); };
what will be the output?
//options given for the above program were confusing .
// Here you are having three chances
// main will finish before starting threads
//main will finish middle of one thread
// main will finish after one thread
//main will finish in middle of the second thread
//main will finish after the second thread
//##important thing is we can't predict which thread is going to start
first


#181
class Foo{
public static int main(String args[]) {
int i=0;
int j=0;
tp:for(;i=i+1;){
for(;;j--){
if(i>j) break tp;
}
}
System.out.println(i+" "+j);
}
};
What will be the output
a)0,0
b)0,1
c)0,-1
d)1,0
e)compile time error
d)Exception

Ans E

#182
class Foo{
public static void main(String args[]) {
public void aMethod(){
int x=4;
int y=2;
int a=2;
if(a=(x/y)){
System.out.println("x :"+x+"y :"+y);
}
}
};
What will be the output ?(options will be given)

Ans Compilation Error

#183
class X implements Runnable {
public static void main(String args[]) {
//what should be inserted here ?
}
}
which of the following line of code is suitable to start a thread
a)Thread t=new Thread(X);
b)Thread t=new Thread(X);t.start();
c)X run=new X();Thread t=new Thread(run);
d)Thread t=new Thread();
x.run();

Ans C


#184
class Foo {
public static void main(String args[]) {
try{
return;
}
finally{
System.out.println("finally");
}
}
};
a)nothing will be printed.
b)finally will be printed .
c)an exception is thrown .
d)Runtime exception is thrown .
Ans B

#185
What is true about anonymous inner classes ?
a)it can implement more than one interfaces
b)it can implement only an interface
c) it can only extend an abstract class
d) it can extend any class
E) it can implement extend a class and implement an interface.
Ans D


#186
What is true about static class ?
a)static class need no instance to access methods or variables
b)static class need an instance to access methods or variables
C)it is not possible to decalre first level class as static
#187
class Foo {
public static void main(String args[]) {
int i=0;
int j=0;
for:for(;;i++){
for(;;j--){
if(i>j)
break for;

}
}
System.out.println(i+" "+j);
}
};
What will be the output ?
a)0,0
b)0,1
c)0,-1
d)1,0
E)Compilation error
f)Exception
Ans E

#188
Which one of the following is used to read ascii codes from a file ?
a)FileReader
b)InputStream
c)characteArrayReader
d)InputStreamReader
e)OutputStreamReader
Ans D

#189
Which one of the following methods will not allow to override
a)public void aMehod(int a);
b)final void aMethod(){ };
c)static final aMethod(){ };
d)final abstract void aMethod(){ };
c)public void final aMethod(){ };

Ans B

#190
Which one of the following will be equal results ?
a)3*2
b)3*8
c)3<<3
d)3*2^3
e)3<<<2

Ans BC

#191
Which one of the following is used to start a Thread ?
options will be given

Ans: Start();


#192
Which of the following are true about static inner classes ?
a)can acces all method variables in the enclosing scope
b)can acces all instance variables in the enclosing scope
c)can access all static variables in the enclosing scope
Ans C

193
1) class hai{
public static void main(int c) {
Integer a[]=new Integer[4];
int I= 1;
System.out.println(a[I]); }
}}

What will be printed if you will execute using java hai?
Exception
Will print true
Will print false
Will print null

Ans Exception no method found main

#194
class Super {};
class Foo extends Super {
public static void aMethod(Foo o){
Super a=o;
System.out.println("Happy");
}
public static void main(String args[]) {]
Foo f=new Foo();
aMethod(f);
}
};
a)compilation error
b)class cast exception
c)clean compile and execution
d)clean compile, runtime error
Ans C


195. which of the following is valid?
public final hai();
public static final abstract void hai();
final public void hai();
public void final hai();

Ans CD

196 What is true about anonymous inner class select 3
It can implement only one interface
it can implement multiple interfaces
it should be declared inside the method
In some cases it can call constructor with parameters

Ans ACD

197. What is a feature of encapsulation
Direct access to variables
Methods are public
Methods are protected
D. no direct access to variables
Ans D

198. Which is/are the correct form of overriding for the method
public int Sree()
public void Sree()
protected int Sree()
c. public int Sree()
final int Sree()
public abstract int Sree()

Ans C
199. which are correct form of overloading for the method public int
Sree()
public void Sree()
B. int Sree(int a,int b)
C. public int Sree(int a)
D. void Sree(int d, String s)
Ans BCD
200. Predict the output
public class test {
public static void main(String args[]) {
int i=0;
int j=0;

outer: for(i = 0; i < 3; i++) {
for(j = 3; j >= 0; j--) {
if(i == j)
break outer;
}
}
System.out.println(i + " " + j);

}

Ans 0 0


201. 1.public static void main(String args[]){
2. byte b=127;
3. char c=126;
4. char d=b-c;
5. System.out.println(d);
6.}

a) Compilation succeeds and prints 1.
b) Compilation succeeds but there will a Runtime Exception.
c) Compilation error at line no 4.
d) Compilation error at line no 5.
Ans C


202. Which is the valid identifier?

a) 1_1d
b) _h_
c) @exam
d) ^java

Ans B

203. Which two values are equal?

a) 12<<2
b) 12/3*2
c) 12>>2
d) 12<<<2
e) 12>>>2
f) 12/2

Ans CE

204. What is not true about anonymous inner class?

a) Anonymous inner class can not be declared as public.
b) Anonymous inner class can not be declared as private.
c) Anonymous inner class can not be declared as static.
d) Anonymous inner class can not extend an abstract class.
e) Anonymous inner class can not be declared as protected.

Ans D

205. Which of the following will return an long value?

a) Math.max(double)
b) Math.min(double)
c) Math.ceil(double)
d) Math.floor(double)
e) Math.round(double)
Ans E

206. 1.import java.io.IOException;
2.class Happy {
3. public static void main(String args[]) {
4. try {
5. method1();
6. }
7. catch(IOException e) {
8. System.out.println("Catch IOException");
9. }
10. catch(ArithmeticException e) {
11. System.out.println("Catch ArithmeticException");
12. }
13. finally {
14. System.out.println("Catch Finally");
15. }
16. }
17.
18. public void method1() throws IOException{
19. throw new IOException();
20. }
21.}

a) Compilation succeeds and prints "Catch IOException" and "Catch
Finally".
b) Compilation succeeds but there will be a run time exception.
c) Compilation error at line no.10
d) Compilation error at line no.5
e) Compilation error at line no.19

Ans D

207. Which method is used to construct and execute the thread?

a) execute()
b) run()
c) build()
d) start()

Ans D

208. import java.io.EOFException;
class Happy {
public static void main(String args[]) {
try {
//code which throws an EOFException
}
catch(EOFException e) {
System.out.println("Catch EOFException");
}
catch(IOException e) {
System.out.println("Catch IOException");
}
}
}

a) Compilation error complaining about class IOExcepion have not been
imported.
b) Clean compilation but run time Exception is thrown saying that
IOExceptions is never thrown
c) Execute fine.

Ans C

209. switch(s) {
default: System.out.println("Best Wishes");
}
Which datatype can 's' be?

a) char
b) long
c) float
d) double

Ans A

210. public static void main(String args[]) {
int i=1;
int j=10;
do{
if(i>j)
continue;
j --;
} while(++i<6);
System.out.println("i= "+i+" j= "+j);
}
What will be the output?

a) i=4 , j=5
b) i=5 , j=6
c) i=5 , j=5
d) i=4 , j=6
e) i=6 , j=5
Ans E

211. 1.public static void main(String args[]) {
2. Object o=new Float(12.4f);
3. Object []oa=new Object[1];
4. oa[0]=o;
5. o=null;
6. oa[0]=null;
7. return o;
8.}
Where will be the garbage collecter may invoke?

a) just after line no.5
b) just after line no.6
c) just after line no.7
d) never invokes
e) compilation error
Ans B

212. Which will be used to resize only the horizontal length?

a) FlowLayout
b) GridBagLayout
c) BorderLayout
d) GridLayout
e) both b and d
f) both c and b

Ans C

213. Which of the following are valid declarations? (Select two)
a) char c= ' \' '
b) char c= "cafe"
c) char c= '\ucafe'
d) char c= '\u01001'
e) char c= '0x001'

Ans AC

214. Which of the following will not directly cause a thread to stop
?(Select two)
a) notify()
b) wait()
c) exits synchronized code
d) setPriority
e) InputSteam access
f) sleep()
Ans AC

215. What is the output?
class Happy {
public static void main(String args[]) {
default: for(int i=0;i<10;i++) {
mid: for(int j=0;j<5;j++) {
System.out.println(j);
}
}
}
}

Ans Compilation Error default is a keyword


216. What is the value of char ?
a) -32768 - 32765
b) 0 - 65536
c) 0 - 32765
d) -65536 - 65535
e) 0 - 65535

Ans E

217. public class Happy {
public static void main(String args[]) {
int i=4;
int j=2;
methodA(i,j);
System.out.println(i);
}
static public void methodA(int i,int j) {
i<<=j;
}
}
What will be the output ?
a) compilation error
b) 16
c) 64
d) 4
f) runtime exception

Ans A

218. class Happy {
public static returntype methodA(double d) {
return (long)d;
}
public static void main(String args[]) {
methodA(4.426);
}
}
What should be the returntype in the methodA declaration?

a) void
b) int
c) long
d) double
e) nothing needed
Ans C

219. Which of the following are true of anonymous inner class?

a) Anonymous inner class can be declared as private , protected or
public.
b) Anonymous inner class can implement multiple interfaces.
c) if not extended or implemented, an anonymous inner class can
become immediate subclass of the outer class or implement an interface.
d) if not extended or implemented, an anonymous inner class can
become immediate subclass of outer class or implement multiple interfaces.
Ans C


220. Which of the following are true about encapsulation ? (Select two)
a) Direct access to variables.
b) Access to variables through methods.
c) keeping methods protected.
d) use methods to access and modify the variable data.

Ans BD

221. 1.class Happy {
2. public static void main(String args[]) {
3. public class Sad {
4. public void methodA() {
5. System.out.println("inside sad");
6. }
7. }
8. System.out.println("inside happy");
9. }
10.}
What will be the output ?
a) compilation error at line no.3
b) compilation error at line no.4
c) compilation error at line no.5
d) compilation succeeds but runtime exception
e) clean compile and give the output "inside happy"

Ans A // if there is no public at line 3 then clean compile and
execution


222. 1.class Happy {
2. public static void main(String args[]) {
3. int x=4;
4. int y=2;
5. if(x/y==2) {
6. System.out.println(x+y);
7. }
8. }
9.}
What is the output?
a) prints 6
b) compilation error at line no.6
c) compilation error at line no.5
d) runtime exception occurs
e) prints 4
Ans A

223. How will you specify the access modifier for a method which
should be
accessible only to the class in which it is defined?

a) protected
b) static
c) private
d) default
e) final
f) abstract
Ans C

224. class Happy {
public static int main(String args[]) {
Happy h=new Happy();
methodA();
}
public void methodA() {
System.out.println("Best Wishes");
}
}
What will the output ?

a)completime error
b)runtime excption
c)clean compile and output "Best Wishes"

Ans A

225. class Happy {
class Sad extends Exception {}
public void methodA() throws Sad {
System.out.println("Best Wishes");
}
public void methodB() // what exception is to be thrown {
methodA();
}
}

a) Exception
b) catch(Exception e)
c) Throwable
d) no code necessary
e) comple time error

Ans A

226 Which type of the event is raised when enter key is pressed on
java.awt.Component?

a) ClickEvent
b) ActionEvent
c) KeyEvent
d) ItemEvent
e) MouseEvent

Ans C

227. What is true about constructors ? (Select two)

a) They initialize all the member variables
b) They initialize all the instance variables
c) If there is no constructor , the compiler includes default
constructor.
Compiler supplied constructor will invoke the default constructor of
the
Super class.

Ans CD

228. class Happy {
public static void main(String args[]) {
float [] [] f1={ {1.2f,2.3f},{4.5f,5.6f}};
Object oo=f1;
f1[1] =oo;
System.out.println("Best Wishes "+f1[1] );
}
}
What will the output ?

a) {4.5,5.6}
b) 4.5
c) compilation error in line no.5
d) exception
Ans C


229. class Happy {
static StringBuffer sb1=new StringBuffer("A");
static StringBuffer sb2=new StringBuffer("B");
public static void main(String args[]) {
Happy h=new Happy();
h.methodA(sb1,sb2);
System.out.println(sb1+" , "+sb2);
}
public void methodA(StringBuffer x,StringBuffer y) {
y.append(x);
x=y;
}
}
What will the output ?

a) BA,BA
b) A,AB
c) BA,AB
d) A,BA
e) A,A
f) compilation error

Ans C


230. class Happy {
String s="Hello";
public static void main(String args[]) {
Happy h=new Happy();
h.methodA(s);
System.out.println(h.s);
}
public void methodA(String s) {
s.replace('a','e');
s+=" World !!!" ;
}
}
What will the output ?

a) compile error because string is not static.
b) prints "Hello World !!!"
c) prints "Hello"
d) prints " World !!!"
e) Runtime exception occurs
Ans A


231. class Happy {
public int getLength() {
System.out.println("int version");
}
}
class Life extends Happy {
public long getLength() {
System.out.println("long version");
}
public static void main(String args[]) {
Happy e=new Life();
e.getLength();
}
}
Which method gets executed?
a) int version
b) long version
c) compile time error
d) run time exception
e) clean compile but no output

Ans C


232. class Happy {
public static void main(String args[]) {
int i=1;
int j=i++;
if ((i == (++j) ) & ( (i++) = = j ) {
i+=j;
}
}
}
What will the value of i after this code execution ?

a) 4
b) 5
c) 2
d) 3
e) compilation error

Ans B


233. Which of the following will directly stop the execution of a
Thread? (Choose two)

a) wait()
b) notify()
c) notifyall()
d) exits synchronized code
e) setPriority

Ans D


234. class Happy extends Frame {
Happy() {
Panel p = new Panel();
Button b1 = new Button(揘orth?;
p.add(b1);
add (p,BorderLayout.NORTH);
Button b2 = new Button(揝OUTH?;
add(b2,BorderLayout.SOUTH);
setSize(300,300);
setVisible(true);
}
public static void main(String args[]) {
Happy h = new Happy();
h.pack();
}
}

a)
b)
c)
d) Runtime Exception
e) Compilation Error

Ans :the North button will be in north but it will not occupy the full
place the secon button in south will occupy full place



235. class Happy extends Thread {
final StringBuffer sb1 = new StringBuffer();
final StringBuffer sb2 = new StringBuffer();
public static void main(String args[]) {
final Happy h=new Happy();
new Thread() {
public void run()throws Exception {
synchronized(this) {
h.sb1.append(揂?;
h.sb2.append(揃?;
System.out.println(h.sb1);
System.out.println(h.sb2);
}
}
}.start();
new Thread() {
public void run() {
synchronized(this) {
h.sb1.append(揇?;
h.sb2.append(揅?;
System.out.println(h.sb2);
System.out.println(h.sb1);
}
}
}.start();
}
}
What may be the output of this code ?(Choose two)

a) ABBCAD
b) ABCBCAD
c) CDADACB
d) CDDACB
e) Output non-deterministic because of the chance of 慸eadlock?
f) Output determined due to the underlying platform.

Ans BC //out put is non deterministic because here threads are locking
different objects


####Same type of question with little change

class Happy extends Thread {
final StringBuffer sb1 = new StringBuffer();
final StringBuffer sb2 = new StringBuffer();
public static void main(String args[]) {
final Happy h=new Happy();
new Thread() {
public void run()throws Exception {
synchronized(h.sb1) {
h.sb1.append(揂?;
h.sb2.append(揃?;
System.out.println(h.sb1);
System.out.println(h.sb2);
}
}
}.start();
new Thread() {
public void run() {
synchronized(h.sb1) {
h.sb1.append(揇?;
h.sb2.append(揅?;
System.out.println(h.sb2);
System.out.println(h.sb1);
}
}
}.start();
}
}
What may be the output of this code ?(Choose two)

a) ABBCAD
b) ABCBCAD
c) CDADACB
d) CDDACB
e) Output non-deterministic because of the chance of 慸eadlock?
f) Output determined due to the underlying platform.


Ans A,D

236. class Happy {
static String str =擧ello?
Public static void main(String args[]) {
String s = new String();
s.change(str);
System..out.println(str);
}
public void change(String s) {
s +=?World?
}
}
a) prints Hello World
b) prints Hello
c) compilation error because change is not static
d) runtime exception
Ans C


237. A method is to be accessed in same package without instances of
the class.
a) public void method1();
b) protected void method1();
c) static void method1();
d) void method1();
Ans C

238. Which is/are the valid identifiers ?
a) 2int
b) name-lst
c) _123
d) 1dir_list
e) second-1
Ans C

239. Classone.java
1.
2.package cmd.abc.pkg1;
3.public class Classone {
4. char getVar() {
5. System.out.println(揌ello?;
6. return 慳?
7. }
8.}

Classtest.java

1.package cmd.abd.pkg2;
2.import cmd.abc.pkg1.Classone;
3.class Classtest extends Classone {
4. public static void main(String args[]) {
5. char a=(new Classone()).getVar();
6. char b=(new Classtest()).getVar();
7. System.out.println(揌ello?;
8. }
9.}
a) Compilation error at line no. 6 in Classone.java
b) Compilation error at line no. 5 in Classtest.java
c) Compilation error at line no. 6 in Classtest.java
d) Compilation succeeds but there is a Runtime exception due to
instance of the class can not be assigned to a char datatype.

Ans C //because getvar is declared as friend

240. 1. abstract class Happy {
2. abstract float getFloat();
3.}
4.class Life extends Happy {
5. private float getFloat(){}
6. public static void main(String args[]) {
7. System.out.println(揃est Wishes?;
8. }
9.}
a) Compilation errot at line no.4
b) Compilation errot at line no.5
c) Compilation errot at line no.2
d) Compilation succeeds but runtime exception occurs.
e) Clean compilatin and executes finely.

Ans B //cannot be overriden as private

241. Which is the true statement ?

a) Errors are subclasses of RuntimeExceptions.
b) No Exceptions are subclasses of Errors.
c) Errors should be catched and handled.
d) RuntimeExceptions cannot be catched and handled.

Ans D

242. class Happy {
protected void setVal(float f) {
// some code
}
}
class Life extends Happy {
// Which overriding methods can be succeessfully placed here ?
}(Select two)

a) protected float setVal(float f) { 厎
b) public void setVal(float f) { 厎
c) protected void setVal(float f,int i) { 厎
d) protected final void setVal(float f) { 厎
e) public float setVal(float f) { 厎

Ans B,D

243.
class Happy {
public byte methodA() {
return 4;
}
}
class Life extends Happy {
public short methodA() {
return 2;
}
public static void main(String args[]) {
short s;
Life a = new Life();
s = a.methodA();
System.out.println(s);
}
}
What is the output ?
a) prints 2.
b) prints 4.
c) prints 0.
d) Compilation error.
e) Runtime exception.

Ans D

244.
An Interface is an interface.
An Adapter0 is a class with zero argument constructor.
AnAdapter1 is a class without zero argument constructor but with an int
argument constructor.
Which is the valid declaration?

a) AnAdapter0 aa=new AnAdapter0();
AnAdapter1 bb=new AnAdapter1();

b) AnAdapter0 aa=new AnAdapter0();
AnAdapter1 bb=new AnInterface();

c) AnAdapter1 aa=new AnAdapter1(5);
AnInterface bb=new AnInterface();

d) AnAdapter0 aa=new AnAdapter0();
AnAdapter1 bb=new AnAdapter1(5);


Ans D

245.
public abstract class Happy {
abstract void methodA();
abstract void methodB() {
System.out.println(揃est Wishes?;
}
}
Which three changes (made independently) allow the code to compile
succeessfully ?
a) Remove the 慳bstract?keyword from methodB.
b) replace ??with { 厎 in methodA .
c) remove the methodB().
d) replace the methodB definition (ie, {厎) with ?攁nd Replace the
class declaration with interface declaration in line no.1.
e) No need to do anyting.
f) Remove the 慳bstract?keyword from line no.1.
Ans ACD
246. Which of the following two declarations used to read a file
called 慣est.txt?
a) RandomAcceesFile raf=new RandomAcceesFile(揟est.txt?;
b) InputStream is = new FileInputStream(揟est.txt?;
c) InputStream is = new
DataInputStream(FileInputStream(揟est.txt?true));
d) FileInputStream fis = new FileInputStream(new File(揟est.txt?);
e) FileoutputStream fos = new FileoutputStream(new File(揟est.txt?);
f) OutputStream os = new FileoutputStream(new File(揟est.txt?false));

Ans BD

247. Import java.io.*;
class Happy {
public static void main(String args[]) {
try {
File f = new File(揟est.txt?;
OutputStream o=new FileOutputStream(f);
}
catch(IOException e) { }
}
}
What happens when this code compiles and run?
a) Compilation error.
b) The code compiles and runs successfully but no file is created.
c) A file with zero length is created.
d) The code compiles but there will be a RuntimeException because there
is no matching constructor for the FileInputStream.
Ans C

248. class Happy extends Thread {
final static StringBuffer sb1 = new StringBuffer();
final static StringBuffer sb2 = new StringBuffer();
public static void main(String args[]) {
new Thread() {
public void run() {
synchronized(sb1) {
sb1.append(揂?;
sb2.append(揃?;
System.out.println(sb1);
System.out.println(sb2);
}
}
}.start();
new Thread() {
public void run() {
synchronized(sb2) {
sb1.append(揇?;
sb2.append(揅?;
System.out.println(sb2);
System.out.println(sb1);
}
}
}.start();
}
}
What may be the output of this code ?(Choose two)
a) ABBCAD
b) ABCBAD
c) CDDACB
d) Output non-deterministic because of the chance of 慸eadlock?
e) Output determined due to the underlying platform.

Ans AD
249. class Happy {
public static void main(String args[]) {
int i=3;
int j=5;
if(i++ > (--j)) & ((++j) < 10)
j+=5;
System.out.println (搄 is ?j+? i is ?i);
}
}
What will the output?
a) j is 10 and i is 4
b) j is 5 and i is 4
c) j is 10 and i is 3
d) j is 5 and i is 3
g) compilation error
h) runtime exception

Ans B


250. class Happy {
public static void main(String args[]) {
float f=4.2f;
Float g=new Float(4.2f);
Double d=new Double(4.2);
}
}
Which will return true (or will not show any compilation error or
RuntimeException)?
(Select two)

a) f==g
b) g==g
c) g.equals(f)
d) d.equals(d)
e) d.equals(f)
Ans BD

251. class Happy implements Runnable {
private int x;
private int y;
public void run() {
// what code should be placed here ?
}
public static void main(String args[]) {
Happy h = new Happy();
new Thread(h).start();
new Thread(h).start();
}
public void setX(int i) {
x = i;
}
public void setY(int j) {
y = j;
}
synchronized public void setXY(int i) {
setX(i);
setY(i);
}
synchronized public boolean check() {
return x != y;
}
}

a) check() never returns true.
b) multiple threads accessing setX(),setY() can result in check()
returning true.
c) multiple threads accessing setXY() can result in check()
returning true.
d) compilation error because there is no start() .

Ans B

252. How to append to file 揟est.txt? (Select two)
a) FileOutputStream fis = new FileOutputStream ( 揟est.txt? true);
b) OutputStream os = new FileOutputStream ( 揟est.txt? 揳ppend?;
c) FileOutputStream fis = new FileOutputStream ( 揟est.txt? 搕rue?;
d) FileOutputStream fis = new FileOutputStream (new File( 揟est.txt?);
e) OutputStream os = new OutputStream (new File( 揟est.txt?, true);

Ans AD

253. public class test {
public static void main(String args[]) {
outer1: for(int k = 0; k < 3; k++) {
outer2: for(int i = 0; i < 3; i++) {
for(int j = 3; j >= 0; j--) {
if(i >j)
System.out.println(i + " " + j);
break outer1;
}
}
}
}

Ans 1 0

254.
class Super
{
int i=0;
Super()
{
i=20;
}
void hello()
{
System.out.println("Afsal");
}
class Sub extends Super
{
int i =30;
void hello()
{
System.out.println("Anson");
}
Sub(String s)
{ this.i=40;
super.i=10;}
public static void main(String args[])
{
Super b=new Sub("Anil");
System.out.println(b.i);
b.hello();
}

What will be printed? Select all correct ans
A. Compilation Error
B. Runtime Error
10
20
30
40
Anson
Afsal

Ans 10 Anson


255. public static void main(String args[])
{
String s="Java";
Stringbuffer s=new StringBuffer("Java");
Change(s);
Change(sb);
System.out.println(s+sb);
}
public static void (String s)
{
s="hello";
}
public static void Change(StringBuffer sb)
{
sb=new StringBuffer("hello");
}
what will be printed?

Ans JavaJava
256.
public static void main(String args[])
{
System.out.println(args[1]);
}
What will be printed if you execute the program using java Program red
green blue

Ans Green


257.
public static void main(String args[])
{
String s="Java";
String s2=s;
s2 +="Sun";
s.concat("Java");
System.out.println(s2 + s);
}
Write the answer?
Ans javaSunjava


258. What happens when the following program is compiled and run.
Select the one correct answer.

public class example {
public static void main(String args[]) {
int x= 2;
int y=2
change(x,y);
System.out.println(x+" "+y);
}
public static change(int i,int j) {
i = 4;
j=4;
}
}
The program does not compile.
B. The program prints 2 2.
The program prints 4 4.
The program prints 4.
The program prints 8.
Ans B


260. Which will stops execution of the a thread? Select one
execute wait()
changing priority value
call terminate method from another thread

Ans A

261. Which of the following is the is a subclass of Throwable?
Thread
Object
Exception
Throw

Ans Thread

262. Which of the following Listener is to handle "MouseMotionEvent"
MouseMotionListener
MouseListener
MotionListener
ActionListener

Ans A

263. class Super
{ int i=0;
Super(String s)
{
i=10;
}
}
class Sub extends Super
{
Sub(String s)
{ i=20; }
public static void main(String args[])
{
Sub b=new Sub("hello");
System.out.println(i);
}

What will be the value of i.?
A. Compilation Error
B. Runtime Error
C. 0
D. 10
E. 20

Ans A

264.which of the following is the correct way(s) to open
RandomAccessFile for append to a file using seek() method
File f=new File(C:\\hello.txt");
new RandomAccessFile(f,"rw");
new RandomAccessFile(f,"r");
new RandomAccessFile(f,"w");
new RandomAccessFile(f,"a");
Ans A


265. Which of the following will give equal results?
A. 6/2 B. 6>>2 C. 6*4 D.
6>>>2

Ans BD

266.. Identify two classes where objects are placed and retrieved by
key value
HashTable
HashSet
HashMap
Map

Ans AC

267. Which of the following is the most restrictive modifier for class
variable to be accessible for all the classes in the same package as
well as in subclasses?
public
protected
default
private

Ans B

268.
1. String s="hello";
2. String s=null;
3. System.gc();
Is Garbage Collector will execute at line 3?
Garbage collector will surely execute
Garbage collector will not execute
Garbage collector may execute

Ans C

269. What will be the result of the following code
class s1 extends Thread{
public void run()
{ for(int i=0;i<3;i++)
{
System.out.println("A");
System.out.println("B");
}
}
}
class s2 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();
s2 t2=new s2();
t1.start();
t2.start();
}
}
A) Compile time Error There is no start method
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厖
Ans C

271.
You have the following code. Which numbers will cause "Test3" to be
printed?
switch(x){
case 1: System.out.println("Test1");
case 2: System.out.println("Test2");
case 4: System.out.println("Test4");
case 3: System.out.println("Test3");
break;
}
}

A. 0
B. 1
C. 2
D. 3
E. 4

Ans BCDE

272.
class a{
public static void main(String ar[])
{ int i=10;
while: for( i=1;i<10;i++)
if(i==5)
continue while;
System.out.println(i);
}
}
Write the output?

Ans Compilation Error

273.
class a{
static String s;
int i;
public static void main(String ar[])
{ int i;
String s="10";
System.out.println("string is"+s);
}

What will be the out put?

Ans string is 10

274. Which of the following method is to handle MouseMotionEvent
processMouseMotionEvent()
processMouseEvent()
processMotionEvent()
processEvent()

Ans A

275. A --------is a collection which can contain duplicate elements
and has an explicit order to its elements
Set
Map
Collection
List
Ans List

276.
class a{
static String s;
int i;
public static void main(String ar[])
{
System.out.println("string is"+s);
}
What will be the out put?

277.
public static void main(String ar[])
{
int j=10;
method(j);
amethod(j);
System.out.println(j);
}
public static method(int j)
{
j++;
}
public static amethod(int j)
{
j++;
}
}
What will be the result?


278. What is the range of byte in values?
-128 to +127



279. class s implements Runnable{
int x,y;
public void run()
{
for(;;)
synchronized(this)
{
x=12;
y=12;
}
System.out.println(x+" "+y);
}
public static void main(String args[])
{
s run=new s();
Thread t1=new Thread(run);
Thread t2=new Thread(run);
t1.start();
t2.start();
}
}
What will be the result?
A. DeadLock
B. Execute without any problems and print 12 12
C. Compilation Error

Ans B //only one thread will be printing other thread will wait


280. Which of the following is used to find max
max()
Math.max();
None of the above

Ans C

PART 2

Q1.What is the output when following code is run ?.

1. class ForLoop{
2. public static void main(String args[]){
3. int i=0,j=5;
4. st: for(;;i++){
5. for(;;j--){
6. if(i>j)
7. break st;
8. }
9. }
10. System.out.println("i="+i+" j="+j);
11. }
12. }

1. i=1 j=-1
2. i=1 j=0
3. i=0 j=1
4. i=0 j=-1
5. Compiler error at line 4.


Q2.What is the output when folllowing code is run?

1. class OuterOne{
2. class InnerOne extends OuterOne{}
3. static void thisMethod(){
4. Object o=(Object)new OuterOne();
5. OuterOne foo=(OuterOne)o;
6. }
7. public static void main(String args[]){
8. thisMethod();
9. }
10. }

1. Will compile fine,but at runtime a ClassCastException is thrown
2. Will compile and run fine
3. Compiler error at line 4
4. Compiler error at line 5


Q3.public class Outer{
//////
}

What should be inserted at ///// so that the code will
compile fine?(Choose two)

1. private class Inner{
static int k;
}

2. static class Inner{
static int k;
}

3. public class Inner{
static int k;
}

4. public abstract class Inner{
public abstract void method();
}

Q4.What will be the output when following code is run?

1. class ThisClass{
2. static void foo() throws Exception{
3. throw new Exception();
4. }
5. public static void main(String args[]){
6. try{
7. foo();
8. }catch(Exception e){
9. System.exit(0);
10. }
11. finally{
12. System.out.println("In finally");
13. }
14. }
15. }

1. Compiler error.
2. Code will compile fine and when run prints In finally.
3. Nothing will be printed
4. Runtime exception.


Q5.import java.awt.*;
class X extends Frame{
X(){
setLayout(new BorderLayout());
setSize(600,600);
Panel p=new Panel();
Button b1=new Button("Button1");
p.add(b1);
Button b2=new Button("Button2");
add(p,BorderLayout.NORTH);
add(b2,BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String args[]){
new X();
}
}

How the components are displayed on the frame?

1. Components will be of equal size.

2. Button1 will be placed at top center and Button2 will occupy rest of the area.

3. Button2 will be placed at top center of the frame and Button1 will be placed at bottom center of the frame.

4. Button2 will be placed at top center of the frame and Button1 will be placed at bottom of the frame from left end to right end.

5. Button1 will be placed at top center of the frame and Button2 will be placed at bottom of the frame from left end to right end.



Q6.File f=new File("aa.txt");
Suppose this file exists in your directory.
Which of the following can be used to write a line of
text to the end of this file?

1. RandomAccessFile f1=new RandomAccessFile(f,"r");
2. RandomAccessFile f1=new RandomAccessFile(f,"a");
3. RandomAccessFile f1=new RandomAccessFile(f,"rw");
4. RandomAccessFile f1=new RandomAccessFile(f,"w");
5. RandomAccessFile f1=new RandomAccessFile(f,"wr");


Q7.What is the value of j when printed?

class Foo{
public static void main(String args[]){
int x=4,j=0;
switch(x){
case 1:j++;
case 2:j++;
case 3:j++;
case 4:j++;
case 5:j++;
break;
default:j++;
}
System.out.println(j);
}
}

1. 1
2. Compiler error
3. 6
4. 3
5. 2

Q7.What will be the content of baz after the the
following code is run?

class A{
public static void main(String args[]){
String bar=new String("blue");
String baz=new String("green");
String var=new String("red");
String c=baz;
baz=var;
bar=c;
baz=bar;
System.out.println(baz);
}
}

1. red
2. Compiler error
3. blue
4. null
5. green


Q8.import java.io.*;
class Excep{
static void method() throws Exception{
throw new EOFException();
}

public static void main(String args[]){
try{
method();
}catch(////){}
}
}

Which of the following should be placed at //// so that
the code will
compile without
errors?

1. IOException e
2. EOFException e
3. MalformedURLException e
4. NullPointerException e
5. Exception e
6. FileNotFoundException

Q9. What will be the output when the following code is
run?
1. class ThisClass{
2. public static void main(String args[]){
3. Object o=(Object)new ThisClass();
4. Object s=new Object();
5. if(o.equals(s))
6. System.out.println("true");
7. }
8. }

1. Prints true
2. Nothing will be printed
3. Compiler error at line 3
4. Compiler error at line 5
5. ClassCastException will be thrown

Q10.Which of the following interfaces that ArrayList directly implements?

1. Set
2. Map
3. SortedSet
4. List
5. Collection

Q11.What will be the output when following code runs?

1. class C1{
2. static int j=0;
3. public void method(int a){
4. j++;
5. }
6. }
7. class Test extends C1{
8. public int method(){
9. return j++;
10. }
11.
12. public void result(){
13. method(j);
14. System.out.println(j+method());
15. }
16. public static void main(String args[]){
17. new Test().result();
18. }
19. }

1. 3
2. 2
3. Compiler error at line 8
4. 1
5. 0

Q12.What will be printed when following code runs?

1. class Foo{
2. static void change(String s){
3. s=s.replace('j','l');
4. }
5.
6. public static void main(String args[]){
7. String s="java";
8. change(s);
9. System.out.println(s);
10. }
11. }

1. Compiler error
2. Will print lava
3. Runtime exception
4. Will print java

13. class Happy {
public static void main(String args[]) {
int index=1;
int a[] =new int[3];
int bas =a[index];
int baz =bas + index
System.out.println( a[baz] );
}
}
What will the output?
a) Compilation error
b) Runtime Exception
c) 1
d) 0
e) 4
f) 3
14. 1.interface I {
2. int i=10;
3.}
4.class Happy implements I {
5. public static void main(String args[]) {
6. Happy h=new Happy();
7. int j;
8. j = I.i;
9. j = Happy.i;
10. j = h.i;
11. }
12.}
What will happen if you try to compile and run this code ?
a) Compilation error at line no.2
b) Compilation error at line no.8
c) Compilation error at line no.9
d) Compilation error at line no.10
e) Clean compile.
f) RuntimeException.
15. class Happy implements Runnable{
private int x;
private int y;
public synchronized void run() {
x++;
y++;
System.out.println(x+" "+y);
}
public static void main(String args[]) {
Happy that=new Happy();
(new Thread(that)).start();
(new Thread(that)).start();
}
}
What happens when this code compiles and run?
a) will print x ,y in order 11 22
b) will print x ,y twice in order 11 22 11 22
c) will print x ,y in order 12 12
d) will print x ,y order is unpredictable.
e) Compilation error.
f) Runtime Exception.
16. class Happy {
public static void main(String args[]) {
int i =1;
int j = 10;
do {
if ( i++ < j--)
continue;
} while ( i <5 );
System.out.println ( i+" "+j );
}
}
What will be the output?
a) will print 5 5
b) will print 5 4
c) will print 6 4
d) will print 5 6
e) Compilation error.

17. How to make instance of the innerclass?
class OuterOne {
class InnerOne {
}
}
class Happy {
OuterOne o = new OuterOne();
//what code can be placed ?
}
a) InnerOne i = new Innerone();
b) InnerOne i = o. new Innerone();
c) OuterOne. InnerOne i = new o.Innerone();
d) OuterOne.InnerOne i = o.new Innerone();
e) InnerOne i = new Outerone.Innerone();

18. class OuterOne {
static class InnerOne {
}
}
What is true about innerclass ?
a) can say new InnerOne()
b) can say new OuterOne.InnerOne()
c) can say new OuterOne.new InnerOne()
d) can't create the instance of the static InnerOne.
e) class InnerOne can not be static.
19. class Happy extends Frame {
Happy() {
SetLayout(new GridLayout(2,2));
Panel p1 = new Panel();
add(p1);
p1.add( new Button("One"));
Panel p2 = new Panel();
add(p2);
p2.add( new Button("Two"));
add( new Button("Three"));
add( new Button("Four"));
setSize(300,300);
setVisible(true);
}
public static void main(String args[]) {
Happy h = new Happy();
}
}
(Select two)
a) When the frame is resized buttons Three, Four will be resized.
b) When the frame is resized all buttons will be resized.
c) When the frame is resized buttons One, Two will not be resized
d) When the frame is resized buttons Two, Four will be resized.
e) When the frame is resized buttons One, Two will be resized.
20. class Happy {
public static void main(String args[]) {
int x =2;
int y = 5;
if ( x=y )
System.out.println ( "true " );
else
System.out.println ( "false " );
}
}
What will be the output?
a) will print "true"
b) will print "false"
c) nothing will be printed
d) compilation error
e) runtime exception
21. 1.class Happy {
2. static int methodA() {
3. return 10;
4. }
5. public static void main(String args[]) {
7. while (methodA()) {
8. x++;
9. System.out.println(x);
10. }
11. }
12.}
a) compilation error at line no. 3
b) compilation error at line no. 7
c) compilation error at line no. 9
d) compilation succeeds and prints 11.
e) compilation succeeds and prints 10.
f) Runtime Exception.


22. class Happy extends Frame{
Happy() {
setLayout("GridLayout");///error
add(new Button("hello1");
add(new Button("hello2");
pack();
show();
}
public static void main(String args[]) {
Happy h=new Happy();
}
}
a) only one button will be shown with label "hello2".
b) only one button will be shown with label "hello1".
c) two buttons will be shown .
d) compilation error.
23. class Happy implements Runnable {
private int x;
private int y;
public void run() {
//some code
}
public static void main(String args[]) {
Happy h=new Happy();
new Thread(h).start();
new Thread(h).start();
}
void setX(int i) {
x=i;
}
void setY(int j) {
y=j;
}
synchronized void setXY() {
setX(1);
setY(1);
}
synchronized boolean check() {
return x!=y;
}
}
Select a true statement from the following.
a) check() never returns true.
b) multiple threads accessing setX(),setY() can result in check() returning true.
c) multiple threads accessing setXY() can result in check() returning true.
d) compilation error because there is no start() .
24. What is true about anonymous innerclass?
a) An anonymous innerclass can be declared as private.
b) An anonymous innerclass can be declared as final.
c) An innerclass can access any final variables in the enclosing scope.
d) An anonymous innerclass can implement multiple interfaces.
25. How do you make instance of a static innerclass other than the
enclosing class?
(Select Two)
a) Outer.Inner I = new Outer.Inner();
b) Outer.Inner I = new Outer().new Inner();
c) Inner I = new Outer(). new Inner();
d) Inner I = new Outer.Inner();
e) Inner I = new Inner();
26. How can you force a method to be overridden in a subclass of its
class?
a) final
b) protected
c) transient
d) volatile
e) implements
f) extends
g) abstract
27. How can you prevent the subclassing of a class?
a) final
b) protected
c) transient
d) private
e) implements
f) extends
g) abstract
28. 1.class Happy {
2. public static int oddA() {
3. int x=5;
4. int y=2;
5. return (5/2);
6. }
7. public static void main(String args[]) {
8. if(oddA())
9. System.out.println(" OK ");
10. }
11.}
What will be the output?
a) Compilation error at line no. 5 (stating that it can return only int values).
b) Compilation error at line no. 8
c) Compilation error at line no. 2 (stating that return type of the method should be double).
d) Compilation succeeds and prints " OK ".
e) Compilation succeeds and prints nothing.
f) Compilation succeeds but there will be Runtime Exception.

29. class Happy {
Happy() {
}
}
Which are the valid overloading constructors?(Select all that apply)

a) public void Happy(){}
b) public Happy(int c){}
c) protected Happy(){}
d) private Happy(){}
e) public int Happy(){}
f) void Happy(){}

30. class Happy {
static StringBuffer s = new StringBuffer("");
static void methodA(int i) {
try {
if (i ==1)
throw new Exception();
s.append("1");
}
catch(Exception e) {
s.append("2");
return;
}
finally {
s.append("3");
}
s.append("4");
}
public static void main(String args[]) {
methodA(0);
methodA(1);
}
}
What will be the value of StringBuffer s at the end of execution?

a) 134234
b) 134123
c) 13423
d) 13412
e) Compilation error.
f) RuntimeException.

31. class Happy {
public static void main(String args[]) {
int j=0xFFFFFFF1;
int s=~j;
System.out.println(s);
}
}
What will be the output?

a) -15
b) 15
c) 1
d) 14
e) -2147483637
f) Compilation error due to int value overflow.
g) RuntimeException.

32. 1.class Happy {
2. public static void main(String args[]) {
3. Happy h=new Happy();
4. h.methodA();
5. }
6. Object methodA() {
7. Object obj1=new Object();
8. Object []Obj2=new Object[1];
9. obj2[0]=obj1;
10. obj1=null;
11. return obj2[0];
12. }
13.}
Where will be the most chance of garbage collector invoked?

a) After line no.9
b) After line no.10
c) After line no.11
d) Garbage collector never invoked in this method.
e) Compilation error.

33. Which access modifier is the most restrictive modifier for a method
to be accessed
from the subclasses of the class from another package?

a) private
b) protected
c) default
d) public
e) final
f) abstract

34. Which method registers athread in a thread scheduler?

a) run()
b) construct()
c) start()
d) register()
e) schedule()

35. In a frame you add a TextField and then you add a Button and it
must placed just
under the TextField. Then you add another Button and it must be placed
bottom of the
previous Button. When the frame is resized the increased area must be
filled with the
TextField.Which Layout manager you will use?

a) GridBagLayout
b) FlowLayout
c) GridLayout
d) CardLayout
e) BorderLayout
36. What is true about HashSet?

a) Values stored in a HashSet will keep an order and it will not
allow duplicates.
b) Values stored in a HashSet will not keep an order and it will not
allow duplicates.
c) Values stored in a HashSet will keep an order and it will
allow duplicates.
d) Values stored in a HashSet will use a unique key for storing its elements.
And it will not allow duplicates

37. class Happy {
public static void main(String args[]) {
try {
return;
}
finally {
System.out.println("finally");
}
System.out.println("Happy");
}
}
What will be the output?
a) will print "finally","Happy".
b) will print "Happy".
c) will print "finally".
d) compilation error because main can't return.
38. //code
class Happy {
public static void main(String args[]) {
PrintWriter p=new PrintWriter(new
java.io.OutputStreamWriter(System.out));
}
}
Which statement can be placed in //code to make this program work?

a) import java.io.PrintWriter
b) import java.io.OutputStreamWriter
c) include java.io.PrintWriter
d) include java.io.OutputStreamWriter
e) import java.io.PrintWriter
import java.io.OutputStream
f) no need to do anything.
39. What are reserved words?
a) run
b) implement
c) import
d) default
40. class Happy implements Runnable {
synchronized void run() {
int x=0;
int y=0;
for( ; ; ) {
x++;
y++;
System.out.println(x+ " " +y);
}
}
public static void main(String arg[]) {
Happy h= new Happy();
Thread t1=new Thread(h);
Thread t2=new Thread(h);
t1.start();
t2.start();
}
}
What will be the output?
a) will print 11 22 33 .............
b) will print 12 12 34 34.............
c) may print like 12 23 13 .............
d) will result a deadlock.
e) compilation error.


40. class Happy {
static int j=0;
static boolean method1(int k) {
j+=k;
return true;
}
static void method2(int i) {
boolean b;
b=(i < 10 | method1(4));
b=(i < 10 || method1(10));

}
public static void main(String args[]) {
method2(0);
System.out.println(j);
}
}
What will be the value of j in the print statement?
a) 4
b) 10
c) 14
d) 0
e) compilation error

分享到:
评论

相关推荐

    Java 面试大合集 大汇总 各大公司面试笔试题汇总.doc SCJP题库.rar java面试100题目(X).pdf java面试笔试题 .doc

    125条常见java面试笔试题大汇总.doc 各大公司面试笔试题汇总.doc java 多线程.doc SCJP题库.rar java面试100题目(X).pdf java私塾面试题----JAVA代码查错.rar java常见错误大全.doc

    scjp面试题目(JAVA).

    scjp下载面试题目(JAVA).scjp下载面试题目(JAVA).

    java面试题

    Java SCJP面试题解析

    SCJP模拟题104道 考你的java知识 对你面试有帮助

    SCJP模拟题104道 考你的java知识 对你面试有帮助 内容涵盖整个j2ee体系 欢迎下载

    java面试题以及技巧

    │ 上海税友软件 面试题.doc │ 公司培训文档-混淆的基本概念.doc │ 基本算法.doc │ 孙卫琴精通struts.基于MVC的.java.web设计与开发.pdf │ 学习Struts提供的和Form相关标签.txt │ 日企编码规范.doc │ 电信盈科...

    java面试题集锦()

    java面试题集锦 JAVA程序员面试32问.txt java面试笔试题大汇总.doc SCJP考试题310-025原题及解答.txt 大公司的Java面试题集.doc 等等.........

    java面试题及技巧4

    │ 上海税友软件 面试题.doc │ 公司培训文档-混淆的基本概念.doc │ 基本算法.doc │ 孙卫琴精通struts.基于MVC的.java.web设计与开发.pdf │ 学习Struts提供的和Form相关标签.txt │ 日企编码规范.doc │ 电信盈科...

    SCJP真题+个人笔记

    这套面试题主要目的是对java言语有一定基础的人同时对scjp 认证考试感性趣的人发的 希望大家能共同进步

    java面试题及技巧3

    │ 上海税友软件 面试题.doc │ 公司培训文档-混淆的基本概念.doc │ 基本算法.doc │ 孙卫琴精通struts.基于MVC的.java.web设计与开发.pdf │ 学习Struts提供的和Form相关标签.txt │ 日企编码规范.doc │ 电信盈科...

    java面试题以及技巧6

    │ 上海税友软件 面试题.doc │ 公司培训文档-混淆的基本概念.doc │ 基本算法.doc │ 孙卫琴精通struts.基于MVC的.java.web设计与开发.pdf │ 学习Struts提供的和Form相关标签.txt │ 日企编码规范.doc │ 电信盈科...

    JAVA面试题

    JAVA面试题,考SCJP可能用得到

    SCJP(含模拟考试系统)

    自己考SCJP时,全靠它了,全不全,自己看看就知道了,面试的话看看这个也是非常不错的。其中还包含"SCJP模拟考试系统". 另外SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) 是一个非常好的...

    IT企业笔试面试题分析

    本套题目主要出自神州数码, 华为, 笔试考试题以及SCJP认证考题,很值得参考····

    神州数码, 华为, 笔试考试题以及SCJP认证考题

    神州数码, 华为, 笔试考试题以及SCJP认证考题

    SCJP_Guide

    SCJP考试指南,官方文档,我就是看了这个文档通过SCJP题目的,拿来与大家分享

    sun认证SCJP——大企业笔试题来源

    大企业的笔试题大部分来自这个附件,sun的认证题我只有这么多,希望有人更多,可以发给我,我的信箱se.jason@163.com

    java面试题目与技巧1

    │ 上海税友软件 面试题.doc │ 公司培训文档-混淆的基本概念.doc │ 基本算法.doc │ 孙卫琴精通struts.基于MVC的.java.web设计与开发.pdf │ 学习Struts提供的和Form相关标签.txt │ 日企编码规范.doc │ 电信盈科...

    AIC的Java课程1-6章

    AIC的学费很贵,半年18000元,大家看看他们教些什么内容吧 &lt;br&gt;他们学校的网址...  题目来源: 大纲范围内,SCJP考题,企业面试题等。  题型:填空,选择,简答,编程等。[参考,可变] &lt;br&gt;

    F:\02soft\download\JAVA基础.rar

    1.非常实用的JAVA基础学习文档。是首个以题目来对JAVA基础进行...3.题目都为选择题,基本没有概念题,都是给你一段程序或代码段,让你来进行解释。杜绝死记硬背 4.关于参数传递、垃圾回收器、继承、线程等讲的非常的好

Global site tag (gtag.js) - Google Analytics