Sie sind auf Seite 1von 146

Downloaded form http://www.laynetworks.com : solutions@laynetworks.

com 1
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

Basic Java

Q1 Given the following class, which statements can be inserted at position 1 without causing
the code to fail compilation?
Public class Test { int a, b=0;
static int c;
public void m()
{ int d;
int e=0;
// Position 1
}}
(a) a++
(b) b++
(c) c++
(d) d++
(e) e++
Q2 Which statement are true concerning the effect of the >> and >>> operators?
(a) For non-negative values of the left operand, the >> and >>> operators will have the
same effect
(b) The result of (-1 >> 1) is 0
(c) The result of (-1 >>> 1) is –1
(d) The value returned by >>> will never be negative as long as the value of the right
operand is equal to or greater than 1
(e) When using the >> operator, the leftmost bit of the bit representation of the resulting
value will always as the same bit value as the leftmost bit of the bit representation of
the left operand
Q3 What is wrong with the following code?
Class MyException extends Exception{}
Public class Test {
public void foo() {
try{ bar(); }
finally { baz(); } catch (MyException e) {} }

public void bar () throws MyException { throw new MyException(); }


public void baz () throws RuntimeException { throw new RuntimeException(); }

(a) Since the method foo() does not catch the exception generated by the method baz(), it
must declare the RuntimeException in its throws clause
(b) A try block cannot be followed by both a catch and a finally block
(c) An empty catch block is not allowed
(d) A catch block cannot follow a finally block
(e) A finally block must always follow one or more catch blocks
Q4 What will be written to the standard output when the following program is run?
Public class Test {
Public static void main(String args [])
{ String word = “restructure”;
System.out.println(word.substring(2,3));
}}
(a) est
(b) es
(c) str
(d) st
(e) s
Q5 Given that a static method doIt() in a class Work represents work to be done, what block
of code will succeed in starting a new thread that will do the work?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 2
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
(a) Runnable r = new Runnable(){ Public void run(){ Work.doIt(); } };
Thread t new Thread( r );
t.start();
(b) Thread t new Thread(){ public void start(){Work.doIt(); }};
t.start();
(c) Runnable r = new Runnable() { Public void run(){ Work.doIt(); } }; r.start();
(d) Thread t new Thread( new Work()) ; t.start();
(e) Runnable t = new Runnable() { Public void run(){ Work.doIt(); } }; t.run();
Q6 Write a line of code that declares a variable named layout of type LayoutManager and
initializes it with a new object, which when used with a container can lay out components
in a rectangular grid of equal-sized rectangles, 3 components wide and 2 components
high
(a) LayoutManager layout = new Map(2,3);
(b) LayoutManager layout = new Paint(2,3);
(c) LayoutManager layout = new GridLayout(2,3);
(d) All of the above
Q7 What will be the result of compiling and running the following code?
Public class Test {
static int a;
int b;
Public Test1
{ Int c;
c=a;
a++;
b += c;
}
public static void main(String args[] ){ new Test1(); }
}
(a) The code will fail to compile, since the constructor is trying to access static members
(b) The code will fail to compile, since the constructor is trying to use static member
variable a before it has been initialized
(c) The code will fail to compile, since the constructor is trying to use static member
variable b before it has been initialized
(d) The code will fail to compile, since the constructor is trying to use local variable c
before it has been initialized
(e) The code will compile, and run without any problems.
Q8 What will be written to the standard output when the following program is run?
Public class Test{ Public static void main(String args[]) { System.out.println(9 ^ 2); } }
(a) 81
(b) 7
(c) 11
(d) 0
(e) false
Q9 Which statements are true concerning the default layout manager for containers in the
java.awt package?
(a) Objects instantiated from Panel do not have a default layout manager
(b) Objects instantiated from Panel have FlowLayout as default layout manager
(c) Objects instantiated from Applet have BorderLayout as default layout manager
(d) Objects instantiated from Dialog have BorderLayout as default layout manager
(e) Objects instantiated from Window have the same default layout manager as instances
of Applet
Q10 Which declarations will allow a class to be started as a standalone program?
(a) Public void main(String args[])
(b) Public void static main(String args[])
(c) Public static main(String[] argv)

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 3
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
(d) Final Public static void main(String[] array)
(e) Public static void main(String args[])
Q11 Under which circumstances will a thread stop?
(a) The method waitforId() in class MediaTracker is called
(b) The run() method that the Thread is executing ends
(c) The call to the start() method of the Thread object returns
(d) The suspend() method is called on the Thread object
(e) The wait() method is called on the Thread object
Q12 When creating a class that associates a set of keys with a set of values, which of these
interfaces is most applicable?
(a) Collection
(b) Set
(c) Sortedset
(d) Map
Q13 What does the value returned by the method getID() found in class java.awt.AWTEvent
uniquely identify?
(a) The particular event instance
(b) The source of the event
(c) The set of events that were triggered by the same action
(d) The type of event
(e) The type of component from which the event originated
Q14 What will be written to the standard output when the following program is run?
Class Base{
int i;
Base() { Add(1); }
void add(int v) { i += v ;}
void print() { System.out.println(i); }
Class Extension extends Base{
Extensions() { add(2);}
void add (int v) { i += v*2; }
Public class Test{
Public static void main(String args[]) { bogo(new Extension());}
static void bogo(Base b) { b.add(8); b.print(); }
}
(a) 9
(b) 18
(c) 20
(d) 21
(e) 22
Q15 Which lines of code are valid declarations of a native method when occurring within the
declaration of the following class?
Public class Test{ // insert declaration of a native method here }
(a) native public void setTemperature(int kelvin);
(b) private native void setTemperature(int kelvin);
(c) protected int native getTemperature();
(d) public abstract native void setTemperature(int kelvin);
(e) native int setTemperature(int kelvin) ()
Q16 How does the weighty property of the GridBagConstraints objects used in grid bag layout
affect the layout of the components?
(a) If affects which grid cell the components end up in
(b) If affects how the extra vertical space is distributed
(c) If affects the alignment of each components
(d) If affects whether the components completely fill their allotted display area vertically
Q17 Which statement can be inserted at the indicated position in the following code to make
the program write 1 on the standard output when run?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 4
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
Public class Test{
Int a = 1 ;
Int b = 1 ;
Int c = 1 ;
Class Inner{
Int a = 2 ;
Int get() {
int c = 3 ;
// insert statement here
return c;
} }
Test () {
Inner I = new Inner()
System.out.println(i.get());
}
public static void main(String args[]){ new Test(); }
}
(a) C = b;
(b) C = this.a;
(c) C = this.b;
(d) C = Test.this.a;
(e) C = c;
Q18 Which is the earliest line in the following code after which the object created on the line
marked (0) will be candidate for being garbage collected assuming no compiler
optimizations are done?
Public class Test{
Static String f(){
Static a = “hello”;
Static b = “bye” ; // (0)
Static c = b + “!” ; // (1)
Static d = b ;
b= a; // (2)
d= a; // (3)
return c ; // (4)
}
public static void main(String args[]) {
String msg = f();
System.out.println(msg); // (5)
} }

(a) The line marked (1)


(b) The line marked (2)
(c) The line marked (3)
(d) The line marked (4)
(e) The line marked (5)
Q19 Which methods from the String and StringBuffer classes modify the object on which they
are called?
(a) The charAt() method of the String class
(b) The toUpperCase() method of the String class
(c) The replace() method of the String class
(d) The reverse() method of the StringBuffer class
(e) The length() method of the StringBuffer class

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 5
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
Q20 Which statements, when inserted at the indicated position in the following code, will cause
a runtime exception when attempting to run the program?
Class A {}
Class B extends A{}
Class C extends A{}
Public class Test {
Public static void main(String args[]) {
A x = new A();
B y = new B();
C z = new C();
// insert statement here ;
} }
(a) x = y;
(b) z = x ;
(c) y = (B) x ;
(d) z = (C ) y;
(e) y = (A) y;
Q21 Which of these are keywords in Java?
(a) Default
(b) NULL
(c) String
(d) Throws
(e) Long
Q22 It is desirable that a certain method within a certain class only be accessed by classes that
are defined within the same package as the class of the method. How can such
restrictions be enforced?
(a) Mark the method with the keyword public
(b) Mark the method with the keyword protected
(c) Mark the method with the keyword private
(d) Mark the method with the keyword package
(e) Do not mark the method with any accessibility modifiers
Q23 Which code fragments will succeed in initializing a two dimensional array named tab with a
size that will cause the expression tab[3][2] to access a valid element?
(a) Int[] [] tab = { {0,0,0}, {0,0,0}};
(b) Int tab[] [] = new int[4][]; for(int I=0, I<tab.length; I++) tab[I] = new int[3];
(c) Int tab[][]= {
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0};
(d) Int tab [3][2];
(e) Int [] tab[]= { {0,0,0}, {0,0,0},{0,0,0},{0,0,0}};
Q24 What will be the result of attempting to run the following program?
Public class Test{
Public static void main(String args[])
{ String[][][] arr = { {{},null}, {{ “1”,”2”}, {“1”, null, “3”}}, {}, { {“1”,null} } };
System.out.println (arr.length + arr[1][2].length);
} }
(a) The program will terminate with an ArrayIndexOutOfBoundException
(b) The program will terminate with an NullPointerException
(c) 4 will be written to standard output
(d) 6 will be written to standard output
(e) 7 will be written to standard output
Q25 Which expressions will evaluate to true if preceded by the following code?
String a = “hello”;
String b = new String(a);

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 6
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
String c = a;
Char[] d = {‘h’,’e’,’l’,’l’,’o’};

(a) (a == “Hello”)
(b) (a == b)
(c) (a == c)
(d) a.equals(b)
(e) a.equals(d)
Q26 Which statements concerning the following code are true?
Class A {
public A() {}
public A(int I) { this(); }
}
class B extends A {
public boolean B(String msg) { return false; }
}
class C extends B {
private C() {super (); }
public C(String msg) { this();}
public C(int I) {}
}

(a) The code will fail to compile


(b) The constructor in A that takes an int as an argument will never be called as a result
of constructing an object of class B or C
(c) Class C has three constructors
(d) Objects of class B cannot be constructed
(e) At most one of the constructors of each class is called as a result of constructing an
object of class C
Q27 Given two collection objects referenced by col1 an col2, which of these statements are
true?
(a) The operation col1.retainAll(col2) will not modify the col1 object
(b) The operation col1.removeAll(col2) will not modify the col2 object
(c) The operation col1.addAll(col2) will return a new modify collection object, containing
elements from both col1 and col2
(d) The operation col1.containsAll(col2) will not modify the col1 object
Q28 Which statements concerning the relations between the following classes are true?
Class Foo{
Int num;
Baz comp = new Baz();
}
class Bar {
boolean flag ;
}
class Baz extends Foo{
Bar thing = new Bar();
Double limit ;
}
(a) A Bar is a Baz
(b) A Foo has a Bar
(c) A Baz is a Foo
(d) A Foo is a Baz
(e) A Baz has a Bar

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 7
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
Q29 Which statements concerning the value of a member variable are true, when no explicit
assignments have been made?
(a) The value of an int is undetermined
(b) The value of all numeric types is zero
(c) The compiler may issue an error if the variable is used before it is initialized
(d) The value of a String variable is “” (empty string)
(e) The value of all object variables is null
Q30 Which statements describe guaranteed behavior of the garbage collection and finalization
mechanisms?
(a) Objects are deleted when they can no longer be accessed through any reference
(b) The finalize() method will eventually be called on every object
(c) The finalize() method will never be called more than once on an object
(d) The object will not be garbage collected as long as it is possible for an active part of
the program to access it through a reference
(e) The garbage collector will use a mark and sweep algorithm
Q31 Which code fragments will succeed in printing the last argument given on the command
line to the standard output, and exit gracefully with no output if no arguments are given?
(a) Public static void main(String args[]){
If (args.length!=0)
System.out.println(args[args.length-1]);
}
(b) Public static void main(String args[]) {
Try { System.out.println(args[args.length]) ; }
Catch (ArrayIndexOutOfBoundsException e) {}
}
(c) Public static void main(String args[]) {
Int ix = args.length;
String last = args[ix];
If (ix > 0) System.out.println(last);
}
(d) Public static void main(String args[]) {
Int ix = args.length-1;
If (ix > 0) System.out.println(args[ix]);
}
(e) Public static void main(String args[]) {
Try { System.out.println(args[args.length-1]) ; }
Catch (NullPointerException e) {}
}
Q32 Which of the following statements concerning the collection interfaces are true?
(a) Set extends Collection
(b) All methods defined in Set are also defined in Collection
(c) List extends Collection
(d) All method defined in List are also defined in Collection
(e) Map extends Collection
Q33 The range of short is
(a) –2 7 to 2 7 –1
(b) –2 8 to 2 8
(c) –2 15 to 2 15 -1
(d) –2 16 to 2 16 –1
(e) 0 to 2 16 –1
Q34 What is the name of the method that threads can use to pause their execution until
signalled to continue by another thread?
(a) Wait
(b) Suspend
(c) Stop

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 8
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
(d) All of the above
Q35 Given the following class definitions, which expression identifies whether the object
referred to by obj was created by instantiating class B rather than classes A, C and D?
Class A {}
Class B extends A {}
Class C extends B {}
Class D extends A {}
(a) Obj instanceof B
(b) Obj instanceof A && ! (obj instanceof C)
(c) Obj instanceof B && ! (obj instanceof C)
(d) !(obj instanceof C || obj instanceof D)
(e) !(obj instanceof A) && ! (obj instanceof C) && ! (obj instanceof D)
Q36 What will be written to the standard output when the following program is run?
Public class Test {
Public static void main(String args[]){
Double d = -2.9;
Int i = (int) d;
i *= (int) Math.ceil(d);
i *= (int) Math.abs(d);
System.out.println(i);
} }
(a) –12
(b) 18
(c) 8
(d) 12
(e) 27
Q37 What will be written to the standard output when the following program is run
Public class Test{
Int a;
Int b;
Public void f() {
A = 0;
B=0;
Int [] c = { 0 };
G(b,c);
System.out.println(a+” “ + b + “ “ + c[0] + “ “ );
}
Public void g(int b, int [] c) {
A = 1;
B=1;
C[0] = 1;
}
Public static void main(String args[]) {
Test obj = new Test();
Obj.f();
}
}
(a) 0 0 0
(b) 0 0 1
(c) 0 1 0
(d) 1 0 0
(e) 1 0 1
Q38 Which statements concerning the effect of the statement gfx.drawRect (5,5,10,10) are
true, given that gfx is a reference to a valid Graphics object?
(a) The rectangle drawn will have a total width of 5 pixels
(b) The rectangle drawn will have a total height of 6 pixels

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 9
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
(c) The rectangle drawn will have a total width of 10 pixels
(d) The rectangle drawn will have a total height of 11 pixels
(e) None of the above
Q39 Given the following code, which code fragments, when inserted at the indicated location,
will succeed in making the program display a button spanning the whole window area?
Import java.awt. ;
Public class Test{
public static void main(String args[]) {
Window win = new Frame();
Button but = new Button(“button”);
// insert code fragment here
win.setSize(200,200);
win.setVisible(true);
} }
(a) Win.setLayout(new BorderLayout());
Win.add(but);
(b) Win.setLayout(new GridLayout(1,1));
Win.add(but);
(c) Win.setLayout(new BorderLayout( ));
Win.add(but.BorderLayout.CENTER);
(d) Win.add(but);
(e) Win.setLayout(new FlowLayout());
Win.add(but);
Q40 Which method implementations will write the given string to a file named “file”, using UTF8
encoding?
(a) Public void write(String msg) throws IOException{
FileWriter fw = new FileWriter(new File(“file”));
Fw.write(msg);
Fw.close();
}
(b) Public void write(String msg) throws IOException{
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(“file”),
“UTF8);
Osw.write(msg);
Osw.close();
}
(c) Public void write(String msg) throws IOException{
FileWriter fw = new FileWriter(new File(“file”));
Fw.setEncoding(“UTF8);
Fw.write(msg);
Fw.close();
}
(d) Public void write(String msg) throws IOException{
FilterWriter fw = new FilterWriter(new FileWriter(“file”),”UTF8”);
Fw.write(msg);
Fw.close();
}
(e) Public void write(String msg) throws IOException{
OutputStreamWriter osw = new OutputStreamWriter(new OutputStream(new
File(“file”)), UTF8);
Osw.write(msg);
Osw.close();
}
Q41 Which are the valid identifiers?
(a) _class
(b) $value$

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 10
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
(c) zer@
(d) angstrom
(e) 2much
Q42 What will be the output of attempting to compile and run the following program?
Public class Test{
Public static void main(String args[]) {
Int counter = 0;
L1:
For(int i = 10; I<0;I++){
L2:
Int j = 0;
While(j<10) {
If (j > 1) break L2;
If ( i == j ) {
Counter ++ ;
Continue L1;
}
}
counter - - ;
}
System.out.println(counter);
}
}
(a) The program will fail to compile
(b) The program will not terminate normally
(c) The program will write 10 to the standard output
(d) The program will write 0 to the standard output
(e) The program will write 9 to the standard output
Q43 Given the following definition, which definitions are valid?
Interface I {
void setValue(int val);
Int getValue(); }
(a) Class A extends I {
Int value;
Void setValue(int val) {value = val;}
Int getValue(){return value;} }
(b) Interface B extends I {
void increment(); }
(c) Abstract class C implements I {
Int getValue(); { return 0; }
Abstract void increment(); }
(d) Interface D implements I {
void increment(); }
(e) Class E implements I {
Int value ;
Public void setValue(int val) {value = val ;} }
Q44 Which statements concerning the methods notify() and notifyAll() are true?
(a) Instances of class Thread have a method called notify()
(b) A call to the method notify() will wake the thread that currently owns the monitor of the
object
(c) The method notify() is synchronized
(d) The method notifyAll() is defined in class Thread
(e) When there is more than one thread waiting to obtain the monitor of an object, there is
no way to be sure which thread will be notified by the notify() method
Q45 Which statements concerning the correlation between the inner and outer instances of
non-static inner classes are true?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 11
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
(a) Member variables of the outer instances are always accessible to inner instances,
regardless of their accessibility modifiers
(b) Member variables of the outer instances can never be referred to using only the
variable name within the inner instance
(c) More than one inner instance can be associated with the same outer instance
(d) All variables from the outer instance that should be accessible in the inner instance
must be declared final
(e) A class that is declared final cannot have any inner classes
Q46 What will be the result of attempting to compile and run the following code?
Public class Test{
Public static void main(String args[]) {
Int I = 4;
Float f = 4.3;
Double d = 1.8;
Int c = 0;
If ( I == f ) c++;
If ((int) ( f + d )) == ((int) f + (int) d)) c +=2;
System.out.println( c );
} }
(a) The code will fail to compile
(b) 0 will be written to the standard output
(c) 1 will be written to the standard output
(d) 2 will be written to the standard output
(e) 3 will be written to the standard output
Q47 Which operators will always evaluate all the operands?
(a) | |
(b) +
(c) &&
(d) ? :
(e) %
Q48 Which statements concerning the switch construct are true?
(a) All switch statements must have a default label
(b) There must be exactly one label for each code segment in a switch statement
(c) The keyword continue can never occur within the body of a switch statement
(d) No case label may follow a default label within a single switch statement
(e) A character literal can be used as a value for a case label
Q49 Which modifiers and return types would be valid in the declaration of a working main()
method for a Java standalone application?
(a) Private
(b) Final
(c) Static
(d) Int
(e) Abstract
Q50 What will be the appearance of an applet with the following init() method?
Public void init() { Add(new Button(“hello”)); }
(a) Nothing appears in the applet
(b) A button will cover the whole area of the object
(c) A button will appear in the top left corner of the applet
(d) A button will appear, centered in the top region of the applet
(e) A button will appear in the center of the applet

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 12
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
Q51 Which statements concerning the event model of the AWT are true?
(a) At most one listener of each type can be registered with a component
(b) Mouse motion listeners can be registered on a List instance
(c) There exists a class named ContainerEvent in package java.awt.event
(d) There exists a class named MouseMotionEvent in package java.awt.event
(e) There exists a class named ActionAdapter in package java.awt.event
Q52 Which statements are true, given the code new FileOutputStream(“data” , true) for
creating an object of class FileOutputStream?
(a) FileOutputStream has no constructors matching the given arguments
(b) An IOException will be thrown if a file named “data” already exists
(c) An IOException will be thrown if a file named “data” does not already exist
(d) If a file named “data’ exists, its contents will be reset and overwritten
(e) If a file named “data’ exists, its output will be appended to its current contents
Q53 Given the following code, write a line of code that, when inserted at the indicated location,
will make the overriding method in Extension invoke the overridden method in class Base
on the current object
Class Base{
Public void print() {
System.out.println(“base”);
}}
Class Extension extends Base{
Public void print() {
System.out.println(“extension”);
// insert line of implementation here
} }
public Class Test{
public static void main(String args[]) {
Extension ext = new Extention();
ext.print();
} }
(a) Super.print();
(b) Super.Test();
(c) Ext.print();
(d) None of the above
Q54 Given that file is a reference to a File object that represents a directory, which code
fragments will succeed in obtaining a list of the entries in the directory?
(a) Vector filelist = ((Directory) file).getList();
(b) String[] filelist = file.directory;
(c) Enumeration filelist = file.contents();
(d) String[] filelist = file.list();
(e) Vector filelist = (new Directory(file)).files();
Q55 What will be written to the standard output when the following program is run?
Public class Test{
Public static void main)String args[]) {
String space = “ “;
String composite = space + “hello” + space + space ;
Composite.concat(“world”);
String trimmed = composite.trim();
System.out.println(trimmed.length());
} }
(a) 5
(b) 6
(c) 7
(d) 12
(e) 13

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 13
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
Q56 Given the following code, which statement concerning the objects referenced through the
member variables i, j, k are true, given that any thread may call the methods a, b, c at any
time?
Class Counter{
Int v = 0;
Synchronized void inc() { v++; }
Synchronized void dec() { v- -; }
}
public class Test{
Counter I, j, k ;
Public synchronized void a() {
i.inc();
System.out.println(“a”);
i.dec();
}
Public synchronized void b() {
i.inc(); j.inc(); k.inc();
System.out.println(“b”);
i.dec(); j.dec(); k.dec();
}
Public void c() {
k.inc();
System.out.println(“c”);
k.dec();
}
}

(a) i.v is guaranteed always to be 0 or 1


(b) j.v is guaranteed always to be 0 or 1
(c) k.v is guaranteed always to be 0 or 1
(d) j.v will always be greater than or equal to k.v at any given time
(e) k.v will always be greater than or equal to j.v at any given time
Q57 Which statements concerning casting and conversion are true?
(a) Conversion from int to long does not need a cast
(b) Conversion from byte to short does not need a cast
(c) Conversion from float to long does not need a cast
(d) Conversion from short to char does not need a cast
(e) Conversion from boolean to int using a cast is not possible
Q58 Given the following code, which method declarations, when inserted at the indicated
position will not cause the program to fail compilation?
Public class Test {
Public ling sum (long a, long b) { return a + b; }
// insert new method declaration here
}

(a) Public int sum( int a, int b) { return a + b ; }


(b) Public int sum( long a, long b) { return 0 ; }
(c) Abstract int sum();
(d) Private long sum(long a , long b) {return a + b ; }
(e) Public long sum(long a , int b) {return a + b ; }
Q59 The 8859-1-character code for the uppercase letter A is 565. Which of these code
fragments declare and initialize a variable of type char with this value?
(a) Char ch = 65;
(b) Char ch = ‘\65’;
(c) Char ch = ‘\0041’;;
(d) Char ch = ‘A’;

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 14
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
(e) Char ch = “A” ;
Q60 Given the following code, which of these constructors could be added to the MySubclass
without causing a compile time error?
Class MySuper{
Int number ;
MySuper(int i ) {number = i ;}
}
class MySub extends MySuper {
int count;
MySub(int cnt, int sum) {
Super(num);
Count = cnt ;
}
// insert additional constructor here
}
(a) MySub() { }
(b) MySub( int cnt) { count = cnt; }
(c) MySub( int cnt) { super(); count = cnt; }
(d) MySub( int cnt) { count = cnt; super(cnt); }
(e) MySub( int cnt) { this(cnt , cnt); }
Q61 Which of these statements are true?
(a) A super() or this() call must always be provided explicitly as the first statement in the
body of a constructor
(b) If both a subclass and its superclass do not have any declared constructors, the
implicit default constructor of the subclass will call super() when run
(c) If neither super() or this() is declared as the first statement in the body of a
constructor, then this() will implicitly be inserted as the first statement
(d) If super() is the first statement in the body of constructor, then this() can be declared
as the second statement
(e) Calling super() as the first statement in the body of a constructor of a subclass will
always work, since all superclasses have a default constructor?
Q62 What will the following program print when run?
Public class MyClass {
Public static void main(String args[]) {
B b = new B(“Test”);
}
}
class A {
A() { this(“1”, “2”);}
A(String s, String t) { this( s + t );}
A(String s) { System.out.println(s);}
}
class B extends A {
B(String s) { System.out.println(s);}
B(String s, String t) { this( t + s + “3” );}
B() {super(“4”); };
}
(a) It will simply print Test
(b) It will print Test followed by Test
(c) It will print 123 followed by Test
(d) It will print 12 followed by Test
(e) It will print 4 followed by Test
Q63 Given the following variable declaration within the definition of an interface which of these
declarations are equivalent to it?
Int answer = 42;
(a) Public static int answer = 42;

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 15
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
(b) Public final int answer = 42;
(c) static final int answer = 42;
(d) Public int answer = 42;
(e) All of the above
Q64 What is wrong, if anything, with the following code?
Abstract class MyClass implements Interface1, Interface2 {
Void f() { } ;
Void g() { } ;
}
interface Interface1 {
int VAL_A = 1 ;
int VAL_B = 2 ;
void f() ;
void g() ;
}
interface Interface2 {
int VAL_B = 3 ;
int VAL_C = 4 ;
void g() ;
void h() ;
}
(a) Interface1 and Interface2 do not match, therefore MyClass cannot implement them
both
(b) MyClass only implements Interface1. Implementation for void h() from Interface2 is
missing
(c) The declarations of void g() in the two interfaces clash
(d) The declarations of int VAL_B in the two interfaces clash
(e) Nothing is wrong in the code, it will compile without errors
Q65 Given the following program, which statement is true?
Public class Test {
Public static void main(String args[]){
A[] arrA ;
B[] arrB ;
arrA = new A[10];
arrB = new B[20];
arrA = arrB; // (1)
arrB = (B[]) arrA ; // (2)
arrA = new A[10];
arrB = (B[]) arrA; // (3)
}
}
class A { }
class B extends A { }
(a) The program will fail to compile, owing to the line labeled (1)
(b) The program will throw a lava.lang.ClassCastException at the line labeled (2)
(c) The program will throw a lava.lang.ClassCastException at the line labeled (3)
(d) The program will compile and run without problems, even if the (B[]) cast in the lines
labeled (2) and (3) were removed
(e) The program will compile and run without problems, but would not do so if the (B[])
cast in the lines labeled (2) and (3) were removed
Q66 Which is the first line that will cause compilation to fail in the following program?
Class Test {
Public static void main(String args{}){
Test a ;
SubTest b ;
A = new test(); // (1)

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 16
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
B = new SubTest (); // (2)
A=b; // (3)
B=a; // (4)
A = new SubTest(); // (5)
B = new Test(); // (6)
}
}
class SubTest extends Test { }
(a) Line labeled (1)
(b) Line labeled (2)
(c) Line labeled (3)
(d) Line labeled (4)
(e) Line labeled (5)
(f) Line labeled (6)
Q67 Given three classes A,B and C where B is a subclass of A and C is a subclass of B, which
one of these boolean expressions correctly identifies when an object o has actually been
instantiated from class B as opposed from A or C?
(a) ( o instanceof B ) && ( !( o instanceof A ))
(b) ( o instanceof B ) && ( !( o instanceof C ))
(c) !(( o instanceof A ) || ( o instanceof B )
(d) ( o instanceof B )
(e) ( o instanceof B ) && !(( o instanceof A )) || (o instanceof C))
Q68 What will be the result of attempting to compile and run the following program?
Public class Polymorphism {
Public static void main(String args[]) {
A ref1 = new C();
B ref2 = (B) ref1;
System.out.println(ref2.f());
}
}
class A { int f() {return 0; } }
class B extends A { int f() {return 1; } }
class C extends B { int f() {return 2; } }
(a) The program will fail to compile
(b) The program will compile without errors, but will throw a ClassCastException when
run
(c) The program will compile without errors, and print 0 when run
(d) The program will compile without errors, and print 1 when run
(e) The program will compile without errors, and print 2 when run
Q69 Given the following code, which statements are true?
Public interface HeavenlyBody{ String describe(); }
Class Star implements HeveanlyBody{
String starName;
Public String describe() { return “star” + starName; }
}
class Planet{
String name;
Star orbiting;
Public String describe() {
Return “planet” + name + “orbiting” + orbiting.describe();
}
}
(a) The code will fail to compile
(b) The use of aggregation is justified, since planet has-a star
(c) The code will fail to compile if the name starName is replaced with the name
bodyName throughout the Star class definition

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 17
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
(d) the code will fail to compile if the name starName is replaced with the name name
throughout the Star class definition
(e) An instance of Planet is a valid instance of a HeavenlyBody
Q70 What will happen if you register more than one ActionListener in a button component?
(a) The program will fail to compile
(b) The program will issue a runtime exception during execution
(c) All the registered action listeners will be notified when the button is clicked
(d) The last registered action listeners will be notified when the button is clicked
(e) The first registered action listeners will be notified when the button is clicked

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 18
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

A1 A2 A3 A4 A5 A6 A7 A8 A9 A10
A,B,C, A,B,E D E A C E C B,D D,E
E

A11 A12 A13 A14 A15 A16 A17 A18 A19 A20
B D D E A,B B A,D C D C

A21 A22 A23 A24 A25 A26 A27 A28 A29 A30
D,E D,E B,E A C,D B,C B,D C,E B,E C,D

A31 A32 A33 A34 A35 A36 A37 A38 A39 A40
A A,B,C C A C C E D A,B,C B

A41 A42 A43 A44 A45 A46 A47 A48 A49 A50
A,B,D A B A,E A,C A B,E E B,C D

A51 A52 A53 A54 A55 A56 A57 A58 A59 A60
B,C E A D A A,B A,B,E A,E D E

A61 A62 A63 A64 A65 A66 A67 A68 A69 A70
B D E E C D B E B C

Advance JAVA

Q1 JAVA Swing are


(a) Methods
(b) Class
(c) Components
(d) Libraries
Q2 Difference between AWT and JFC Classes are:
(a) AWT classes are more functional than JFC classes and reside in the java.awt
package
(b) JFC classes are more functional, start with the letter J and reside in the
java.awt.swing package
(c) JFC classes use the AWT peer mechanism, start with the letter J and reside in the
java.awt.swing package
(d) None of the above
Q3 The content pane can be accessed by calling :
(a) The getContentPane on Jframe() from JFC
(b) The getFrame() on Frame() class from AWT
(c) The textfield on JtextField() from JFC
(d) Cannot be accessed
Q4 To present contents in a Tabbed Pane format :
(a) The TabbedPane class is used
(b) The JtabbedPane class is used
(c) The addTab() method is used
(d) None of the above
Q5 The Jlabel class has properties of:
(a) Displaying text message
(b) Icon to the text message
(c) Position of the text message and icon
(d) All of the above
Q6 Anonymous inner classes are
(a) A heavyweight listener and is generally avoided

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 19
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
(b) Never used in JAVA programming
(c) A useful way to add a lightweight listener to a component
(d) The class definition begins and ends with {( … )}; combination
Q7 The JtoggleButtons class announces state changes by sending ItemEvents to
ItemListenters
(a) True
(b) False
Q8 The Jcheckbox class is similar to JToggleButton
(c) True
(a) False
Q9 The JcomboBox class is like the java.awt.Choice class
(d) True
(a) False
Q10 The JSlider class
(a) Is unlike the java.awt.Scrollbar class
(b) Enhances the java.awt.Scrollbar class
(c) Both the classes are different in their functionality
(d) None of the above

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 20
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

Q11 The JToolBar class


(a) Is a rectangular area that can be detached from the window in which it resides and
placed on the desktop or any other region of the window
(b) Cannot be detached from the window in which it resides
(c) Is a rectangular area that cannot contain other components
(d) None the above
Q12 JAVA is
(a) A write once run anywhere language
(b) A write once run once compiler
(c) A machine dependent programming language
(d) A write once and compile at runtime language
Q13 Java based clients are
(a) Heavily dependent on hardware and software maintenance
(b) Expensive software requiring configurations
(c) Thin clients
(d) None of the above
Q14 Relational Database Management Systems (RDBMS) has been the pioneering vision of
(a) Microsoft
(b) IBM
(c) Bill Clinton
(d) Dr. E.F Codd
Q15 Data stored in RDBMS are retrieved through
(a) Natural Query Language
(b) Structured Query Language (SQL)
(c) Both the above
(d) None of the above
Q16 All RDBMS and DBMS systems support SQL to retrieve data
(a) True
(b) False
Q17 In a two tier model
(a) The database developer creates an application front-end and access data through a
socket connection to the server
(b) The database developer creates an application front-end and access data through a
socket connection on the same computer
(c) No socket connection is used to access data from the server database
(d) There is no concept of front end and back end database connectivity
Q18 The formatting and display of the data is the responsibility of :
(a) Server application
(b) Client application
(c) Third Party vendor
(d) A combination of the above
Q19 Macro Programs written for simple data manipulation are called
(a) Applications
(b) Functions
(c) Stored procedures
(d) Packages
Q20 ________ executes stored procedures automatically when some event occurs
(a) Remote Procedure Calls
(b) Triggers
(c) Remote Method Invocation
(d) All the above

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 21
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

Q21 Two tier database models have limitations like:


(a) Simple library functions for manipulation of server database
(b) New Versions could be easily incorporated without recompiling or redistribution
(c) Universal vendor provided libraries
(d) Large client side runtimes applications, driving up the cost
Q22 In a three tier database design, the client communicates with:
(a) The database server directly
(b) An intermediate server that provides a layer of abstraction from the database
(c) Client side libraries for database access
(d) Data reply procedure calls
Q23 The JDBC API is designed to allow developers to:
(a) Create database front ends without having to continually rewrite their code
(b) Rewrite their code using various vendor developed libraries
(c) Access proprietary third party solutions
(d) Create one time write and forget applications
Q24 JDBC provides a multi interfaced API that is uniform and database dependent
(a) True
(b) False
Q25 The Java interface drivers
(a) Access the database servers for fetching the data from the database table
(b) Are mapped to the corresponding library routine calls of the database serer
(c) Take care of the translation of the standard JDBC calls into the specific calls required
by the database it supports
(d) There is no concept of drivers in Java
Q26 JDBC is
(a) Derived from Microsoft’s Open Database Connectivity specification (ODBC)
(b) Developed in Java and based on X/Open SQL Command Level Interface
(c) Non compliant with ODBC drivers
(d) Developed by Microsoft in conjunction with ODBC calls
Q27 The JDBC API has ______________ and _______________ interface
(a) Application Layer and Client Layer
(b) Client Layer and Business Layer
(c) Middleware Layer and Firmware Layer
(d) Application Layer and Driver Layer
Q28 The main interfaces for a Driver Layer are:
(a) Driver, Connection, Statement and ResultSet
(b) Application, Connection, Statement and ResultSet
(c) DriverManager, Connection, Statement and ResultSet
(d) ApplicationManager, Connection, Statement and ResultSet
Q29 The DriverManager is responsible for:
(a) Loading and unloading drivers, making connection through drivers, logging and
database login timeouts
(b) Loading and making connection through drivers, database login timeouts
(c) Unloading and disconnecting through drivers, logout and login timeouts
(d) None of the above
Q30 Every JDBC program must have at least _______ implementations of the JDBC driver
(a) 0
(b) 10
(c) 1
(d) 3

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 22
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

Answers:
A1 A2 A3 A4 A5 A6 A7 A8 A9 A10
C B A B D C A A A B

A11 A12 A13 A14 A15 A16 A17 A18 A19 A20
A A C D B A A B C B

A21 A22 A23 A24 A25 A26 A27 A28 A29 A30
D B A B C B D A A C

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 23
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
1. Which statement are characteristics of the >> and >>> operators.

A. >> performs a shift


B. >> performs a rotate
C. >> performs a signed and >>> performs an unsigned shift
D. >> performs an unsigned and >>> performs a signed shift
E. >> should be used on integrals and >>> should be used on floating point
types

C.

2. Given the following declaration

String s = "Example";

Which are legal code?

A. s >>> = 3;
B. s[3] = "x";
C. int i = s.length();
D. String t = "For " + s;
E. s = s + 10;

CDE.

3. Given the following declaration

String s = "hello";

Which are legal code?

A. s >> = 2;
B. char c = s[3];
C. s += "there";
D. int i = s.length();
E. s = s + 3;

CDE.

4. Which statements are true about listeners?

A. The return value from a listener is of boolean type.


B. Most components allow multiple listeners to be added.
C. A copy of the original event is passed into a listener method.
D. If multiple listeners are added to a single component, they all must all be
friends to
each other.
E. If the multiple listeners are added to a single component, the order [in which
listeners
are called is guaranteed].

BC.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 24
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

5. What might cause the current thread to stop executing.

A. An InterruptedException is thrown.
B. The thread executes a wait() call.
C. The thread constructs a new Thread.
D. A thread of higher priority becomes ready.
E. The thread executes a waitforID() call on a MediaTracker.

ABDE.

6. Given the following incomplete method.

1. public void method(){


2.
3. if (someTestFails()){
4.
5. }
6.
7.}

You want to make this method throw an IOException if, and only if, the method
someTestFails() returns a value of true.
Which changes achieve this?

A. Add at line 2: IOException e;


B. Add at line 4: throw e;
C. Add at line 4: throw new IOException();
D. Add at line 6: throw new IOException();
E. Modify the method declaration to indicate that an object of [type]
Exception might be thrown.

DE. (E suppose they mean the method declaration for someTestFails.)

7. Which modifier should be applied to a method for the lock of the object this
to be
obtained prior to executing any of the method body?

A. final
B. static
C. abstract
D. protected
E. synchronized

E.

8. Which are keywords in Java?

A. NULL
B. true
C. sizeof
D. implements
E. instanceof

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 25
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

DE.

9. Consider the following code:

Integer s = new Integer(9);


Integer t = new Integer(9);
Long u = new Long(9);

Which test would return true?

A. (s==u)
B. (s==t)
C. (s.equals(t))
D. (s.equals(9))
E. (s.equals(new Integer(9))

CE.

10. Why would a responsible Java programmer want to use a nested class?

Don't know the answers. But here are some reasons from Exam Cram.

a.To keep the code for a very specialized class in close association with the
class it works with.
b. To support a new user interface that generates custom events.
c. To impress the boss with his/her knowledge of Java by using nested classes
all over the
place.

AB.

11. You have the following code. Which numbers will cause "Test2" to be
printed?

switch(x){

case 1:
System.out.println("Test1");
case 2:

case 3:
System.out.println("Test2");
break;
}
System.out.println("Test3");
}

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

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 26
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

BCD.
11. You have the following code. Which numbers will cause "Test3" to be
printed?

switch(x){

case 1:
System.out.println("Test1");
case 2:

case 3:
System.out.println("Test2");
break;
default:
System.out.println("Test3");
}

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

12. Which statement declares a variable a which is suitable for referring to an


array of 50
string objects?

A. char a[][];
B. String a[];
C. String []a;
D. Object a[50];
E. String a[50);
F. Object a[];

BCF.

13. What should you use to position a Button within an application frame so
that the
width of the Button is affected by the Frame size but the height is not affected.

A. FlowLayout
B. GridLayout
C. Center area of a BorderLayout
D. East or West of a BorderLayout
E. North or South of a BorderLayout

E.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 27
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
14. What might cause the current thread to stop executing?

A. An InterruptedException is thrown
B. The thread executes a sleep() call
C. The thread constructs a new Thread
D. A thread of higher priority becomes ready (runnable)
E. The thread executes a read() call on an InputStream

ABDE.

Non-runnable states:

* Suspended: caused by suspend(), waits for resume()


* Sleeping: caused by sleep(), waits for timeout
* Blocked: caused by various I/O calls or by failing to get a monitor's lock,
waits for I/O or
for the monitor's lock
* Waiting: caused by wait(), waits for notify() or notifyAll()
* Dead: Caused by stop() or returning from run(), no way out

From Certification Study Guide p. 227

15. Consider the following code:

String s = null;

Which code fragments cause an object of type NullPointerException to be


thrown?

A. if((s!=null) & (s.length()>0))


B. if((s!=null) &&(s.length()>0))
C. if((s==null) | (s.length()==0))
D. if((s==null) || (s.length()==0))

AC.

16. Consider the following code:

String s = null;

Which code fragments cause an object of type NullPointerException to be


thrown?

A. if((s== null) & (s.length()>0))


B. if((s==null) &&(s.length()>0))
C. if((s!=null) | (s.length()==0))
D. if((s!=null) || (s.length()==0))

ABCD.

17. Which statement is true about an inner class?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 28
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
A. It must be anonymous
B. It can not implement an interface
C. It is only accessible in the enclosing class
D. It can only be instantiated in the enclosing class
E. It can access any final variables in any enclosing scope.

E.

18. Which statements are true about threads?

A. Threads created from the same class all finish together


B. A thread can be created only by subclassing Java.Lang.Thread.
C. Invoking the suspend() stops a thread so that it cannot be restarted
D. The Java Interpreter's natural exit occurs when no non daemon threads
remain alive
E. Uncoordinated changes to shared data by multiple threads may result in the
data being
read, or left, in an inconsistent state.

DE.

19. Consider the following code:

1. public void method(String s){


2. String a,b;
3. a = new String("Hello");
4. b = new String("Goodbye");
5. System.out.println(a + b);
6. a = null;
7. a = b;
8. System.out.println(a + b);
9. }

Where is it possible that the garbage collector will run the first time?

A. Just before line 5


B. Just before line 6
C. Just before line 7
D. Just before line 8
E. Never in this method

C.

20. Which code fragments would correctly identify the number of arguments
passed via
the command line to a Java application, excluding the name of the class that is
being
invoked?

A. int count = args.length;


B. int count = args.length - 1;
C. int count = 0;
while (args[count] != null)

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 29
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
count ++;
D. int count = 0;
while (!(args[count].equals("")))
count ++;

A.

21. Which are keywords in Java?

A. sizeof
B. abstract
C. native
D. NULL
E. BOOLEAN

BC.

22. Which are correct class declarations? Assume in each case text
constitutes the entire
contents of a file called Fred.java on a system with a case-significant system.

A. public class Fred {


public int x = 0;
public Fred (int x) {
this.x = x;
}
}

B. public class fred {


public int x = 0;
public fred (int x) {
this.x = x;
}
}

C. public class Fred extends MyBaseClass, MyOtherBaseClass {


public int x = 0;
public Fred (int xval) {
x = xval;
}
}

D. protected class Fred {


private int x = 0;
private Fred (int xval) {
x = xval;
}
}

E. import java.awt.*;
public class Fred extends Object {
int x;
private Fred (int xval) {
x = xval;

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 30
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
}
}

AE.

22. Which are correct class declarations? Assume in each case text constitutes
the entire
contents of a file called Fred.java on a system with a case-significant system.

A. public class Fred {


public int x = 0;
public Fred (int x) {
this.x = x;
}
}

B. public class fred {


public int x = 0;
public fred (int x) {
this.x = x;
}
}

C. public class Fred extends MyBaseClass, MyOtherBaseClass {


public int x = 0;
public Fred (int xval) {
x = xval;
}
}

D. protected class Fred {


private int x = 0;
private Fred (int xval) {
x = xval;
}
}

E. import java.awt.*;
public class Fred extends Object {
int x;
private Fred (int xval) {
x = xval;
}
}

AE.
//Do car in the place of fred

B is wrong because of case-sensitivity. C is wrong because multiple


inheritance is not
supported in Java. D is wrong because a class can only be public, abstract,
final or default
(with no access modifier).

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 31
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
23. Which are correct class declarations? Assume in each case text
constitutes the entire
contents of a file called Test.java on a system with a case-significant system.

A. public class Test {


public int x = 0;
public Test (int x) {
this.x = x;
}
}

B. public class Test extends MyClass, MyOtherClass {


public int x = 0;
public Test (int xval) {
x = xval;
}
}

C. import java.awt.*;
public class Test extends Object {
int x;
private Test (int xval) {
x = xval;
}
}

D. protected class Test {


private int x = 0;
private Test (int xval) {
x = xval;
}
}

AC.

24. A class design requires that a particular member variable must be


accesible for direct
access by any subclasses of this class, otherwise not by classes which are not
members
of the same package. What should be done to achieve this?

A. The variable should be marked public


B. The variable should be marked private
C. The variable should be marked protected
D. The variable should have no special access modifier
E. The variable should be marked private and an accessor method provided

C.

25. Which correctly create an array of five empty Strings?

A. String a [] = new String [5];


for (int i = 0; i < 5; a[i++] = "");

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 32
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

B. String a [] = {"", "", "", "", "", ""};

C. String a [5];
D. String [5] a;
E. String [] a = new String[5];
for (int i = 0; i < 5; a[i++] = null);

AB.

26. Which cannot be added to a Container?

A. an Applet
B. a Component
C. a Container
D. a Menu
E. a Panel

D.

26. Which cannot be added to a Container?

A. an Applet
B. a Panel
C. a Container
D a Container
E. a MenuItem

E.

27. FilterOutputStream is the parent class for BufferedOutputStream,


DataOutputStream
and PrintStream. Which classes are a valid argument for the constructor of a
FilterOutputStream?

A. InputStream
B. OutputStream
C. File
D. RandomAccessFile
E. StreamTokenizer

B.

28. FilterInputStream is the parent class for BufferedInputStream and


DataInputStream.
Which classes are a valid argument for the constructor of a FilterInputStream?

A. File
B. InputStream
C. OutputStream
D. FileInputStream
E. RandomAccessFile

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 33
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
B.

29. Given the following method body:

{
if (atest()) {
unsafe():
}
else {
safe();
}
}

The method "unsafe" might throw an AWTException (which is not a subclass


of
RunTimeException). Which correctly completes the method of declaration
when added at
line one?

A. public AWTException methodName()


B. public void methodname()
C. public void methodName() throw AWTException
D. public void methodName() throws AWTException
E. public void methodName() throws Exception

DE.

30. Given a TextArea using a proportional pitch font and constructed like this:

TextField t = new TextArea("12345", 5, 5);

Which statement is true?

A. The displayed width shows exactly five characters on each line unless
otherwise
constrained.
B. The displayed height is five lines unless otherwise constrained.
C. The maximum number of characters in a line will be five.
D. The user will be able to edit the character string.
E. The displayed string can use multiple fonts.

BD.

31. Given this skeleton of a class currently under construction:

public class Example {


int x, y;

public Example(int a){


//lots of complex computation
x = a;
}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 34
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
public Example(int a, int b) {
/*do everything the same as single argument version of constructor
including assignment x = a */
y = b;
}
}

What is the most concise way to code the "do everything..." part of the
constructor
taking two arguments?

Answer:

this(a);

32. Given this skeleton of a class currently under construction:

public class Example {


int x, y;

public Example(int a, int b){


//lots of complex computation
x = a;
}

public Example(int a, int b, long c) {


/*do everything the same as the two argument version of constructor
including assignment x = a */
y = b;
}
}

What is the most concise way to code the "do everything..." part of the
constructor
taking two arguments?

Answer:

this(a, b);

Warning! A similar question will appear with at least two classes, then it is
super("arguments"); that you should use.

33. Which correctly create a two dimensional array of integers?

A. int a [][] = new int [10,10];


B. int a [10][10] = new int [][];
C. int a [][] = new int [10][10];
D. int []a[] = new int [10][10];
E. int [][]a = new int [10][10];

CDE.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 35
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
34. Given the following method body:

{
if (sometest()) {
unsafe();
}
else {
safe();
}
}

The method "unsafe" might throw an IOException (which is not a subclass of


RunTimeException). Which correctly completes the method of declaration
when added at
line one?

A. public void methodName() throws Exception


B. public void methodname()
C. public void methodName() throw IOException
D. public void methodName() throws IOException
E. public IOException methodName()

AD.

35. What would be the result of attempting to compile and run the following
piece of
code?

public class Test {


static int x;

public static void main(String args[]){


System.out.println("Value is " + x);
}
}

A. The output "Value is 0" is printed.


B. An object of type NullPointerException is thrown.
C. An "illegal array declaration syntax" compiler error occurs.
D. A "possible reference before assignment" compiler error occurs.
E. An object of type ArrayIndexOutOfBoundsException is thrown.

A.

35. What would be the result of attempting to compile and run the following
piece of
code?

public class Test {


static int[] x = new int[10];

public static void main(String args[]){


System.out.println("Value is " + x[5]);
}
}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 36
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

A. The output "Value is 0" is printed.


B. An object of type NullPointerException is thrown.
C. An "illegal array declaration syntax" compiler error occurs.
D. A "possible reference before assignment" compiler error occurs.
E. An object of type ArrayIndexOutOfBoundsException is thrown.

A.

36. What would be the result of attempting to compile and run the following
piece of
code?

public class Test {


public int x;

public static void main(String args[]){


System.out.println("Value is " + x);
}
}

A. The output "Value is 0" is printed.


B. Non-static variable x cannot be referenced from a static context..
C. An "illegal array declaration syntax" compiler error occurs.
D. A "possible reference before assignment" compiler error occurs.
E. An object of type ArrayIndexOutOfBoundsException is thrown.

B.

37. What would be the result of attempting to compile and run the following
piece of
code?

public class Test {

public static void main(String args[]){


int x;
System.out.println("Value is " + x);
}
}

A. The output "Value is 0" is printed.


B. An object of type NullPointerException is thrown.
C. An "illegal array declaration syntax" compiler error occurs.
D. A "possible reference before assignment" compiler error occurs.
E. An object of type ArrayIndexOutOfBoundsException is thrown.

D. Compiler says variable x might not have been initialized.

38. What should you use to position a Button within an application Frame so
that the size
of the Button is NOT affected by the Frame size?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 37
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
A. a FlowLayout
B. a GridLayout
C. the center area of a BorderLayout
D. the East or West area of a BorderLayout
E. The North or South area of a BorderLayout

A.

For the following six questions you will be presented with a picture in the real
test,
showing the relationship and quite long text, but don't be afraid the questions
are quite
easy.

39. We have the following organization of classes.

class Parent {}
class DerivedOne extends Parent {}
class DerivedTwo extends Parent {}

Which of the following statements is correct for the following expression?

Parent p = new Parent();


DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
p = d1;

A. Illegal both at compile and runtime.


B. Legal at compile time, but may fail at runtime.
C. Legal at both compile and runtime.
D. ....
E. ....

C.

39. We have the following organization of classes.

class Parent {}
class DerivedOne extends Parent {}
class DerivedTwo extends Parent {}

A methods declares three variables as shown bellow and assign them non null
values.
Which of the following statements is correct for the following expression?

Parent p;
DerivedOne d1;
DerivedTwo d2;
p = d1;

A. Illegal both at compile and runtime.


B. Legal at compile time, but may fail at runtime.
C. Legal at both compile and runtime.
D. ....

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 38
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
E. ....

C.

40. We have the following organization of classes.

class Parent {}
class DerivedOne extends Parent {}
class DerivedTwo extends Parent {}

Which of the following statements is correct for the following expression?

Parent p = new Parent();


DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
d2 = d1;

A. Illegal both at compile and runtime.


B. Legal at compile time, but may fail at runtime.
C. Legal at both compile and runtime.
D. ....
E. ....

A.

41. We have the following organization of classes.

class Parent {}
class DerivedOne extends Parent {}
class DerivedTwo extends Parent {}

Which of the following statements is correct for the following expression?

Parent p = new Parent();


DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
d1 = (DerivedOne)d2;

A. Illegal both at compile and runtime.


B. Legal at compile time, but may fail at runtime.
C. Legal at both compile and runtime.
D. ....
E. ....

A. Illegal both at compile and runtime. You cannot assign an object to a sibling
reference,
even with casting.

43. We have the following organization of classes.

class Parent {}
class DerivedOne extends Parent {}
class DerivedTwo extends Parent {}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 39
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
Which of the following statements is correct for the following expression?

Parent p = new Parent();


DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
d1 = (DerivedOne)p;

A. Illegal both at compile and runtime.


B. Legal at compile time, but may fail at runtime.
C. Legal at both compile and runtime.
D. ....
E. ....

B.

44. We have the following organization of classes.

class Parent {}
class DerivedOne extends Parent {}
class DerivedTwo extends Parent {}

Which of the following statements is correct for the following expression?

Parent p = new Parent();


DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
d1 = p;

A. Illegal both at compile and runtime.


B. Legal at compile time, but may fail at runtime.
C. Legal at both compile and runtime.
D. ....
E. ....

A.

45. We have the following organization of classes.

class Parent {}
class DerivedOne extends Parent {}
class DerivedTwo extends Parent {}

Which of the following statements is correct for the following expression?

Parent p = new Parent();


DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
p = (Parent)d1;

A. Illegal both at compile and runtime.


B. Legal at compile time, but may fail at runtime.
C. Legal at both compile and runtime.
D. ....
E. ....

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 40
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

C.

46. What would you use when you have duplicated values that needs to be
sorted?

A. Map
B. Set
C. Collection
D. List
E. Enumeration

D.

47. What kind of reader do you use to handle ASCII code?

A. BufferedReader
B. ByteArrayReader
C. PrintWriter
D. InputStreamReader
E. ?????

D.

InputStreamReader and FileReader automatically converts from a particular


character
encoding to Unicode. From Core Java Advanced Features p. 820.

48. How can you implement encapsulation in a class?

A. Make all variables protected and only allow access via methods.
B. Make all variables private and only allow access via methods.
C. Ensure all variables are represented by wrapper classes.
D. Ensure all variables are accessed through methods in an ancestor class.

B.

49. What is the return-type of the methods that implement the MouseListener
interface?

A. boolean
B. Boolean
C. void
D. Pont

C.

50. What is true about threads that stop executing?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 41
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
A. When a running thread's suspend() method is called, then it is indefinitely
possible for
the thread to start.
B. The interpreter stops when the main method stops.
C. A thread can stop executing when another thread is in a runnable state.
D. ......
E. ......

C. {It is doubtable answer it might be A}

51. For which of the following code will produce "test" as output on the
screen?

A. int x=10.0;
if (x=10.0)
{
System.out.println("test");
}

B. int x=012;
if (x=10.0)
{
System.out.println("test");
}

C. int x=10f;
if (x=10.0)
{
System.out.println("test");
}

D. int x=10L;
if (x=10.0)
{
System.out.println("test");
}

B.

52. Given this class ?

class Loop {
public static void main (String [] args){
int x=0;
int y=0,
outer: for (x=0; x<100;x++) {
middle: for (y=0;y<100y++) {

System.out.printl("x=" + x + "; y=" + y);

if (y==10){

<<<>>>}
}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 42
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
}
}//main
}//class

The question is which code must replace the <<<>>> to finish the outer loop?
A. continue middle;
B. break outer;
C. break middle;
D. continue outer;
E. none of these...

B.

53. What does it mean when the handleEvent() returns the true boolean?

A. The event will be handled by the component.


B. The action() method will handle the event.
C. The event will be handled by the super.handleEvent().
D. Do nothing.

A.

54. What is the target in an Event?

A. The Object() where the event came from.


B. The Object() where the event is destined for.
C. What the Object() that generated the event was doing.

A.

55. What is the statement to assign a unicode constant CODE with 0x30a0?

Answer:

public static final char CODE='\u30a0';

56. The following code resides in the source?

class StringTest {

public static void main (String [] args){


//
// String comparing
//
String a,b;
StringBuffer c,d;
c = new StringBuffer ("Hello");
a = new String("Hello");
b = a;
d = c;

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 43
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
if (<<>>) {}
}
}//class

Which of the following statement return true for the <<>> line in
StringTest.class?

A. b.equals(a)
B. b==a
C. d==c
D. d.equals(c)

ABCD.

57. Which are valid identifiers?

A. %fred
B. *fred
C. thisfred
D. 2fred
E. fred

CE.

57. Which are valid identifiers?

A. Employee
B. _Employee
C. %something
D. *something
E. thisemployee

A.B.E.

58. We have the following class X.

public class X {

public void method();

<<>>

Which of the following statement return true for the <<>> line in
StringTest.class?

A. abstract void method() ;


B. class Y extends X {}
C. package java.util;
D. abstract class z { }

BD.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 44
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
59. Which code fragments would correctly identify the number of arguments
passed via
the command line to a Java Application?

A. int count = args.length;


B. int count = 0;
while args[count] !=null)
count++;
C. int count = 0;
while (!(args[count].equals("")))
count++;
D. int count = args.length - 1;

A.

60. Given a TextField that is constructed like this:

TextField t = new TextField(30);

Which statement is true?

A. The displayed width is 30 columns.


B. The displayed string can use multiple fonts.
C. The user will be able to edit the character string.
D. The displayed line will show exactly thirty characters.
E. The maximum number of characters in a line will be thirty.

AC.

61. What does the java runtime option -cs do?

A. check source with debug report


B. clear classes that are existing when starting compilation.
C. check if the source is newer when loading classes.

C.

62. When is an action invoked?

A. TextField Enter
B. TextArea Enter
C. Scrollbar
D. MouseDown
E. Button

AE.

63. What error does the following code generate?

class SuperClass{

SuperClass(String s){}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 45
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
}

class SubClass extends SuperClass{


SubClass(){}
}

......

SubClass s1 = new SubClass("The");


SuperClass s = new SubClass("The");

A. java.lang.ClassCastException
B. Wrong number of arguments in constructor
C. Incompatible type for =. Can't convert SubClass to SuperClass.
D. No constructor matching SuperClass found in class SuperClass.

BD.

64. How do you declare a native method called myMethod in Java?

Answer:

public native void myMethod();

65. What should you use to position a component within an application frame
so that the
components height is affected by the Framesize?

A. FlowLayout
B. GridLayout
C. Center area of a BorderLayout
D. East or West of a BorderLayout
E. North or South of a BorderLayout

D. C.

66. What should you use to position a component within an application frame
so that the
components width is affected by the Framesize?

A. FlowLayout
B. GridLayout
C. Center area of a BorderLayout
D. East or West of a BorderLayout
E. North or South of a BorderLayout

E.C.

67. What should you use to position a Button within an application frame so
that the
height of the Button is affected by the Frame size but the width is not affected.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 46
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
A. FlowLayout
B. GridLayout
C. Center area of a BorderLayout
D. East or West of a BorderLayout
E. North or South of a BorderLayout

D.

68. Which are keywords in Java?

A. NULL
B. sizeof
C. friend
D. extends
E. synchronized

DE.

69. Which is the advantage of encapsulation?

A. Only public methods are needed.


B. No exceptions need to be thrown from any method.
C. Making the class final causes no consequential changes to other code.
D. It changes the implementation without changing the interface and causes
no
consequential changes to other code.
E. It changes the interface without changing the implementation and causes
no
consequential changes to other code.

D.

70. What can contain objects that have a unique key field of String type, if it is
required
to retrieve the objects using that key field as an index?

A. Map
B. Set
C. List
D. Collection
E. Enumeration

A.

71. Which statement is true about a non-static inner class?

A. It must implement an interface.


B. It is accessible from any other class.
C. It can only be instantiated in the enclosing class.
D. It must be final if it is declared in a method scope.
E. It can access private instance variables in the enclosing object.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 47
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
E.

72. Which declares an abstract method in an abstract Java class?

A. public abstract method();


B. public abstract void method();
C. public void abstract Method(};
D. public void method() {abstract;/}
E. public abstract void method() {/}
F. public void abstract Method();

B.

73. Which statements on the<<< call>>> line are valid expressions?

public class SuperClass {


public int x;
int y;
public void m(int a) {}
Superclass(){ }
}
class SubClass extends SuperClass{
private float f;
void m2() { return; }
SubClass() { }
}
class T {
public static void main (String [] args) {
int i;
float g;
SubClass b = SubClass();
<<< calls >>>
}
}

A. b.m2();
B. g=b.f;
C. i=b.x;
D. i=b.y;
E. b.m(6);

ACDE.

74. Type 7 in hexadecimal form.

Answer:

0x7

75. Type 7 in octal form.

Answer:

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 48
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

07

76. What is the range of an integer?

A. -128–127
B. -32 768 – 32 767
C. -231 – 231 -1
D. -232 – 232 -1

C.

77. What is the range of a char?

A. ‘\u0000’ – ‘\uFFFF’
B. -32 768 – 32 767
C. -128 – 127
D. -231 – 231 -1

A.

78. What is the range of a char?

A. 0 – 215-1
B. -32 768 – 32 767
C. - 2 16 – 216-1
D. 0 – 216-1

D.

78. What is the range of a char?

A. 0 – 216-1
B. 0 – 232 -1
C. - 2 16 – 216-1
D. 0 – 216-1

D.

79. Which method contains the code-body of a thread?

A. start()
B. run()
C. init()
D. suspend()
E. continue()

B.

80. What can you place first in this file?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 49
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

//What can you put here?

public class Apa{}

A. class a implements Apa


B. protected class B {}
C. private abstract class{}
D. import java.awt.*;
E. package dum.util;
F. private int super = 1000;

ADE.

81. What is the output of the following code?

outer: for(int i=1; i<3; i++){


inner: for(int j=1; j<3; j++){
if (j==2){
continue outer;
}
System.out.println(i+" and "+j);
}
}

A. 1 and 1
B. 1 and 2
C. 2 and 1
D. 2 and 2
E. 2 and 3
F. 3 and 2
G. 3 and 3

AC.

82. What is the output of the following code?

outer: for(int i=1; i<2; i++){


inner: for(int j=1; j<2; j++){
if (j==2){
continue outer;
}
System.out.println(i " and " j);
}
}

A. 1 and 1
B. 1 and 2
C. 2 and 1
D. 2 and 2
E. 2 and 3
F. 3 and 2
G. 3 and 3

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 50
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
A.

82. What is the output of the following code?

outer: for(int x=0; i<2; x++){


inner: for(int a=0; a<2; a++){
if (a==1){
continue outer;
}
System.out.println(a +" and "+x);
}
}

A. 0 and 0
B. 0 and 1
C. 2 and 1
D. 1 and 2
E. 1 and 2
F. 2 and 1
G. 1 and 3

A.B.

83. How do you declare a native method?

A. public native method();


B. public native void method();
C. public void native method();
D. public void native() {}
E. public native void method() {}

B.

84. Given the following code, what is the output when a exception other than a
NullPointerException is thrown?

try{

//some code
}
catch(NullPointerException e) {
System.out.println("thrown 1");
}
finally {
System.out.println("thrown 2");
}
System.out.println("thrown 3");

A thrown 1
B thrown 2
C thrown 3
D none

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 51
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
B.

85. You have been given a design document for a employee system for
implementation in
Java. It states.
bla bla bla

choose between the following words:

public
class
object
extends
Employee
Person
plus at least five more

How should you name the class?

Answer:

public class Employee extends Person

86. You have been given a design document for a polygon system for
implementation in
Java. It states.

A polygon is drawable, is a shape. You must access it. It should have values
in a vector
bla bla

Which variables should you use?

choose between the following words:

public
object
vector
drawable
color
plus some more

Answer:

vector, color (not sure)

87. You have been given a design document for a polygon system for
implementation in
Java. It states. A polygon is a Shape. You must access it. It has a vector and
corners bla
bla

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 52
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
choose between the following words: What will you write when defining the
class?

public
class
Polygon
object
extends
Shape
plus some more

Answer:

public class Polygon extends Shape

88. What is true about the garbage collection?

A. The garbage collector is very unpredictable.


B. The garbage collector is predictable.
C.
D.
E.

A.

87. What is true about a thread?

A The only way you can create a thread is to subclass java.lang.Thread


B If a Thread with higher priority than the currently running thread is created,
the thread
with the higher priority runs.
C.
D.
E.

B (not sure)

88. What happens when you compile the following code?

public classTest {

public static void main(String args[] ) {


int x;
System.out.println(x);
}

A. Error at compile time. Malformed main method


B. Clean compile
C. Compiles but error at runtime. X is not initialized
D.
E. Compile error. Variable x may not have been initialized.

E.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 53
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

89.What happens hen you run the following program with the command:

java Prog cat dog mouse

public class Prog {

public static void main(String args[]) {


System.out.println(args[0]) ;
}

A. Error at compile time. ArrayIndexOutOfBoundsException thrown


B. Compile with no output
C. Compile and output of dog
D. Compile and output of cat
E. Compile and output of mouse

D.
89.What happens hen you run the following program with the command:

java Prog cat

public class Prog {

public static void main(String args[]) {


System.out.println(args[0]) ;
}

A. Error at compile time. ArrayIndexOutOfBoundsException thrown


B. Compile with no output
C. Compile and output of dog
D. Compile and output of cat
E. Compile and output of mouse

D.

90. Which number for the argument must you use for the code to print cat?

With the command:

java Prog dog cat mouse

public class Prog {

public static void main(String args[]) {


System.out.println(args[?]) ;
}
}

A. 0
B. 1
C. 2
D. 3
E. none of these

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 54
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

B.

91. Which number for the argument must you use for the code to print cat?
With the command:

java Prog cat dog mouse

public class Prog {

public static void main(String args[]) {


System.out.println(args[?]) ;
}
}

A. 0
B. 1
C. 2
D. 3
E. none of these

A.

92. You have the following code:

.......
String s;
s = "Hello";
t = " " + "my";
s.append(t);
s.toLowerCase();
s+= " friend";
System.out.println(s);

What will be printed?

Answer:

hello my friend

93. What will happen when you attempt to compile and run this code?

public class MySwitch{

public static void main(String argv[]) {


MySwitch ms = new MySwitch();
ms.amethod();
}

public void amethod() {

char k=10;

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 55
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
switch(k){
default:
System.out.println("This is the default output");

break;
case 10:
System.out.println("ten");
break;
case 20:
System.out.println("twenty");
break;
}
}
}

A. None of these options


B. Compile time error target of switch must be an integral type
C. Compile and run with output "This is the default output"
D. Compile and run with output "ten"

D.

94. What will happen when you attempt to compile and run the following code?

public class MySwitch{

public static void main(String argv[]) {


MySwitch ms = new MySwitch();
ms.amethod();
}

public void amethod() {

int k=10;
switch (k){
default: //Put the default at the bottom, not here
System.out.println("This is the default output");
break;
case 10:
System.out.println("ten");
case 20:
System.out.println("twenty");
break;
}
}
}

A. None of these options


B. Compile time error target of switch must be an integral type
C. Compile and run with output "This is the default output
D. Compile and run with output "ten"

A.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 56
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
95. Which of the following statements are true?
A. For a given component, events will be processed in the order that the
listeners were
added
B. Using the Adapter approach to event handling means creating blank
method bodies for
all event methods
C. A component may have multiple listeners associated with it
D. Listeners may be removed once added

CD.

Button for instance has the methods addActionListener (ActionListener a) and


removeActionListener(ActionListener a).

96. Which of the following statements are true?


A. Directly subclassing Thread gives you access to more functionality of the
Java
threading capability than using the Runnable interface
B. Using the Runnable interface means you do not have to create an instance
of the
Thread class and can call run directly
C. Both using the Runnable interface and subclassing of Thread require
calling start to
begin execution of a Thread
D. The Runnable interface requires only one method to be implemented, this
method is
called run

CD.

97. If you want subclasses to access, but not to override a superclass member method,
what keyword should precede the name of the superclass method?

Answer:

final

98. If you want a member variable to not be accessible outside the current
class at all,
what keyword should precede the name of the variable when declaring it?

Answer:

private

99. Which of the following are correct methods for initializing the array
"dayhigh" with 7
values?

A. int dayhigh = { 24, 23, 24, 25, 25, 23, 21 };

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 57
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
B. int dayhigh[] = { 24, 23, 24, 25, 25, 23, 21 };
C. int[] dayhigh = { 24, 23, 24, 25, 25, 23, 21 };
D. int dayhigh [] = new int [24, 23, 24, 25, 25, 23, 21];
E. int dayhigh = new [24, 23, 24, 25, 25, 23, 21] ;

BC.

100. Assume that val bas been defined as an int for the code below.

if(val > 4){


System.out.println("Test A");
}

else if(val > 9){


System.out.println("Test B");
}
else System.out.println("Test C");

Which values of val will result in "Test C" being printed:


A. val < 0
B val between 0 and 4
C val between 4 and 9
D val > 9
E val = 0
F no values for val will be satisfactory

ABE.

100. Assume that val bas been defined as an int for the code below.

if(val > 4){


System.out.println("Test A");
}

else if(val > 9){


System.out.println("Test B");
}
else System.out.println("Test C");

Which values of val will result in "Test C" being printed:


A. val < 0
B val between 0 and 4
C val between 4 and 9
D val > 9
E val = 10 or morethen 10
F no values for val will be satisfactory

AB

101. Consider the code below.

void myMethod(){
try{
fragile() ;
}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 58
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
catch(NullPointerException npex){

System.out.println("NullPointerException thrown ");


)
catch(Exception ex){
System.out.println("Exception thrown ");
}
finally{
System.out.println("Done with exceptions ");
}
System.out.println("myMethod is done");
}

What is printed to standard output if fragile() throws an


IllegalArgumentException?

A. "NullPointerException thrown"
B. "Exception thrown"
C. "Done with exceptions"
D. "myMethod is done"
E. Nothing is printed

BCD.

102. A class design requires that a particular member variable must be


accessible for
direct access by classes which are members of the same package. What
should be done
to achieve this?

A. The variable should be marked public


B. The variable should be marked private
C. The variable should be marked protected
D. The variable should have no special access modifier
E. The variable should be marked private and an accessor method provided

D.

103. Which method do you call to run a Thread?

A. start()
B. init()
C. begin()
D. run()

A.

104. Which statements on the <<< call >>> line are valid expressions?

public class SuperClass {

public int x;
int y;

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 59
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
public void m1( int a ) {
}

SuperClass( ) {
}
}

class SubClass extends SuperClass {

private float f;
void m2(int c) {
int x;
return;
}
SubClass() {
}
}

class T {

public static void main( String [] args) {


int i;
float g;
SubClass b = new SubClass( );
<<< calls >>>
}
}

A. b.m2();
B. g=b.f;
C. I=b.x;
D. I=b.y;
E. b.m1(6);
F. g=b.x;

testa.

105. What is the range of a byte?

A. -128–127
B. -32 768 – 32 767
C. -231 – 231 -1
D. -232 – 232 -1

A.

106. What is the range of a byte?

A. -27 – 27 -1
B. -2 15 – 215 -1
C. -231 – 231 -1
D. -263 – 263 -1

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 60
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
A.

107. What would be the result of attempting to compile and run the following
piece of
code?

public class Test {


public static void main (String args[]) {
int x[] = new int[10];
System.out.println("Value is " + x[5]);
}
}

A. The output "Value is 0" is printed.


B. An object of type NullPointerException is thrown.
C. An "illegal array declaration syntax" compiler error occurs.
D. A "possible reference before assignment" compiler error occurs.
E. An object of type ArrayIndexOutOfBoundsException is thrown.

A.

108. Which interface should you use if you want no duplicates, no order and
no particular
retrieval system?

A. Map
B. Set
C. List
D. Collection
E. Enumeration

B.

109. Which are keywords in Java?

A. NULL
B. TRUE
C. sizeof
D. implements
E. synchronized

DE.

110. Consider the code fragment below:

outer: for(int i=0; i < 2; i++){


inner: for(j = 0; j < 2; j++){
if(j==1)
continue outer;
System.out.println("i = " + i ", j = " + j);
}}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 61
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
Which of the following will be printed to standard output?

A. i = 0, j = 0
B. i = 1, j = 0
C. i = 2, j = 0
D. i = 0, j = 1
E. i = 1, j = 1
F. i = 2, j = 1
G. i = 0, j = 2
H. i = 1, j = 2
I. i = 2, j = 2

AB.

111. Consider the code fragment below:

outer: for(int i=1; i < 3; i++){


inner: for(j = 1; j < 3; j++){
if(j==2)
continue outer;
System.out.println("i = " + i ", j = " + j);
}}

Which of the following will be printed to standard output?

A. i = 1, j = 1
B. i = 1, j = 2
C. i = 1, j = 3
D. i = 2, j = 1
E. i = 2, j = 2
F. i = 2, j = 3
G. i = 3, j = 1
H. i = 3, j = 2

AD.

112. What is the modifier for events in EventListeners?

A. public
B. none
C. private
D. .....

B.

113. You want the program to print 3 to the output. Which of the following
values for x
will do this?

Code:

switch(x){

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 62
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

case(1):
System.out.println("1");
case(2):
case(3):
System.out.println("2");
default:
System.out.println("3");
}

A1
B2
C3
D4

ABCD.

114. You want the program to print 3 to the output. Which of the following
values for x
will do this?

Code:

switch(x){

case(1):
System.out.println("1");
case(2):
case(3):
System.out.println("3");
break;
default:
System.out.println("Default");
}

A1
B2
C3
D4

ABC.

115. A question about the GridbagLayout. One alternative.

116. You have been given a design document for an employee system for
implementation
in Java. It states.

An Employee has a vector of bla bla, dates for meetings, number of


dependants (what is
this???)

A. Vector
B. Int

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 63
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
C. Date
D. Object
E. New employee e;

Which variables should you use?

Answer:

A, B, C. (Not sure. Don't remember exactly how the question was posed)

117. You have been given a design document for a polygon system for
implementation in
Java. It states. A polygon is drawable. You must access it. It has a vector and
corners
bla bla

choose between the following words: What will you write when defining the
class?

public
class
Polygon object
extends
Shape
drawable
plus some more

Answer:

public class Polygon implements drawable

118. A question about stacks. Only one correct answer.

A. (Is it possible to have stack objects in a String) Code contained + "" +


stack2 + "" +
B. (Is it possible to write stack1 = stack2;)
C. Prints bla bla bla
D. Prints bla bla
E. Prints bla bla

119. Threads: Which statements are true about threads stopping to execute?

A. All threads in the same class stop at the same time


B. The suspend() method stops the thread so that you can not start it again
C.
D.

C or D.

120. Which statements are true about garbage collection?


A. Garbage collection is predictable.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 64
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
B. You can mark a variable or an object, (telling the system) so that it can be
garbage
collected
C. .....
D. .....

C or D.(not sure)

121. Consider the following code. What will be on the output. No Exception is
thrown.

public class Mock2ExceptionTest{

public static void main(String [] args){

Mock2ExceptionTest e = new Mock2ExceptionTest();


e.trythis();
}

public void trythis(){

try{

System.out.println("1");
problem();
System.out.println("1b");
}

catch(Exception x){
System.out.println("3");

finally{
System.out.println("4");

}
System.out.println("5");
}

public void problem()throws Exception{

//throw not any Exception();


}
}

A. 1
B 1b
C. 3
D. 4
E. 5

ABDE.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 65
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

No exception is thrown and everything except 3 will be printed.

121b. Consider the following code. What will be on the output. No Exception is
thrown.

public class Mock3ExceptionTest{

public static void main(String [] args){

Mock3ExceptionTest e = new Mock3ExceptionTest();


e.trythis();
}

public void trythis(){

try{

System.out.println("1");
problem();
System.out.println("1b");
}

catch(Exception x){
System.out.println("3");

finally{
System.out.println("4");

}
System.out.println("5");
}

public void problem()throws Exception{

throw new Exception();


}
}

A. 1
B 1b
C. 3
D. 4
E. 5

ACDE.

122. What can you put where the X is?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 66
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
Public class A{}

A. import java.awt.*;
B. package bla.bla;
C. class bla{}
D. public abstract final method();
E. public final int x = 1000;

ABC.

123. What are the characteristics of a totally encapsulated class? One answer.

A. methods not private


B. variables not public
C. ......
D. all modifying of the object should be made through methods

D.

124. Consider the following code:

public class TBMock1{

public static void main(String[]args){

Integer n = new Integer(7);


Integer k = new Integer(7);
Long i = new Long(7);
}
}

Which return true?

A. n==i

B. n==k

C. n.equals(k)

D. n.equals(7)

E. n.equals(new Integer(7))

CE.

125. Write 7 in hexadecimal. Do not use more than four characters and do no
assignment.

Answer:

0x7, 0x07

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 67
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
126. Consider the classes defined below:

import java.io.*;

class Super{

void method (int x, int b)


}

class Sub extends Super{}

How will a correct method in Sub look like?

A. int method(int x, int b)


B. void method (int x) throws Exception
C. void anotherMethod(int x)
D. ........

BC.

127. Consider the classes defined below:

import java.io.*;

class Super{

int method1 (int x, long b) throws IOException


{//code }
}

public class Sub extends Super{}

Which of the following are legal method declarations to add to the class Sub?
Assume that
each method is the only one being added.

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


B. float method2(){}
C. long method1 (int c, long d) {}
D. int method1(int c, long d) throws ArithmeticException{}
E. int method1 (int c, long d) throws FileNotFoundException{}

ABE.

128. What does the method getID do?

A. returns a value which shows the nature of the event


B. .......
C. ...
D. .....

A.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 68
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
129. Which object will be created when you implement a KeyListener? Unsure
about how
the question was formulated!!

Answer:

Answered KeyEvent

130 Which of these will create an array that can be used for 50 Strings?

A. char a[][]
B. String a[]
C. String[]a;
D. String a[5];
E. ...

BC. (not sure)

131 How can you declare a legal inner class?

A class x{}
B
C myInterface (String x){
D myInterface () {

132. One Superclass and one Subclass. One was public and one was default.
How do you
create a new instance?? Unsure of the formulation of the question.

A. new Inner()
B. new Outer().new Inner()
C. .....
D. ......

B.

133. Claims about adapters and listeners

134. Which are legal identifiers

A. niceIdentifier
B. 2Goodbajs
C. %hejpådig
D. _goddag
E. Hello2

ADE.

135. Which statement is true about a non-static inner class?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 69
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
A. It must implement an interface.
B. It is accessible from any other class.
C. It can only be instantiated in the enclosing class.
D. It must be final if it is declared in a method scope.
E. It can access any final variables in the enclosing class

E.

136. A question with some wrong code. What must you do to correct. I
changed to the
static modifier.

137. Consider the following code:

String s = "Svenne";
int i = 1;

What can you do?

A String t = s>>i;
B Long x = 12;
String s = s + x;
C. .....
D. ......

B.

138. Assume that val has been defined as an int for the code below.

if(val > 4){


System.out.println("Test A");
}

else if(val > 9){


System.out.println("Test B");
}
else System.out.println("Test C");

Which values of val will result in "Test C" being printed:


A. val < 0
B val between 0 and 4
C val between 4 and 9
D val > 9
E val = 10
F no values for val will be satisfactory

AB.

139. You are going to read some rows one by one from a file that is stored
locally on your
hard drive. How do you do it?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 70
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
A. BufferedReader
B. InputStreamReader
C. One reader with "8859-1" as an argument
D. Don't remember
E. FileReader

E. (Don't remember if E was an alternative.)

140. What is a correct argument list for a public static void main method?

A. (String argv [])


B. (String arg )
C. (String [] fish)
D. (String args)
E. (String args{})

Made up alternatives C-E by myself.

AC.

141. Consider the following code:

What will be printed?

public class AB{

public static void main (String[] args){

int x = 1;

if (0 < x--){

System.out.println(x);
}
}

A. 0
B. 1
C. 2
D. Nothing
E. An IllegalArgumentException will be thrown

A.

141b. Consider the following code:

What will be printed?

public class AB{

public static void main (String[] args){

int x = 1;

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 71
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

if (0 < x--){

System.out.println(x);
}
}

A. 0
B. 1
C. 2
D. Nothing
E. An IllegalArgumentException will be thrown

D.

142. Which of the following are valid definitions of an application's main ( )


method?

A. public static void main( );


B. public static void main( String args );
C. public static void main( String args [] );
D. public static void main( Graphics g );
E. public static boolean main( String args [] );

C.

143. Which of the following are Java keywords?

A array
B boolean
C Integer
D protect
E super

BE.

144. After the declaration:

char[] c = new char[100];

what is the value of c[50]?

A. 50
B. 49
C. '\u0000'
D. '\u0020'
E. ""
F. cannot be determined
G. always null until a value is assigned

C.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 72
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
145. Which identifiers are valid?

A. _xpoints
B. U2
C. blabla$
D set-flow
E. something

ABCE.

146. Represent the number 6 as a hexadecimal literal.

Answer:

0x6, 0x06, 0X6, 0X06

147. Which of the following statements assigns "Hello Java" to the String
variable s?

A. String s = "Hello Java";


B. String s [] = "Hello Java";
C. new String s = "Hello Java";
D. String s = new String ("Hello Java");

AD.

148. An integer, x has a binary value (using 1 byte) of 10011100. What is the
binary value
of z after these statements:

int y = 1 << 7 ;
int z = x & y;

A. 1000 0001
B. 1000 0000
C. 0000 0001
D. 1001 1101
E. 1001 1100

B.

149. The statement ...

String s = "Hello" + "Java";

yields the same value for s as ...

String s = "Hello";
String s2 = "Java";
s.concat( s2 );

A. True

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 73
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
B. False

B.

150. If you compile and execute an application with the following code in its
main()method:

String s = new String("Computer");

if ( s == "Computer" )
System.out.println ("Equal A");
if( s.equals( "Computer" ) )
System.out.println ("Equal B");

A. It will not compile because the String class does not support the = =
operator.
B. It will compile and run, but nothing is printed.
C. "Equal A" is the only thing that is printed.
D. "Equal B" is the only thing that is printed.
E. Both "Equal A" and "Equal B" are printed.

D.

151. Given the variable declarations below:

byte myByte;
int myInt;
long myLong;
char myChar;
float myFloat;
double myDouble;

Which one of the following assignments would need an explicit cast?

A. myInt = myByte;
B. myInt = myLong;
C. myByte = 3;
D. myInt = myChar;
E. myFloat = myDouble;
F. myFloat = 3;
G. my Double = 3.0;

BE.

152. Consider this class example:

class MyPoint {
void myMethod(){
int x, y;
x = 5; y = 3;
System.out.print("(" + x + ", " + y + ")");
switchCoords(x,y);
System.out.print("(" + x + ", " + y + ")");

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 74
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
}

void switchCoords(int x,int y){


int temp;
temp = x;
x = y;
y = temp;
System.out.print("(" + x + ", " + y + ")");
}
}

What is printed to standard output if myMethod() is executed?

A. (5, 3) (5, 3) (5, 3)


B. (5, 3) (3, 5) (3, 5)
C. (5, 3) (3, 5) (5, 3)

C.

153. To declare an array of 31 floating point numbers representing snowfall for


each day
of March in Gnome, Alaska, which declarations would be valid?

A. double snow[] = new double[31];


B. double snow[31] = new array[31];
C. double snow[31] = new array;
D. double[] snow = new double[31];

AD.

154. If arr[] contains only positive integer values, what does this function do?

public int guessWhat(int arr[]){


int x = 0;
for(int i = 0; i < arr.length; i++)
x = x < arr[i] ? arr[i] : x;
return x;
}

A. Returns the index of the highest element in the array


B. Returns true/false if there are any elements that repeat in the array
C. Returns how many even numbers are in the array
D. Returns the highest element in the array
E. Returns the number of question marks in the array

D.

155. Consider the code below:

arr[0] = new int[4];


arr[1] = new int[3];
arr[2] = new int[2];
arr[3] = new int[l];

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 75
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
for(int n = 0; n < 4; n++)
System.out.println ( /*What will work here? */);

Which statement below, when inserted as the body of the for loop, would print
the
number of values in each row?

A. arr[n].length();
B. arr.size;
C. arr.size -1;
D. arr[n] [size] ;
E. arr[n].length;

E.

156. Which of the following are legal declarations of a two-dimensional array


of integers?

A. int[5][5] a = new int[][];


B. int a = new int[5,5];
C. int[]a[] = new int [5][5];
D. int[][]a = new [5]int[5];

C.

157. Given the variables defined below:

int one = 1;
int two = 2;
char initial = '2';
boolean flag = true;

Which of the following are valid?


A. if ( one ){}
B. if( one = two ){}
C. if( one == two ) {}
D. if( flag ){}
E. switch ( one ) { }
F. switch ( flag ) { }
G. switch ( initial ) { }

CDEG.

158. If val = 1 in the code below:

switch (val){
case 1: System.out.println("P");
case 2:
case 3: System.out.println("Q") ;
break;
case 4: System.out.println("R");
default: System.out.println ("S");
}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 76
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

A. P
B. Q
C. R
D. S

AB.

159. What exception might a wait() method throw?

Answer:

InterruptedException

160. For the code:

m = 0;
while( m++ < 2 )
System.out.println(m);

Which of the following are printed to standard output?

A. 0
B. 1
C. 2
D. 3
E. Nothing and an exception is thrown

BC.

161. For the code:

m = 0;
while( ++m < 2 )
System.out.println(m);

Which of the following are printed to standard output?

A. 0
B. 1
C. 2
D. 3
E. Nothing and an exception is thrown

B.

162. Consider the following code sample:

class Tree{}
class Pine extends Tree{}
class Oak extends Tree{}
public class Forest {

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 77
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
public static void main (String [] args){
Tree tree = new Pine():

if( tree instanceof Pine )


System.out.println ("Pine");

if( tree instanceof Tree )


System.out.println ("Tree");

if( tree instanceof Oak )


System.out.println ( "Oak" );

else
System.out.println ("Oops ");
}
}

Select all choices that will be printed:

A. Pine
B. Tree
C. Forest
D. Oops
E. Nothing will be printed

ABD.

163. Which of the following statements about Java's garbage collection are
true?

A. The garbage collector can be invoked explicitly using a Runtime object.


B. The finalize method is always called before an object is garbage collected.
C. Any class that includes a finalize method should invoke its superclass'
finalize method.
D. Garbage collection behaviour is very predictable.

BC.

164. What line of code would begin execution of a thread named myThread?

Answer:

myThread.start();

165. Which methods are required to implement the interface Runnable?

A. wait()
B. run()
C. stop()
D. update()
E. resume()

B.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 78
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

166. What class defines the wait() method?

Answer:

Object.

for yield(), sleep(#), start(), run() it is Thread


for wait(), notify(), notifyAll() it is Object

167. For what reasons might a thread stop execution?

A. A thread with higher priority began execution.


B. The thread's wait() method was invoked.
C. The thread invoked its yield() method.
D. The thread's pause() method was invoked.
E. The thread's sleep() method was invoked.

ABCE.

168. Which method below can change a String object, s ?

A. equals( s )
B. substring( s )
C. concat( s )
D. toUpperCase ( s )
E. none of the above will change s

E.

169. If s1 is declared as:

String sl = "phenobarbital";

What will be the value of s2 after the following line of code:

String s2 = s1.substring( 3, 5 );

A. null
B. "eno"
C. "enoba"
D. "no"

D.

170. What method(s) from the java.lang.Math class might method() be if the
statement
method( -4.4 )== -4; is true.

A. round()
B. min()

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 79
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
C. trunc()
D. abs()
E. floor()
F. ceil()

AF.

171. Which methods does java.lang.Math include for trigonometric


computations?

A. sin( )
B. cos( )
C. tan ( )
D. aSin ( )
E. Cos ( )
F. aTan( )
G. toDegree ( )

ABC.

172. This piece of code:

TextArea ta = new TextArea ( 10, 3 );

Produces (select all correct statements):

A. a TextArea with 10 rows and up to 3 columns


B. a TextArea with a variable number of columns not less than 10 and 3 rows
C. a TextArea that may not contain more than 30 characters
D. a TextArea that can be edited

AD.

173. In the list below, which subclass(es) of Component cannot be directly


instantiated:

A. Panel
B. Dialog
C. Container
D. Frame

C.

174. Of the five Component methods listed below, only one is also a method
of the class
MenuItem. Which one?

A. setVisible (boolean b)
B. setEnabled (boolean b)
C. getSize ()
D. setForeground (Color c)
E. setBackground (Color c)

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 80
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

B.

175. If a font with variable width is used to construct the string text for a
column, the
initial size of the column is:

A. determined by the number of characters in the string, multiplied by the width


of a
character in this font
B. determined by the number of characters in the string, multiplied by the
average width
of a character in this font
C. exclusively determined by the number of characters in the string
D. undetermined

B.

176. Which of the following methods from the java.awt.Graphics class could
be used to
draw the outline of a rectangle with a single method call? Select all.

A. fillRect()
B. drawRect()
C. fillPolygon()
D. drawPolygon()
E. drawLine()

BD.

177. Of the following AWT classes, which one(s) are responsible for
implementing the
components layout? Select all.

A. LayoutManager
B. GridBagLayout
C. ActionListener
D. WindowAdapter
E. FlowLayout

BE.

178. A component that should resize vertically but not horizontally should be
placed in:

A. BorderLayout in the North or South location


B. FlowLayout as the first component
C. BorderLayout in the East or West location
D. BorderLayout in the Center location
E. GridLayout

C.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 81
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

179. What type of object is the parameter for all methods of the MouseListener
interface?

Answer:

MouseEvent

180. What type of object is the parameter for all methods of the
MouseMotionListener
interface?

Answer:

MouseEvent

181. What type of object is the parameter for all methods of the KeyListener
interface?

Answer:

KeyEvent

182. What type of object is the parameter for all methods of the ActionListener
interface?

Answer:

ActionEvent

183. Which of the following statements about event handling in JDK 1.1 and
later are true?
Select all.

A. A class can implement multiple listener interfaces


B. If a class implements a listener interface, it only has to overload the
methods it uses
C. All of the MouseMotionAdapter class methods have a void return type

AC.

184. Which of the following describe the sequence of method calls that result
in a
component being redrawn?

A. invoke paint() directly


B. invoke update which calls paint()
C. invoke repaint() which チ
D. invoke repaint() which invokes paint() directly

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 82
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
C.

185. Choose all valid forms of the argument list for the FileOutputStream
constructor
shown below:

A. FileOutputStream(FileDescriptor fd)
B. FileOutputStream(String n, boolean b)
C. FileOutputStream(boolean a)
D. FileOutputStream()
E. FileOutputStream(File f)

ABE.

186. A "mode" argument such as "r" or "rw" is required in the constructor for
the
class(es):

A. DataInputStream
B. InputStream
C. RandomAccessFile
D. File
E. None of the above

C.

187. A directory can be created using a method from the class(es):

A. File
B. DataOutput
C. Directory
D. FileDescriptor
E. FileOutputStream

A.

188. If raf is a RandomAccessFile, what is the result of compiling and


executing the
following code?

raf.seek(raf.length());

A. The code will not compile.


B. An IOException will be thrown.
C. The file pointer will be positioned immediately before the last character of
the file.
D. The file pointer will be positioned immediately after the last character of the
file.

D.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 83
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
189. Consider the following code: What will be printed?

public class ExceptionTest{

public static void main(String [] args){

ExceptionTest e = new ExceptionTest();


e.trythis();
}

public void trythis(){

try{

System.out.println("1");
problem();
}

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");
}

public void problem()throws Exception{

throw new Exception();


}
}

1
2
3
4
5

ACD.

190. Consider the following code: What will be printed?

public class ExceptionTest2{

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 84
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
public static void main(String[] args){

ExceptionTest2 e = new ExceptionTest2();


e.divide(4, 0);}

public void divide(int a, int b){

try{
int c = a/b;
}
catch(ArithmeticException e){

System.out.println("ArithmeticException");
}

catch(RuntimeException e){

System.out.println("RuntimeException");
}

catch(Exception e){

System.out.println("Exception");
}

finally{

System.out.println("Finally");
}
}
}

ArithmeticException
RuntimeException
Exception
Finally

A. ArithmeticException D. Finally.

191. Consider the following code: What will be printed?

public class ExceptionTestMindQ{

public static void main(String [] args){

ExceptionTestMindQ e = new ExceptionTestMindQ();


e.trythis();
}

public void trythis(){

try{

System.out.println("Innan testet");

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 85
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
problem();
}

catch (NullPointerException x){

System.out.println("Nullpointer");

catch(Exception x){
System.out.println("Exception");
return;
}

finally{
System.out.println("finally");
}
System.out.println("mymethod is done");
}

public void problem()throws IllegalArgumentException{

throw new IllegalArgumentException();


}
}

Innan testet
Nullpointer´
Exception
finally
mymethod is done

A. Innan testet C. exception D. finally.

192. Consider the following code: What will be printed?

public class exceptiontest101{

public static void main(String[]args){

exceptiontest101 e = new exceptiontest101();


e.myMethod();
}
void myMethod(){
try{
fragile() ;
}
catch(NullPointerException npex){

System.out.println("NullPointerException thrown ");


}
catch(Exception ex){
System.out.println("Exception thrown ");
}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 86
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
finally{
System.out.println("Done with exceptions ");
}
System.out.println("myMethod is done");
}

public void fragile() {


throw new IllegalArgumentException();
}
}

NullpointerException thrown
Exception thrown
Done with exceptions
D.myMethod is done

B. Exception thrown C. Done with exceptions D. myMethod is done

193. What is the range of a short?

A. –128 – 127
B. -32 768 – 32 767
C. -231 – 231 -1
D. -263 – 263 -1

B.

194. What is the range of a short?

-128 – 127
-231 – 231 –1
C. -215 – 215 -1
D. -263 – 263 -1

C.

195. What is the range of a long?

-128 – 127
-231 – 231 –1
C. -215 – 215 -1
D. -263 – 263 -1

D.

196. Consider the following code: What will be printed?

public class Access{

int i = 10;
int j;
char z = 823;

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 87
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
char q = '1';
boolean b;
static int k = 9;

public static void main(String arg[]){

Access a = new Access();


a.amethod();
System.out.println(k);
}

public void amethod(){

System.out.println(j);
System.out.println(b);
System.out.println(z);
System.out.println(q);
}

0 true 823 1 9
null false 823 1 9
false 823 1 9 0
0 false ? 1 9
true 823 1 9 0

D.

197. Consider the following code: What will be printed?

public class amethod{

public static void main(String arg[]){

String s = "Hello";
char c ='H';
s+=c;
System.out.println(s);

A. Nothing will be printed because of a compilation error.


B. HelloH
C. Hhello
D. Hello\u345
Hello

B.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 88
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
198. Consider the following code: What will be printed?

public class Arg{


String [] MyArg;
public static void main(String arg[]){
MyArg[] = arg[];
}
public void amethod(){
System.out.println(arg[1]);
}
}
null
0
Nothing will be printed. Compilation error.
Compiles just fine, but a RuntimeException will be thrown.

C.

199. Consider the following code: What will be printed?


public class Arg2{
static String [] MyArg = new String[2];
public static void main(String arg2[]){
arg2 = MyArg;
System.out.println(arg2[1]);
}

null
0
Nothing will be printed. Compilation error.
Compiles just fine, but a RuntimeException will be thrown.

A.

200. Consider the following code: What will be printed?

public class Arraytest{


public static void main(String kyckling[]){
Arraytest a = new Arraytest();
int i[] = new int[5];
System.out.println(i[4]);
a.amethod();
Object o[] = new Object[5];
System.out.println(o[2]);

void amethod(){
int K[] = new int[4];
System.out.println(K[3]);
}
}

A. null null null

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 89
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
B. null 0 0
C. 0 0 null
D. 0 null 0

C.

201. Consider the following code: What will be printed?

class Arraytest2{

public static void main(String[]args){


int [] arr = {1, 2, 3};
for(int i = 0; i < 2; i++){
arr[i] = 0;
}

for(int i = 0; i < 3; i++){


System.out.println(arr [i]);
}
}
}

123
003
023
000

B.

202. Consider the following code: What will be printed?

public class ArrayTest3{

public static void main(String[]args){

int [][] a = new int[5][5];


System.out.println(a[4][4]);
}
}

0000
00
0
0000000000000000

C.

203. Consider the following code: What will be printed?

public class booleanFlag{


public static void main(String[] args){
test();

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 90
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
test2();
}

public static void test(){

//boolean flag = false;

if(flag=true){

System.out.println("true");

else {

System.out.println("false");
}}

public static void test2(){


boolean flag = false;
if(flag==true){

System.out.println("true");
}

else {
System.out.println("false");
}
}
}

true true
true false
false true
false false
Compilation error. Cannot resolve symbol: variable flag.

E.

204. Consider the following code: What will be printed?


class CeilTest{
static float k = 3.2f;
public static void main(String[]args){
System.out.println("Ceil for 3.2 is: " + Math.ceil(k) + "Floor for 3.2 is: " +
Math.floor(k));
}
}

Ceil for 3.2 is : 4.0 Floor for 3.2 is : 3.0


Ceil for 3.2 is : 3.0 Floor for 3.2 is : 4.0
Ceil for 3.2 is : 4 Floor for 3.2 is : 3
Ceil for 3.2 is : 3 Floor for 3.2 is : 4
Compiler error. Variable k cannot be reached.

A.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 91
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

205. Consider the following code: What will be printed?

public class DoubleTest{


public static void main(String[]args){
Float i = new Float(0.9f);
Float j = new Float(0.9f);
if(i.equals(j))
System.out.println("i.equals(j)");
if(i==j)
System.out.println("i==j");
float s = 10.0f;
int t = 10;
long x = 10;
char u = 10;
if(s==t)
System.out.println("s==t");
if(x==u)
System.out.println("x==u");
}}

i.equals(j) i==j s==t x==u


B. i==j s==t x==u
C. i.equals(j) s==t x==u
i.equals(j) x==u

C. What makes the difference here is the new operator. You get fooled and
think that s =
= t and x = = u would return false.

206. Consider the following code: What will be printed?

public class Equal{

public static void main(String kyckling[]){

int Output = 10;

boolean b1 = false;

if((b1==true) && ((Output+=10)==20))


{
System.out.println("We are equal " + Output);
}
else
{System.out.println("Not equal! " + Output);
}
}}

A. Nothing will be printed. You must use the word args instead of kyckling in
the main
method.
We are equal 10
Not equal 10

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 92
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
We are equal 20
Not equal 20

C.

207. Consider the following code: What will be printed?

public class EqualsTest{


public static void main(String []args){
if ("john" == "john")
System.out.println("\"john\" == \"john\"");
if("john".equals("john"));
System.out.println("\"john\".equals(\"john\")");
Boolean flag = new Boolean(true);
boolean flagga = true;
}
}

john == john
"john" == "john" "john".equals "john"
john == john john.equals john
john.equals john
"john" == "john"
"john".equals "john"

B.

208. Consider the following code: What will be printed?

public class EqualsTest2{

public static void main(String[]args){


byte A = (byte)4096;

if (A == 4096)
System.out.println("Equal");

else System.out.println("Not Equal");

System.out.println(A);

int B = (int)4096;

if (B == 4096)
System.out.println("Equal");

else System.out.println("Not Equal");

System.out.println(A);

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 93
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
Not Equal Not Equal 0
Not Equal Equal 0
Equal Not Equal 4096
Equal Equal 4096

B.

A byte can store values from –128-127. Therefore the variable A will not be
able to store
4096.

209. Consider the following code: What will be printed?

class ExampleInteger extends Object{

public static void main(String[] args){

ExampleInteger e = new ExampleInteger();


e.Result(30);
}

public void Increment(Integer N){


N = new Integer(N.intValue() + 1);
}

public void Result(int x){

Integer X = new Integer(x);


Increment(X);
System.out.println("New value is " + X);
}
}

New value is 30
New value is 31
New value is 1
New value is null

A.

210. Consider the following code: What will be printed?

public class Hope{


public static void main(String[]args)
{
Hope h = new Hope();
}
protected Hope(){
for(int i = 0; i < 10; i++){

System.out.println(i);}

}}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 94
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
0123456789
Compiler error. Constructor cannot be protected.
1 2 3 4 5 6 7 8 9 10

211. Consider the following code: What will be printed?


public class Init{
public static void main(String arg[]){
int[]a = new int[5];
String s[] = new String[5];
for (int i = 0; i System.out.println(a[1]);
}
System.out.println(s[3]);
}
}

0 null 0 null 0 null 0 null 0 null


0 0 0 0 0 null null null null null
0 null
0 0 0 0 0 null
Nothing will be printed due to Compilation Error.

D.

212. Consider the following code: What will be printed?

public class Init2{


public static void main(String arg[]){
int[]a = new int[5];
String s[] = new String[5];
String t[];
String u;
System.out.println(a[1]);
System.out.println(s[3]);
System.out.println(t[3]);
System.out.println(u);
}

0 null null null


0 0 0 0 null null null null
null null null null
null 0 0 0
Nothing will be printed: Variable t and u may not have been initialized.

E.

213. Consider the following code: What will happen when you try to compile it?

public class InnerClass{


public static void main(String[]args)

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 95
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
{}
public class MyInner{
}}

It will compile fine.


It will not compile, because you cannot have a public inner class.
It will compile fine, but you cannot add any methods, because then it will fail to
compile.

A.

214. Consider the following code: What will be printed?

public class charTest2{


public static void main(String[] args){
int y = 020;
char b = '\u0010';
if(b==y)
System.out.println("Char 'u0010' = 16.0");
}
}

A. Nothing will be printed.


B. Compilation error. The char variable cannot be assigned this way.
C. Char 'u0010' = 16.0
Compilation error. The int variable cannot be assigned this way.
Compilation error. The char variable cannot be assigned this way. The int
variable cannot
be assigned this way.

C.

215. If you're stupid and program like this. What will happen?

public class LoseInformationCast2{

public static void main(String[]args){

byte b = (byte)259;
short s = (short)b;

System.out.println("short s =" + s + " byte b = " + b);

}
}

short s = 3 + byte b = 3
short s = 3 byte b = 3
short s = 3 byte b = 259
Nothing will be printed. Illegal explicit cast.

B.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 96
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

216. If you're stupid and program like this. What will happen?
public class LoseInformationCast{
public static void main(String[]args){
short s = 259;
byte b = (byte)s;
System.out.println("short s =" + s + "= byte b = " + b);

}
}

short s = 259 + byte b = 3


short s = 259 = byte b = 3
short s = 3 byte b = 259
Nothing will be printed. Illegal explicit cast.

B.

217. Consider the following code: What will be printed?


public class maze{
public static void main(String[]args){
int x = 4;
if(x<=7){
if(x==5)
System.out.println("2");
System.out.println("1");
}

else if (x==17)
System.out.println("3");

else if (x==18)
System.out.println("4");
System.out.println("0");

Nothing will be printed.


0
10
Compilation Error. You can not use two else if statements in this way.
21340

C.

218. Consider the following code: What will be printed?


public class maze2{
public static void main(String[]args){
int x= 4;
if(x<=7){
if(x==5){

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 97
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
System.out.println("2");
System.out.println("1");
}
}

else if (x==17)
System.out.println("3");
else if (x==18){
System.out.println("4");
System.out.println("0");
}

Nothing will be printed.


0
10
Compilation Error. You can not use two else if statements in this way.
21340

A.

219. Consider the following code: What will be printed?


public class maze3{
public static void main(String[]args){
int x= 4;
if(x<=7){
if(x==5){
System.out.println("2");
System.out.println("1");
}
}
else if (x==17)
System.out.println("3");
else if (x==18){
System.out.println("4");
}
System.out.println("0");

Nothing will be printed.


0
10
Compilation Error. You can not use two else if statements in this way.
21340

B.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 98
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

220. Consider the following code: What will be printed?


public class MyAr{
public static void main(String args[]){
MyAr m = new MyAr();
m.amethod();
}
public void amethod(){
int i = 12;
System.out.println(i);
}
}

12
null
Error you cannot initialize a non-static variable in the main method.
0

A.

221. Consider the following code: What will be printed?


public class MyAr{
public static void main(String args[]){
MyAr m = new MyAr();
m.amethod();
}

public void amethod(){


static int i; System.out.println(i);
}
}

A. 1
null
Syntax error: illegal start of expression. You cannot define an integer as static
inside a
non-static method
0
Syntax error: illegal start of expression. Error you cannot define an integer as
non-static
inside a static method.

C.

222. Consider the following code: What will be printed?


public class MyAr{
public static void main(String args[]){
MyAr m = new MyAr();
m.amethod();
}
public void amethod(){
int i;
System.out.println(i);

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 99
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
}
}

A. 1
null
Error. Variable i might not have been initialized.
0
Error you cannot define an integer as non-static inside a static method.

C.

223. Consider the following code: What will be printed?


public class newIntegerLong{
public static void main(String[]args){
Integer nA = new Integer(4096);
Long nB = new Long(4096);
if(nA.equals(nB))
System.out.println("LongEqualsInteger.");
if(nA.intValue() == nB.longValue()){
System.out.println("If you create new primitive values of Long(4096) and
Integer(4096),
then == true.");

}}}

LongEqualsInteger.
If you create new primitive values of Long(4096) and Integer(4096), then ==
true.
LongEqualsInteger. If you create new primitive values of Long(4096) and
Integer(4096),
then == true.
If you create new primitive values of Long(4096) and Integer(4096), then ==
true.
Nothing will be printed.

B.

224. Consider the following code: What will be printed?

public class Q{
public static void main(String arg[]){
int anar[] = new int[]{1,2,3};
System.out.println(anar[1]);
int i = 9;
switch(i){
default:
System.out.println("default");
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
case 2:
System.out.println("two");

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 100
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
}
boolean b=true;
boolean b2 = true;
if (b==b2){
System.out.println("So true");
}
}
}

2 default so true
2 default zero so true
1 default zero so true
0 default so true
2 default zero two
2 default zero two so true

B.

225. Consider the following code: What will be printed?


public class Scope{
private int i;
public static void main(String arg[]){
Scope s = new Scope();
s.amethod();
}

public static void amethod(){


System.out.println(i);
}

0
null
Error. Non-static variable i cannot be referenced from a static context.
Error. Variable I may not have been initialized.

C.

Consider the following code: What will be printed?


public class StringBuf{
public static void main(String args[]){
StringBuffer sb = new StringBuffer("abc ");
String s = new String(" a b c");
String st = "ABCDE";
sb.append("def ");//nu är s = abc def
sb.insert(1, " zzz ");//nu är s = a zzz bc def
String i = s.concat(st);
s = s.trim();
System.out.println(s);
System.out.println(sb);
System.out.println(st.indexOf("C"));
System.out.println(st.indexOf('A'));

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 101
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
System.out.println(st.indexOf('A', 2));
System.out.println(st.indexOf('G'));
System.out.println(i);
}
}

What will be printed?

abc
azzz bc def
2
0
-1
-1
a b cABCDE

abc
abc def zzz
2
0
-1
-1
a b c ABCDE

C.
abc
abcdefzzz
2
0
1
-1
a b c ABCDE

D.
abc
a zzz bc def
2
0
-1
-1
a b cABCDE

D.

227. Consider the following code: What will be printed?

public class StringBufferTest{

public static void main(String[]args){

StringBuffer sb1 = new StringBuffer("Anna");


StringBuffer sb2 = new StringBuffer("Anna");

if (sb1.equals(sb2))

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 102
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

System.out.println("Equals");

else

System.out.println("StringBuffer Equals not");

String s1 = new String("Anna");


String s2 = new String("Anna");

if (s1.equals(s2))

System.out.println("String Equals");

else

System.out.println("Equals not");
}
}

Equals
Equals not

StringBuffer Equals not


String Equals

Equals
String Equals
D.
StringBuffer Equals not
Equals not

B.

228. Consider the following code: What will be printed?

public class StringBufferTest2{


public static void main(String[]args){
String a, b;
StringBuffer c, d;
c = new StringBuffer("Kalle");
a = new String("Kalle");
b = a;
d = c;
StringBuffer e = new StringBuffer("Kalle");
String f = new String("Kalle");
if(b.equals(a))
System.out.println("b.equals(a)");
if(b==a)
System.out.println("b==a");
if(d.equals(c))
System.out.println("d.equals(c)");
if(d==c)
System.out.println("d==c");
if(f.equals(a))

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 103
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
System.out.println("f.equals(a)");
if(f==a)
System.out.println("f==a");
else
System.out.println("f!=a");
if(e.equals(c))
System.out.println("e.equals(c)");
else
System.out.println("e.equalsnot(c)");
if(e==c)
System.out.println("e==c");
else
System.out.println("e!=c");
}
}

A.
b.equals(a)
b==a
d.equals(c)
d==c
f.equals(a)
f!=a
e.equalsnot(c)
e!=c

B.
b.equals(a)
b==a
d.equals(c)
d==c
f!=a
e.equalsnot(c)
e!=c

C.
b.equals(a)
d.equals(c)
f.equals(a)
f==a
e.equals(c)
e!=c

D.
b==a
d==c
f.equals(a)
f!=a
e.equalsnot(c)
e==c

E.
b.equals(a)
b==a
d.equals(c)
d==c

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 104
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
f.equals(a)
e.equals(c)
e!=c

A.

229. Consider the following code: What will be printed?


public class StringTest{
public static void main(String[] args){
String s = "Kalle och Matte";
int i = s.length();
int j = args.length;
System.out.println(i + " " + j);
}}

13 13
13 0
15 0
15 13
15 null
15 + + 0
13 + + 0

C.

230. Check and run this code and you will see how the round method in the
Java.lang.math class will behave when you have –4.5 and 4.5. It will round up.
You will
also see how substring(); floor(); and ceil(); behave.
import java.lang.Math;
public class Substr{
public static void main(String[]args){
String s1 = ("Kalle Bengtsson");
String s2 = s1.substring(0,5);
System.out.println(s2);
s1 = ("phenobarbital");
s2 = s1.substring(3,5);
System.out.println(s2);

long i = Math.round(-4.4);
System.out.println("Math.round(-4.4) = " + i);

long j = Math.round(4.5);
System.out.println("Math.round(4.5) = " + j);

long k = Math.round(-4.5);
System.out.println("Math.round(-4.5) = " + k);

int l = (int)(Math.ceil(-4.4));
System.out.println("Math.ceil(-4.4) = " + l);

int m = (int)Math.ceil(-4.5);
System.out.println("Math.ceil(-4.5) = " + m);

int n = (int)Math.ceil(4.4);
System.out.println("Math.ceil(4.4) = " + n);

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 105
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

int o = (int)Math.floor(-4.4);
System.out.println("Math.floor(-4.4) = " + o);

int p = (int)Math.floor(4.4);
System.out.println("Math.floor(4.4) = " + p);

int q = (int)Math.floor(4.5);
System.out.println("Math.floor(4.5) = " + q);
}
}

231. Consider the following code: What will be printed?


public class Testa
{
Integer a = 10;
Integer b = 20;
Integer c = 10;
public static void main (String[] args){
if (a==c){
System.out.println("Fel");
}

if (a.equals(c)){
System.out.println("rätt");
}
}
}

Fel rätt
rätt
Fel
None will be printed. A NullPointerException will be thrown.
None will be printed. Compilation error. Incompatible types and a non-static
variable
cannot be referenced from a static context.

Consider the following code: What will be printed?

public class Testb


{
static Integer a = 10;
static Integer b = 20;
static Integer c = 10;
public static void main (String[] args){

if (a==c){
System.out.println("Fel");
}

if (a.equals(c)){
System.out.println("rätt");
}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 106
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
}
}

Fel rätt
rätt
Fel
None will be printed. A NullPointerException will be thrown.
None will be printed. Compilation error. Incompatible types and a non-static
variable
cannot be referenced from a static context.
None will be printed. Compilation error. Incompatible types.

F.

232. Consider the following code: What will be printed?

public class Testc


{

static Integer a = new Integer(10);


static Integer b;
static Integer c = new Integer(10);

public static void main (String[] args){

if (a==c){
System.out.println("Fel");
}

if (a.equals(c)){
System.out.println("rätt");
}

}
}

Fel rätt
rätt
Fel
None will be printed. A NullPointerException will be thrown.
None will be printed. Compilation error. Incompatible types and a non-static
variable
cannot be referenced from a static context.
None will be printed. Compilation error. Incompatible types.

B.

233. Consider the following code: What will be printed?

class Unchecked{
public static void main(String[]args){
try{
method();
}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 107
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
catch (Exception e) {
System.out.println("Fångar Exception");
}
}
static void method(){
try{
method1();
System.out.println("Testar method1");
}

catch (ArithmeticException ae) {


System.out.println("Fångar ArithmeticException");
}
finally{
System.out.println("Kör finally");
}
System.out.println("I method() utanför finally");
}

static void wrench(){


throw new NullPointerException();
}
}

A. Kör finally. I method() utanför finally. Fångar Exception.


Kör finally. Fångar Exception.
None.
Kör finally.

B.

234. Consider the following code: What will be printed?

class Unchecked1{
public static void main(String[]args){
}
void method(){
try{
metod1();
System.out.println("Testar metod1");
}
catch (ArithmeticException ae) {
System.out.println("Fångar ArithmeticException");
}
finally{
System.out.println("Kör finally");
}
System.out.println("I method() utanför finally");
}

void metod1(){
throw new NullPointerException();
}
}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 108
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

Kör finally. I method() utanför finally. Fångar Exception.


Kör finally. Fångar Exception.
None.
Kör finally.

C. (Strange but that´s it).

Originally created by Thomas Leuthard.

Updated by Sasa.

Kept alive by you.

1. Which statement are characteristics of the >> and >>> operators.

A. >> performs a shift


B. >> performs a rotate
C. >> performs a signed and >>> performs an unsigned shift
D. >> performs an unsigned and >>> performs a signed shift
E. >> should be used on integrals and >>> should be used on floating point
types

C.

2. Given the following declaration

String s = "Example";

Which are legal code?

A. s >>> = 3;
B. s[3] = "x";
C. int i = s.length();
D. String t = "For " + s;
E. s = s + 10;

CDE.

3. Given the following declaration

String s = "hello";

Which are legal code?

A. s >> = 2;
B. char c = s[3];
C. s += "there";
D. int i = s.length();
E. s = s + 3;

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 109
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

CDE.

4. Which statements are true about listeners?

A. The return value from a listener is of boolean type.


B. Most components allow multiple listeners to be added.
C. A copy of the original event is passed into a listener method.
D. If multiple listeners are added to a single component, they all must all be
friends to
each other.
E. If the multiple listeners are added to a single component, the order [in which
listeners
are called is guaranteed].

BC.

5. What might cause the current thread to stop executing.

A. An InterruptedException is thrown.
B. The thread executes a wait() call.
C. The thread constructs a new Thread.
D. A thread of higher priority becomes ready.
E. The thread executes a waitforID() call on a MediaTracker.

ABDE.

6. Given the following incomplete method.

1. public void method(){


2.
3. if (someTestFails()){
4.
5. }
6.
7.}

You want to make this method throw an IOException if, and only if, the method
someTestFails() returns a value of true.
Which changes achieve this?

A. Add at line 2: IOException e;


B. Add at line 4: throw e;
C. Add at line 4: throw new IOException();
D. Add at line 6: throw new IOException();
E. Modify the method declaration to indicate that an object of [type]
Exception might be thrown.

DE. (E suppose they mean the method declaration for someTestFails.)

7. Which modifier should be applied to a method for the lock of the object this
to be

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 110
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
obtained prior to executing any of the method body?

A. final
B. static
C. abstract
D. protected
E. synchronized

E.

8. Which are keywords in Java?

A. NULL
B. true
C. sizeof
D. implements
E. instanceof

DE.

9. Consider the following code:

Integer s = new Integer(9);


Integer t = new Integer(9);
Long u = new Long(9);

Which test would return true?

A. (s==u)
B. (s==t)
C. (s.equals(t))
D. (s.equals(9))
E. (s.equals(new Integer(9))

CE.

10. Why would a responsible Java programmer want to use a nested class?

Don't know the answers. But here are some reasons from Exam Cram.

To keep the code for a very specialized class in close association with the
class it works
with.
To support a new user interface that generates custom events.
To impress the boss with his/her knowledge of Java by using nested classes
all over the
place.

AB.

11. You have the following code. Which numbers will cause "Test2" to be
printed?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 111
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

switch(x){

case 1:
System.out.println("Test1");
case 2:

case 3:
System.out.println("Test2");
break;
}
System.out.println("Test3");
}

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

BCD.

12. Which statement declares a variable a which is suitable for referring to an


array of 50
string objects?

A. char a[][];
B. String a[];
C. String []a;
D. Object a[50];
E. String a[50);
F. Object a[];

BCF.

13. What should you use to position a Button within an application frame so
that the
width of the Button is affected by the Frame size but the height is not affected.

A. FlowLayout
B. GridLayout
C. Center area of a BorderLayout
D. East or West of a BorderLayout
E. North or South of a BorderLayout

E.

14. What might cause the current thread to stop executing?

A. An InterruptedException is thrown
B. The thread executes a sleep() call

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 112
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
C. The thread constructs a new Thread
D. A thread of higher priority becomes ready (runnable)
E. The thread executes a read() call on an InputStream

ABDE.

Non-runnable states:

* Suspended: caused by suspend(), waits for resume()


* Sleeping: caused by sleep(), waits for timeout
* Blocked: caused by various I/O calls or by failing to get a monitor's lock,
waits for I/O or
for the monitor's lock
* Waiting: caused by wait(), waits for notify() or notifyAll()
* Dead: Caused by stop() or returning from run(), no way out

From Certification Study Guide p. 227

15. Consider the following code:

String s = null;

Which code fragments cause an object of type NullPointerException to be


thrown?

A. if((s!=null) & (s.length()>0))


B. if((s!=null) &&(s.length()>0))
C. if((s==null) | (s.length()==0))
D. if((s==null) || (s.length()==0))

AC.

16. Consider the following code:

String s = null;

Which code fragments cause an object of type NullPointerException to be


thrown?

A. if((s==null) & (s.length()>0))


B. if((s==null) &&(s.length()>0))
C. if((s!=null) | (s.length()==0))
D. if((s!=null) || (s.length()==0))

ABCD.

17. Which statement is true about an inner class?

A. It must be anonymous
B. It can not implement an interface
C. It is only accessible in the enclosing class
D. It can only be instantiated in the enclosing class

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 113
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
E. It can access any final variables in any enclosing scope.

E.

18. Which statements are true about threads?

A. Threads created from the same class all finish together


B. A thread can be created only by subclassing Java.Lang.Thread.
C. Invoking the suspend() stops a thread so that it cannot be restarted
D. The Java Interpreter's natural exit occurs when no non daemon threads
remain alive
E. Uncoordinated changes to shared data by multiple threads may result in the
data being
read, or left, in an inconsistent state.

DE.

19. Consider the following code:

1. public void method(String s){


2. String a,b;
3. a = new String("Hello");
4. b = new String("Goodbye");
5. System.out.println(a + b);
6. a = null;
7. a = b;
8. System.out.println(a + b);
9. }

Where is it possible that the garbage collector will run the first time?

A. Just before line 5


B. Just before line 6
C. Just before line 7
D. Just before line 8
E. Never in this method

C.

20. Which code fragments would correctly identify the number of arguments
passed via
the command line to a Java application, excluding the name of the class that is
being
invoked?

A. int count = args.length;


B. int count = args.length - 1;
C. int count = 0;
while (args[count] != null)
count ++;
D. int count = 0;
while (!(args[count].equals("")))
count ++;

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 114
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

A.

21. Which are keywords in Java?

A. sizeof
B. abstract
C. native
D. NULL
E. BOOLEAN

BC.

22. Which are correct class declarations? Assume in each case text
constitutes the entire
contents of a file called Fred.java on a system with a case-significant system.

A. public class Fred {


public int x = 0;
public Fred (int x) {
this.x = x;
}
}

B. public class fred {


public int x = 0;
public fred (int x) {
this.x = x;
}
}

C. public class Fred extends MyBaseClass, MyOtherBaseClass {


public int x = 0;
public Fred (int xval) {
x = xval;
}
}

D. protected class Fred {


private int x = 0;
private Fred (int xval) {
x = xval;
}
}

E. import java.awt.*;
public class Fred extends Object {
int x;
private Fred (int xval) {
x = xval;
}
}

AE.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 115
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

B is wrong because of case-sensitivity. C is wrong because multiple


inheritance is not
supported in Java. D is wrong because a class can only be public, abstract,
final or default
(with no access modifier).

23. Which are correct class declarations? Assume in each case text
constitutes the entire
contents of a file called Test.java on a system with a case-significant system.

A. public class Test {


public int x = 0;
public Test (int x) {
this.x = x;
}
}

B. public class Test extends MyClass, MyOtherClass {


public int x = 0;
public Test (int xval) {
x = xval;
}
}

C. import java.awt.*;
public class Test extends Object {
int x;
private Test (int xval) {
x = xval;
}
}

D. protected class Test {


private int x = 0;
private Test (int xval) {
x = xval;
}
}

AC.

24. A class design requires that a particular member variable must be


accesible for direct
access by any subclasses of this class, otherwise not by classes which are not
members
of the same package. What should be done to achieve this?

A. The variable should be marked public


B. The variable should be marked private
C. The variable should be marked protected
D. The variable should have no special access modifier

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 116
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
E. The variable should be marked private and an accessor method provided

C.

25. Which correctly create an array of five empty Strings?

A. String a [] = new String [5];


for (int i = 0; i < 5; a[i++] = "");

B. String a [] = {"", "", "", "", "", ""};

C. String a [5];
D. String [5] a;
E. String [] a = new String[5];
for (int i = 0; i < 5; a[i++] = null);

AB.

26. Which cannot be added to a Container?

A. an Applet
B. a Component
C. a Container
D. a Menu
E. a Panel

D.

27. FilterOutputStream is the parent class for BufferedOutputStream,


DataOutputStream
and PrintStream. Which classes are a valid argument for the constructor of a
FilterOutputStream?

A. InputStream
B. OutputStream
C. File
D. RandomAccessFile
E. StreamTokenizer

B.

28. FilterInputStream is the parent class for BufferedInputStream and


DataInputStream.
Which classes are a valid argument for the constructor of a FilterInputStream?

A. File
B. InputStream
C. OutputStream
D. FileInputStream
E. RandomAccessFile

B.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 117
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

29. Given the following method body:

{
if (atest()) {
unsafe():
}
else {
safe();
}
}

The method "unsafe" might throw an AWTException (which is not a subclass


of
RunTimeException). Which correctly completes the method of declaration
when added at
line one?

A. public AWTException methodName()


B. public void methodname()
C. public void methodName() throw AWTException
D. public void methodName() throws AWTException
E. public void methodName() throws Exception

DE.

30. Given a TextArea using a proportional pitch font and constructed like this:

TextField t = new TextArea("12345", 5, 5);

Which statement is true?

A. The displayed width shows exactly five characters on each line unless
otherwise
constrained.
B. The displayed height is five lines unless otherwise constrained.
C. The maximum number of characters in a line will be five.
D. The user will be able to edit the character string.
E. The displayed string can use multiple fonts.

BD.

31. Given this skeleton of a class currently under construction:

public class Example {


int x, y;

public Example(int a){


//lots of complex computation
x = a;
}

public Example(int a, int b) {

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 118
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
/*do everything the same as single argument version of constructor
including assignment x = a */
y = b;
}
}

What is the most concise way to code the "do everything..." part of the
constructor
taking two arguments?

Answer:

this(a);

32. Given this skeleton of a class currently under construction:

public class Example {


int x, y;

public Example(int a, int b){


//lots of complex computation
x = a;
}

public Example(int a, int b, long c) {


/*do everything the same as the two argument version of constructor
including assignment x = a */
y = b;
}
}

What is the most concise way to code the "do everything..." part of the
constructor
taking two arguments?

Answer:

this(a, b);

Warning! A similar question will appear with at least two classes, then it is
super("arguments"); that you should use.

33. Which correctly create a two dimensional array of integers?

A. int a [][] = new int [10,10];


B. int a [10][10] = new int [][];
C. int a [][] = new int [10][10];
D. int []a[] = new int [10][10];
E. int [][]a = new int [10][10];

CDE.

34. Given the following method body:

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 119
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

{
if (sometest()) {
unsafe();
}
else {
safe();
}
}

The method "unsafe" might throw an IOException (which is not a subclass of


RunTimeException). Which correctly completes the method of declaration
when added at
line one?

A. public void methodName() throws Exception


B. public void methodname()
C. public void methodName() throw IOException
D. public void methodName() throws IOException
E. public IOException methodName()

AD.

35. What would be the result of attempting to compile and run the following
piece of
code?

public class Test {


static int x;

public static void main(String args[]){


System.out.println("Value is " + x);
}
}

A. The output "Value is 0" is printed.


B. An object of type NullPointerException is thrown.
C. An "illegal array declaration syntax" compiler error occurs.
D. A "possible reference before assignment" compiler error occurs.
E. An object of type ArrayIndexOutOfBoundsException is thrown.

A.

36. What would be the result of attempting to compile and run the following
piece of
code?

public class Test {


public int x;

public static void main(String args[]){


System.out.println("Value is " + x);
}
}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 120
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

A. The output "Value is 0" is printed.


B. Non-static variable x cannot be referenced from a static context..
C. An "illegal array declaration syntax" compiler error occurs.
D. A "possible reference before assignment" compiler error occurs.
E. An object of type ArrayIndexOutOfBoundsException is thrown.

B.

37. What would be the result of attempting to compile and run the following
piece of
code?

public class Test {

public static void main(String args[]){


int x;
System.out.println("Value is " + x);
}
}

A. The output "Value is 0" is printed.


B. An object of type NullPointerException is thrown.
C. An "illegal array declaration syntax" compiler error occurs.
D. A "possible reference before assignment" compiler error occurs.
E. An object of type ArrayIndexOutOfBoundsException is thrown.

D. Compiler says variable x might not have been initialized.

38. What should you use to position a Button within an application Frame so
that the size
of the Button is NOT affected by the Frame size?

A. a FlowLayout
B. a GridLayout
C. the center area of a BorderLayout
D. the East or West area of a BorderLayout
E. The North or South area of a BorderLayout

A.

For the following six questions you will be presented with a picture in the real
test,
showing the relationship and quite long text, but don't be afraid the questions
are quite
easy.

39. We have the following organization of classes.

class Parent {}
class DerivedOne extends Parent {}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 121
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
class DerivedTwo extends Parent {}

Which of the following statements is correct for the following expression?

Parent p = new Parent();


DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
p = d1;

A. Illegal both at compile and runtime.


B. Legal at compile time, but may fail at runtime.
C. Legal at both compile and runtime.
D. ....
E. ....

C.

40. We have the following organization of classes.

class Parent {}
class DerivedOne extends Parent {}
class DerivedTwo extends Parent {}

Which of the following statements is correct for the following expression?

Parent p = new Parent();


DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
d2 = d1;

A. Illegal both at compile and runtime.


B. Legal at compile time, but may fail at runtime.
C. Legal at both compile and runtime.
D. ....
E. ....

A.

41. We have the following organization of classes.

class Parent {}
class DerivedOne extends Parent {}
class DerivedTwo extends Parent {}

Which of the following statements is correct for the following expression?

Parent p = new Parent();


DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
d1 = (DerivedOne)d2;

A. Illegal both at compile and runtime.


B. Legal at compile time, but may fail at runtime.
C. Legal at both compile and runtime.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 122
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
D. ....
E. ....

A. Illegal both at compile and runtime. You cannot assign an object to a sibling
reference,
even with casting.

43. We have the following organization of classes.

class Parent {}
class DerivedOne extends Parent {}
class DerivedTwo extends Parent {}

Which of the following statements is correct for the following expression?

Parent p = new Parent();


DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
d1 = (DerivedOne)p;

A. Illegal both at compile and runtime.


B. Legal at compile time, but may fail at runtime.
C. Legal at both compile and runtime.
D. ....
E. ....

B.

44. We have the following organization of classes.

class Parent {}
class DerivedOne extends Parent {}
class DerivedTwo extends Parent {}

Which of the following statements is correct for the following expression?

Parent p = new Parent();


DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
d1 = p;

A. Illegal both at compile and runtime.


B. Legal at compile time, but may fail at runtime.
C. Legal at both compile and runtime.
D. ....
E. ....

A.

45. We have the following organization of classes.

class Parent {}
class DerivedOne extends Parent {}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 123
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
class DerivedTwo extends Parent {}

Which of the following statements is correct for the following expression?

Parent p = new Parent();


DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
p = (Parent)d1;

A. Illegal both at compile and runtime.


B. Legal at compile time, but may fail at runtime.
C. Legal at both compile and runtime.
D. ....
E. ....

C.

46. What would you use when you have duplicated values that needs to be
sorted?

A. Map
B. Set
C. Collection
D. List
E. Enumeration

D.

47. What kind of reader do you use to handle ASCII code?

A. BufferedReader
B. ByteArrayReader
C. PrintWriter
D. InputStreamReader
E. ?????

D.

InputStreamReader and FileReader automatically converts from a particular


character
encoding to Unicode. From Core Java Advanced Features p. 820.

48. How can you implement encapsulation in a class?

A. Make all variables protected and only allow access via methods.
B. Make all variables private and only allow access via methods.
C. Ensure all variables are represented by wrapper classes.
D. Ensure all variables are accessed through methods in an ancestor class.

B.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 124
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
49. What is the return-type of the methods that implement the MouseListener
interface?

A. boolean
B. Boolean
C. void
D. Pont

C.

50. What is true about threads that stop executing?

A. When a running thread's suspend() method is called, then it is indefinitely


possible for
the thread to start.
B. The interpreter stops when the main method stops.
C. A thread can stop executing when another thread is in a runnable state.
D. ......
E. ......

C.

51. For which of the following code will produce "test" as output on the
screen?

A. int x=10.0;
if (x=10.0)
{
System.out.println("test");
}

B. int x=012;
if (x=10.0)
{
System.out.println("test");
}

C. int x=10f;
if (x=10.0)
{
System.out.println("test");
}

D. int x=10L;
if (x=10.0)
{
System.out.println("test");
}

B.

52. Given this class ?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 125
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
class Loop {
public static void main (String [] args){
int x=0;
int y=0,
outer: for (x=0; x<100;x++) {
middle: for (y=0;y<100y++) {

System.out.printl("x=" + x + "; y=" + y);

if (y==10){

<<<>>>}
}
}
}//main
}//class

The question is which code must replace the <<<>>> to finish the outer loop?
A. continue middle;
B. break outer;
C. break middle;
D. continue outer;
E. none of these...

B.

53. What does it mean when the handleEvent() returns the true boolean?

A. The event will be handled by the component.


B. The action() method will handle the event.
C. The event will be handled by the super.handleEvent().
D. Do nothing.

A.

54. What is the target in an Event?

A. The Object() where the event came from.


B. The Object() where the event is destined for.
C. What the Object() that generated the event was doing.

A.

55. What is the statement to assign a unicode constant CODE with 0x30a0?

Answer:

public static final char CODE='\u30a0';

56. The following code resides in the source?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 126
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
class StringTest {

public static void main (String [] args){


//
// String comparing
//
String a,b;
StringBuffer c,d;
c = new StringBuffer ("Hello");
a = new String("Hello");
b = a;
d = c;

if (<<>>) {}
}
}//class

Which of the following statement return true for the <<>> line in
StringTest.class?

A. b.equals(a)
B. b==a
C. d==c
D. d.equals(c)

ABCD.

57. Which are valid identifiers?

A. %fred
B. *fred
C. thisfred
D. 2fred
E. fred

CE.

58. We have the following class X.

public class X {

public void method();

<<>>

Which of the following statement return true for the <<>> line in
StringTest.class?

A. abstract void method() ;


B. class Y extends X {}
C. package java.util;
D. abstract class z { }

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 127
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

BD.

59. Which code fragments would correctly identify the number of arguments
passed via
the command line to a Java Application?

A. int count = args.length;


B. int count = 0;
while args[count] !=null)
count++;
C. int count = 0;
while (!(args[count].equals("")))
count++;
D. int count = args.length - 1;

A.

60. Given a TextField that is constructed like this:

TextField t = new TextField(30);

Which statement is true?

A. The displayed width is 30 columns.


B. The displayed string can use multiple fonts.
C. The user will be able to edit the character string.
D. The displayed line will show exactly thirty characters.
E. The maximum number of characters in a line will be thirty.

AC.

61. What does the java runtime option -cs do?

A. check source with debug report


B. clear classes that are existing when starting compilation.
C. check if the source is newer when loading classes.

C.

62. When is an action invoked?

A. TextField Enter
B. TextArea Enter
C. Scrollbar
D. MouseDown
E. Button

AE.

63. What error does the following code generate?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 128
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

class SuperClass{

SuperClass(String s){}
}

class SubClass extends SuperClass{


SubClass(){}
}

......

SubClass s1 = new SubClass("The");


SuperClass s = new SubClass("The");

A. java.lang.ClassCastException
B. Wrong number of arguments in constructor
C. Incompatible type for =. Can't convert SubClass to SuperClass.
D. No constructor matching SuperClass found in class SuperClass.

BD.

64. How do you declare a native method called myMethod in Java?

Answer:

public native void myMethod();

65. What should you use to position a component within an application frame
so that the
components height is affected by the Framesize?

A. FlowLayout
B. GridLayout
C. Center area of a BorderLayout
D. East or West of a BorderLayout
E. North or South of a BorderLayout

D.

66. What should you use to position a component within an application frame
so that the
components width is affected by the Framesize?

A. FlowLayout
B. GridLayout
C. Center area of a BorderLayout
D. East or West of a BorderLayout
E. North or South of a BorderLayout

E.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 129
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
67. What should you use to position a Button within an application frame so
that the
height of the Button is affected by the Frame size but the width is not affected.

A. FlowLayout
B. GridLayout
C. Center area of a BorderLayout
D. East or West of a BorderLayout
E. North or South of a BorderLayout

D.

68. Which are keywords in Java?

A. NULL
B. sizeof
C. friend
D. extends
E. synchronized

DE.

69. Which is the advantage of encapsulation?

A. Only public methods are needed.


B. No exceptions need to be thrown from any method.
C. Making the class final causes no consequential changes to other code.
D. It changes the implementation without changing the interface and causes
no
consequential changes to other code.
E. It changes the interface without changing the implementation and causes
no
consequential changes to other code.

D.

70. What can contain objects that have a unique key field of String type, if it is
required
to retrieve the objects using that key field as an index?

A. Map
B. Set
C. List
D. Collection
E. Enumeration

A.

71. Which statement is true about a non-static inner class?

A. It must implement an interface.


B. It is accessible from any other class.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 130
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
C. It can only be instantiated in the enclosing class.
D. It must be final if it is declared in a method scope.
E. It can access private instance variables in the enclosing object.

E.

72. Which declares an abstract method in an abstract Java class?

A. public abstract method();


B. public abstract void method();
C. public void abstract Method(};
D. public void method() {abstract;/}
E. public abstract void method() {/}

B.

73. Which statements on the<<< call>>> line are valid expressions?

public class SuperClass {


public int x;
int y;
public void m(int a) {}
Superclass(){ }
}
class SubClass extends SuperClass{
private float f;
void m2() { return; }
SubClass() { }
}
class T {
public static void main (String [] args) {
int i;
float g;
SubClass b = SubClass();
<<< calls >>>
}
}

A. b.m2();
B. g=b.f;
C. i=b.x;
D. i=b.y;
E. b.m(6);

ACDE.

74. Type 7 in hexadecimal form.

Answer:

0x7

75. Type 7 in octal form.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 131
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

Answer:

07

76. What is the range of an integer?

A. -128–127
B. -32 768 – 32 767
C. -231 – 231 -1
D. -232 – 232 -1

C.

77. What is the range of a char?

A. \u0000 – \uFFFF
B. -32 768 – 32 767
C. -128 – 127
D. -231 – 231 -1

A.

78. What is the range of a char?

A. 0 – 215-1
B. -32 768 – 32 767
C. - 2 16 – 216-1
D. 0 – 216-1

D.

79. Which method contains the code-body of a thread?

A. start()
B. run()
C. init()
D. suspend()
E. continue()

B.

80. What can you place first in this file?

//What can you put here?

public class Apa{}

A. class a implements Apa


B. protected class B {}
C. private abstract class{}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 132
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
D. import java.awt.*;
E. package dum.util;
F. private int super = 1000;

ADE.

81. What is the output of the following code?

outer: for(int i=1; i<3; i++){


inner: for(int j=1; j<3; j++){
if (j==2){
continue outer;
}
System.out.println(i+" and "+j);
}
}

A. 1 and 1
B. 1 and 2
C. 2 and 1
D. 2 and 2
E. 2 and 3
F. 3 and 2
G. 3 and 3

AC.

82. What is the output of the following code?

outer: for(int i=1; i<2; i++){


inner: for(int j=1; j<2; j++){
if (j==2){
continue outer;
}
System.out.println(i " and " j);
}
}

A. 1 and 1
B. 1 and 2
C. 2 and 1
D. 2 and 2
E. 2 and 3
F. 3 and 2
G. 3 and 3

A.

83. How do you declare a native method?

A. public native method();


B. public native void method();
C. public void native method();

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 133
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
D. public void native() {}
E. public native void method() {}

B.

84. Given the following code, what is the output when a exception other than a
NullPointerException is thrown?

try{

//some code
}
catch(NullPointerException e) {
System.out.println("thrown 1");
}
finally {
System.out.println("thrown 2");
}
System.out.println("thrown 3");

A thrown 1
B thrown 2
C thrown 3
D none

BC.

85. You have been given a design document for a employee system for
implementation in
Java. It states.
bla bla bla

choose between the following words:

public
class
object
extends
Employee
Person
plus at least five more

How should you name the class?

Answer:

public class Employee extends Person

86. You have been given a design document for a polygon system for
implementation in
Java. It states.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 134
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
A polygon is drawable, is a shape. You must access it. It should have values
in a vector
bla bla

Which variables should you use?

choose between the following words:

public
object
vector
drawable
color
plus some more

Answer:

vector, color (not sure)

87. You have been given a design document for a polygon system for
implementation in
Java. It states. A polygon is a Shape. You must access it. It has a vector and
corners bla
bla

choose between the following words: What will you write when defining the
class?

public
class
Polygon
object
extends
Shape
plus some more

Answer:

public class Polygon extends Shape

88. What is true about the garbage collection?

A. The garbage collector is very unpredictable.


B. The garbage collector is predictable.
C.
D.
E.

A.

87. What is true about a thread?

A The only way you can create a thread is to subclass java.lang.Thread

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 135
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
B If a Thread with higher priority than the currently running thread is created,
the thread
with the higher priority runs.
C.
D.
E.

B (not sure)

88. What happens when you compile the following code?

public classTest {

public static void main(String args[] ) {


int x;
System.out.println(x);
}

A. Error at compile time. Malformed main method


B. Clean compile
C. Compiles but error at runtime. X is not initialized
D.
E. Compile error. Variable x may not have been initialized.

E.

89.What happens hen you run the following program with the command:

java Prog cat dog mouse

public class Prog {

public static void main(String args[]) {


System.out.println(args[0]) ;
}

A. Error at compile time. ArrayIndexOutOfBoundsException thrown


B. Compile with no output
C. Compile and output of dog
D. Compile and output of cat
E. Compile and output of mouse

D.

90. Which number for the argument must you use for the code to print cat?

With the command:

java Prog dog cat mouse

public class Prog {

public static void main(String args[]) {

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 136
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
System.out.println(args[?]) ;
}
}

A. 0
B. 1
C. 2
D. 3
E. none of these

B.

91. Which number for the argument must you use for the code to print cat?
With the command:

java Prog cat dog mouse

public class Prog {

public static void main(String args[]) {


System.out.println(args[?]) ;
}
}

A. 0
B. 1
C. 2
D. 3
E. none of these

A.

92. You have the following code:

.......
String s;
s = "Hello";
t = " " + "my";
s.append(t);
s.toLowerCase();
s+= " friend";
System.out.println(s);

What will be printed?

Answer:

hello my friend

93. What will happen when you attempt to compile and run this code?

public class MySwitch{

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 137
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
public static void main(String argv[]) {
MySwitch ms = new MySwitch();
ms.amethod();
}

public void amethod() {

char k=10;

switch(k){
default:
System.out.println("This is the default output");

break;
case 10:
System.out.println("ten");
break;
case 20:
System.out.println("twenty");
break;
}
}
}

A. None of these options


B. Compile time error target of switch must be an integral type
C. Compile and run with output "This is the default output"
D. Compile and run with output "ten"

D.

94. What will happen when you attempt to compile and run the following code?

public class MySwitch{

public static void main(String argv[]) {


MySwitch ms = new MySwitch();
ms.amethod();
}

public void amethod() {

int k=10;
switch (k){
default: //Put the default at the bottom, not here
System.out.println("This is the default output");
break;
case 10:
System.out.println("ten");
case 20:
System.out.println("twenty");
break;
}
}
}

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 138
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

A. None of these options


B. Compile time error target of switch must be an integral type
C. Compile and run with output "This is the default output
D. Compile and run with output "ten"

A.

95. Which of the following statements are true?


A. For a given component, events will be processed in the order that the
listeners were
added
B. Using the Adapter approach to event handling means creating blank
method bodies for
all event methods
C. A component may have multiple listeners associated with it
D. Listeners may be removed once added

CD.

Button for instance has the methods addActionListener (ActionListener a) and


removeActionListener(ActionListener a).

96. Which of the following statements are true?


A. Directly subclassing Thread gives you access to more functionality of the
Java
threading capability than using the Runnable interface
B. Using the Runnable interface means you do not have to create an instance
of the
Thread class and can call run directly
C. Both using the Runnable interface and subclassing of Thread require
calling start to
begin execution of a Thread
D. The Runnable interface requires only one method to be implemented, this
method is
called run

CD.

97. If you want subclasses to access, but not to override a superclass member
method,
what keyword should precede the name of the superclass method?

Answer:

final

98. If you want a member variable to not be accessible outside the current
class at all,
what keyword should precede the name of the variable when declaring it?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 139
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
Answer:

private
99. Which of the following are correct methods for initializing the array
"dayhigh" with 7
values?
A. int dayhigh = { 24, 23, 24, 25, 25, 23, 21 };
B. int dayhigh[] = { 24, 23, 24, 25, 25, 23, 21 };
C. int[] dayhigh = { 24, 23, 24, 25, 25, 23, 21 };
D. int dayhigh [] = new int [24, 23, 24, 25, 25, 23, 21];
E. int dayhigh = new [24, 23, 24, 25, 25, 23, 21] ;

BC.
100. Assume that val bas been defined as an int for the code below.

if(val > 4){


System.out.println("Test A");
}
else if(val > 9){
System.out.println("Test B");
}
else System.out.println("Test C");
Which values of val will result in "Test C" being printed:
A. val < 0
B val between 0 and 4
C val between 4 and 9
D val > 9
E val = 0
F no values for val will be satisfactory

ABE.
Q-1 int b1 = << 31;
int b2 = << 31;
b1 >>> = 31;
b1 >>> =1;
b2 >> = 31;
b2 >> = 1;

Ans: (a) b1 all zeros & b2 all zeros


(b) b1 all zeros & b2 all ones
(c) b1 all ones & b2 all zeros
(d) b1 all ones & b2 all ones

Q-2 Which of following expressions throws "Null Pointer Exception" given String S=null;

Ans: (a) if (S1 = null & s.length ( ) > o)


(b) if (S1 = null & s.length ( ) > o)
(c) if (S == null / s.length ( ) ==o)
(d) if (S == null ll s.length ( ) ==o)

Q-3 How can you define an array of 10 by 10 with all zeros ?


(a) int a[][]=new [10,10]
(b) int a[10][10] = new int[][]
(c) int a[][]=new int[10][10]
(d) int []a[]=new int[10][10]
(e) int a[][]=new int[][]

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 140
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

Q-4 Which of the following operations can be done on String


String s = "abcdef";
(a)s>>>=4;
(b)s[3]=a;
(c)s=s.trim();
(d)s=s.toUppercase();
(e)s.equals("xyz");
(f)s.append("xyz");
(g)s.subString(3);

Q-5 Following can be placed at Point1

// Point1
public class Xyz{
}

(a) import java.awt.*;


(b) public class Abc{ }
(c) package my.util.*;
(d) import lang.util.*;
(e) protected class pqr{ }

Q-6 In order to read a file line by line which is most preffered way
(a) FileInputStream fis = new FileInputStream("a.dat");
(b) DataInputStream dis = new DataInputStream(new FileInputStream("a.dat",r));
(c) DataInputStream dis = new DataInputStream(new FileInputStream("a.dat"));
(d) BufferedReader br = new BufferedReader(new FileReader("file");

Q-7 Intially "old.dat" file consists of 10 bytes of data after the following operation what is the length of the file
"old.dat" ?

FileOutputStream fos = new FileOutputStream("old.dat");


DataOutputStream dos = new DataOutputStream(fos);
dos.write(bye);

(a) 8
(b) 14
(c) 4
(d) 20
(e) none

Q-8 Which is the valid argument passed to constructor of FilterInputStream


(a) FileInputStream
(b) BufferedInputStream
(c) File
(d) RandomAccessFile
(e) DataInputStream

Q-9 Following cannot be added to a Container


(a)Component
(b)Panel
(c)MenuItem
(d)Container

Q-10 If following method may throw IOException How do you declare the method in a proper way

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 141
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

(a) public void IOException{ }


(b) void method throw IOException{ }
(c) public void method throws IOException{ }

Q-11 What will be the output for the following code


Outer: for(int i=1;i<3;i++) {
for(int j=1;j<3;j++){
if(j==2){ continue Outer; }
System.out.println(" i : "+i+" j : "+j);
}
}
(a) i=0 j=0
(b) i=1 j=0
(c) i=0 j=1
(d) i=1 j=1
(e) i=2 j=1
(f) i=1 j=2
(g) i=2 j=0
(h) i=0 j=2
(b) i=2 j=2

Q-12 For what values of x the "Point 3" only will be printed
if(x>4) { System.out.println("Point1"); }
else if(x>9 ) { System.out.println("Point 3"); }
else { System.out.println("Point 3"); }

(a) x is less than zero


(b) between 0 and 4
(c) greater than 4
(d) none

Q-13 How can you represent 7 in Hexadecimal ___

Q-14 What is return type of method in Actionlistener

Q-15 What is the arguments passed for methods in MouseMotionListener ?

Q-16 For the following code snippet which expression will result true ?
Integer A = new Integer(10);
Integer B = new Integer(10);
Float f = new Float(10.0f);
int x = 10;
long y = 10L;
Integer c = b;
(a) if(c==b)
(b) if(a==b)
(c) if(a.equals(10));
(d) if(x==a)
(e) if(x==y)

Q-17 Parent
| |
Derived1 Derived2

Parent p1;

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 142
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
Derived1 d1;
Derived2 d2;

All of above given class are not null;

(a) compile error


(b) runtime error
(c) compiles and runs without Exception

Q-18

class Abc{
int x,y,z;
public Abc(int a , int b) {
x= a;
y= b;
}
public Abc(int a, int b, int c) {
//point 1
z =c;
}
}
In order to initialize x and y in second constructor write shortest possible way at point 1

Q-19

What is output of following code


class StackTest{
static Stack s1,s2;
public static void main(String a[]){
s1 = new Stack();
s1.push(new Integer(100));
amethod(s1);
System.out.println(" s1 is " + s1 + "\n s2 is " + s2);
}
public static void amethod(Stack s1){
s2=s1;
}
}
(a) s1 is [100]
s2 is []
(b) s1 is [100]
s2 is [100]
(c) compile error
(d) runtime error

Q-20 If you resize a component horizontally you need to add the component

(a) to the north and south of BorderLayout


(b) to the east and west of BorderLayout
(c) to the FlowLayout
(d) to east and north of BorderLayout

Q-21 Which are the correct class declarations ? Assume in each case text constitutes of a file called Car.java on a
system with a case significant FileSystem.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 143
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com

(a) public class Fred{


(b) public class fred{
(c) public class Fred extends Abc,Xyz{
(d) public class Fred extends Objec{

Q-22 valid java keywords

q-23 legal identifiers

Q-24 after creating thread which method make that thread eligible to run _____ .

Q-25 what is range of char 0 to (2 raiseto 16) -1

Q-26 At which line in following code object created at line 3 becomes garbage collected

Q-27 Which interface impose no order, no restrictions and no content duplication ?


(a) Set
(b) Map
(c) List
(d) Collection

Q-28 When you resize window row changes its height vericatically . what is to be given
(a) weightx
(b) gridx
(c) gridy
(d) weighty

Q-29 what constraints effects size of GridbagLayout.


(a) fill
(b) gridx
(c) gridy
(d) anchor
Q-30 Specify true or false
(a) it is in programmers hand to explicitly invoke garbage collection and frees memory
when required.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 144
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
Preparation Topics
Questions are:

1. What is the return type of main() method?

2. What is the range of int primitive type in Java?

3. What is the argument type of all the methods in Mouse Listener interface?

4. What is the range of Char primitive type in Java?

5. Select the valid Java identifiers from the list.

6. Select from the list those, which are NOT Java keywords.

7. How to add a button in a frame so that height of the button is dependent on the frame, but width is not?

8. How will you represent the number 7 as an octal literal, using not more than four characters?

9. What modifier you should use if you don't want the method to be overridden in a subclass?

10. equals() on wrapper class. How it works?

11. Which is the earliest point in the given method, where Garbage Collection can be done?

12. What is the exact behavior of passing "references to objects" as method parameters?

13. Immutability of String

14. Explicit casting during object reference assignment.

15. Short circuit operators, compare them with logical & and |

16. "while" loop. Question to test that it may not get executed even once if the condition is false in the beginning itself.

17. Labeled "Continue". Question to test that where flow goes when "continue" with a label is encountered?

18. "switch" statement. Question to test the "fall-through" nature of "switch" structure.

19. "If(){} else if(){}" to test mutual exclusiveness of code blocks

20. Signature of a subclass: importance of "extends".

21. Thread: What will stop it from executing?

22. What does the getid() method of AWT subclass return?

23. What are the possible signatures of methods in the given options, allowed in a subclass, given a method in superclass.

24. Characteristics of Garbage Collection in Java

25. Which all keywords from the given list needed to declare instance variables of the given class definition?

26. Given a class definition, what is allowed above it in the code? From the list package, import, other class signature etc.

27. Which element of args array of main will contain the specific command line argument?

28. Class, subclass definitions and calling. Syntax error line identification.

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 145
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
29. What all from the given list a container can contain?

30. Event handling and multiple listeners for a component.

31. Valid declaration type for a two dimensional array.

32. Constructor argument for FilterInputStream

33. A constructor calling this() in the same class.

34. Two ways to construct Threads. (1. extends and 2. implements)

35. Which collection can have ordered items but repetitions allowed?

36. Gridbaglayout parameter characteristics.(weightx and weighty)

37. Adapter classes for event handling.

38. Array declaration (without initialization) in main() what will be printed if array elements are to be printed?

39. With an instance variable of a class without the class getting instantiated getting referred in main.

40. What is the return type of listener?

41. Exception handling: To test that finally always gets executed.

42. What will be the output, when a frame declaration and description without using a separate class for that, is done in
main()?

43. Compare and contrast of >> and >>>

44. In a case sensitive operating system, what are the valid class signatures given the source code file name?

45. Valid inner class declaration and instantiation at same place.

46. "Throws" and "throw" syntax.

47. Final contents of a string after concat, toUppercase etc. without assigning to other string or overwriting, and then finally
with += operator.

48. Ideal conditions for total encapsulation.

49. Math object’s floor, ceil and round.

50. Default scope of class (package level or friendly as it is called without a modifier).

51. How to declare a native method?

52. At a given point (place) in code, which all assignment statements valid, from the given list?

53. Access of variables between inner and outer classes(which variables can the inner class access)

54. Name of the method used to schedule a Thread for execution.

55. From the given list, which all are valid abstract class signatures.

56. From a given list, which statement is true about a non-static inner class?

57. From the list given, what can cause a thread to stop execution?

Visit http://www.laynetworks.com
Downloaded form http://www.laynetworks.com : solutions@laynetworks.com 146
Do visit the site for the tones of information for the programmers and the newbies
Get Your Free Email Account of 6 MB form http://www.laynetworks.com
58. The modifier “synchronized” for object locking.

59. Casting questions to see when the code produces a runtime error and when a compile error

Visit http://www.laynetworks.com

Das könnte Ihnen auch gefallen