Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Answers
1. the code will compile an print "Equal".
2. the code will compile an print "Not Equal".
3. the code will cause a compiler error.
Question 2.
Read this piece of code carefully
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
1. the code will compile an print "Equal".
2. the code will compile an print "Not Equal".
3. the code will cause a compiler error
Question 3.
Read the code below. Will be the result of attempting to compile and
run the code below.
public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
Answers
1. The code does not compile.
2. The code compiles cleanly and shows "Object Version".
3. The code compiles cleanly and shows "String Version"
4. The code throws an Exception at Runtime.
Question 4.
Read the code below. Will be the result of attempting to compile and
run the code below.
public class AQuestion
{
public void method(StringBuffer sb)
{
System.out.println("StringBuffer Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Answers
1. The code does not compile.
2. The code compiles cleanly and shows "StringBuffer Version".
3. The code compiles cleanly and shows "String Version"
4. The code throws an Exception at Runtime.
Question 5.
Read the following code below.
Question 6.
An Interface can never be private or protected.
Answers
True
False
Question 7.
A Vector class in jdk 1.2
1. is public
2. is final
3. implements java.util.List
4. is serializable
5. has only One constructor
Question 8.
A String Class
1. is final
2. is public
3. is serializable
4. has a constructor which takes a StingBuffer Object as an Argument
Question 9.
public interface AQuestion
{
void someMethod();
}
The class which implements AQuestion
Question 10.
public class AQuestion
{
private int i = j;
private int j = 10;
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}
Answers
1. Compiler error complaining about access restriction of private variables of
AQuestion.
2. Compiler error complaining about forward referencing.
3. No error - The output is 0;
4. No error - The output is 10;
Question 11.
public class AQuestion
{
private int i = giveMeJ();
private int j = 10;
private int giveMeJ()
{
return j;
}
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}
Answers
1. Compiler error complaining about access restriction of private variables of
AQuestion.
2. Compiler error complaining about forward referencing.
3. No Compilation error - The output is 0;
4. No Compilation error - The output is 10;
Question 12.
public class AQuestion
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(Throwable t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
1. Compiler error complaining about the catch block, where no Throwable object
can ever be thrown.
2. Compiler error - Throwable Object can not be caught, only Exceptions must be
caught.
3. No compiler error. The lines "Before Try" and "At the end" are printed on the
screen.
Question 13.
public class AQuestion
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(java.io.IOException t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
1. Compiler error complaining about the catch block where no IOException object
can ever be thrown.
2. Compiler error - IOException not found. It must be imported in the first line of
the code.
3. No compiler error. The lines "Before Try" and "At the end" are printed on the
screen.
Question 14.
The class java.lang.Exception
i. Is public
ii. Extends Throwable
iii. Implements Throwable
iv. Is serializable
Question 15.
Read this piece of code carefully
if("String".trim() == "String".trim())
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
1. the code will compile an print "Equal".
2. the code will compile an print "Not Equal".
3. the code will cause a compiler error
Question 16.
Read this piece of code carefully
if( "STRING".toUpperCase() == "STRING")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
1. the code will compile an print "Equal".
2. the code will compile an print "Not Equal".
3. the code will cause a compiler error
Question 17.
The following lines of code
byte b = 0;
b += 1;
Question 18.
The following express
char c = -1;
1. will cause a compiler error as the range of character is between 0 and 2^16 - 1.
Will request for an explicit cast.
2. will not cause a compiler error and c will have the value -1;
3. c will not represent any ascii character.
4. c will still be a unicode character.
Question 19.
Which of the following statements are true?
1. A method can throw an Exception
2. A method can return an Exception
Question 20.
All the wrapper classes (Integer, Boolean, Float, Short, Long,
Double and Character)
1.
2.
3.
4.
5.
are public
are serializable
are immutatable
extend java.lang.Number
are final
Question 31.
Read this piece of code carefully
if("String".replace('T','t') == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
1. the code will compile an print "Equal".
2. the code will compile an print "Not Equal".
3. the code will cause a compiler error
Question 32.
Read this piece of code carefully
System.out.println("String".substring(0,4));
Answers
1. the code will print "Strin" on the screen.
2. the code will print "Stri" on the screen.
3. the code will cause a compiler error.
Question 33.
Read this piece of code carefully
if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
1. the code will compile an print "Equal".
2. the code will compile an print "Not Equal".
3. the code will cause a compiler error
Question 34.
public class ADirtyOne
{
public static void main(String args[])
{
System.out.println(Math.abs(Integer.MIN_VALUE));
}
}
Assuming that the classes are in two seperate files, compilation of the Dervied.java
causes
1. A compiler error because RuntimeException is not a subclass if
ClassNotFoundException.
2. No compiler error.
Question 37.
Math.round(Float.MAX_VALUE);
1.
2.
3.
4.
Returns Integer.MAX_VALUE.
Returns a closest integer to Float.MAX_VALUE;
Causes a compilation error.
Causes a runtime Exception
Question 38.
Read the code below carefully
import java.awt.*;
public class TestFrame extends Frame
{
Button
Button
Button
Button
Button
public TestFrame()
{
setLayout(new BorderLayout());
add(bSouth,BorderLayout.SOUTH);
add(bWest,BorderLayout.WEST);
add(bEast,BorderLayout.EAST);
add(bNorth,BorderLayout.NORTH);
add(bCenter);
setLayout(new FlowLayout());
validate();
pack();
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}
What will be the effect trying compile and run the above class?
1. Compilation error - a Layout cannot be set twice for a component.
2. Compilation error - One button is added without specifing the position in the
borderLayout
3. No Compilation Error. The Buttons are arranged in a line in the order (From left to
right) "North","South","West","East" and "Center".
4. No Compilation Error. The Buttons are arranged in a line in the order (From left to
right) "South","West","East","North" and "Center".
5. No Compilation Error. The Buttons are arranged in the north , south, west, east
and center regions, as in a borderlayout. Any further additions will follow the
rules of FlowLayout manager.
Question 39.
import java.awt.*;
public class TestFrame extends Frame
{
Button
Button
Button
Button
Button
public TestFrame()
{
setLayout(new FlowLayout());
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setLayout(new BorderLayout());
validate();
setSize(300,300);
setVisible(true);
Position
Center
North
South
East
West
Question 40.
A frame uses BorderLayout Management and has components added
to all the regions. One resizing the Frame Some space becomes
available. The space is alloted to the regions, in which Order of
preference?
1.
2.
3.
4.
Question 41.
Read the following piece of code carefully.
import java.awt.*;
public class TestFrame extends Frame
{
Button firstOne = new Button("One");
Button secondOne = new Button("Two");
public TestFrame()
{
add(firstOne,BorderLayout.NORTH);
add(secondOne,BorderLayout.NORTH);
setSize(400,400);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
1. If the above code runs, the buttons - one and three are laid out in a single row
from left to right with a gap in between .
2. If the above code runs, the buttons - one and three are laid out in a single row
from left to right with no gap in between.
3. Code does not compile - a component can not be hidden after being added to a
container.
4. Code gets compiled successfully but throws runtime Exception - a component
can not be hidden after being added to a container.
Question 43.
Read the code below carefully.
import java.awt.*;
public class TestFrame extends Frame
{
public TestFrame()
{
setLayout(new GridLayout(2,1));
for(int i = 1 ; i <= 4 ;++i)
{
add(new Button(Integer.toString(i)));
}
pack();
setVisible(true);
}
public static void main(String args[])
{
1. The code above will not compile - The number of components added is more
than the magnitude of row * columns of the Grid Layout Manager.
2. The code will throw a runtime Exception - The number of components added is
more than the magnitude of row * columns of the Grid Layout Manager.
3. The code will compile cleanly and when run Four buttons are visible in 2 rows
and 2 columns.
4. The code will compile and when run, Four buttons are seen in a single Column.
Question 44.
Read the code below carefully.
import java.awt.*;
public class TestFrame extends Frame
{
public TestFrame()
{
setLayout(new GridLayout());
for(int i = 1 ; i <= 4 ;++i)
{
add(new Button(Integer.toString(i)));
}
pack();
setVisible(true);
}
1. The code will not compile - the grid layout does not have a no-argument
constructor..
2. The code compiles and when run all the buttons are seen in a single column.
3. The code compiles and when run all the buttons are seen in a singe row.
4. The code compiles and when run all button are added one on top or another and
only the last one added is visible.
5. The code compiles , but throws a runtime Exception when components are added.
Question 45.
Does nothing.
Throws UnsupportedMethodException when called.
It is not overriden in java.awt.ScrollPane.
Sets the layout to the specified Layout Manager.
Question 46.
A class which has all its constructors declared as private
1. Cannot be instantiated by any other class.
2. Cannot be extended.
3. Both i and ii.
4. has to be declared final.
Question 47.
The GridBagConstraints Class
1. Is serializable.
2. Is cloneable.
3. belongs to the java.awt package.
4. extends Object.
Question 48.
Read the following code carefully
import java.awt.*;
public class TestFrame extends Frame
{
public TestFrame()
{
CheckboxGroup chg = null;
Checkbox ch = new Checkbox("Test",true,chg);
setLayout(new FlowLayout());
add(ch);
pack();
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
getCanonicalPath()
getCanonicalFile()
getAbsolutePath()
getAbsoluteFile()
createTempFile()
createNewFile()
mkdir()
mkdirs()
toURL()
Question 51.
Read the code below carefully.
if( "String".endsWith(""))
Sytem.out.println("True");
else
System.out.println("False");
Question 53.
Read the code below carefully.
public class TestClass
{
public static void main(String Args[])
{
StringBuffer sb1 = new StringBuffer("String");
StringBuffer sb2 = new StringBuffer("String");
if(sb1.equals(sb2))
{
//lots of code
}
}
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
//do some crap here.
}
//more and more code here
1. The code causes compilation error - sleep cannot be called inside synchronized
methods.
2. The code causes compilation error - sleep is not a static method of
java.lang.Thread
3. The Thread sleeps for at least 500 milliseconds in this method if not interrupted.
4. When the thread "goes to sleep" it releases the lock on the object.
5. The "sleeping" Threads always have the lock on the Object.
Question 57.
The no-argument constructor provided by the compiler when no
constructor is explicitly provided in the code
1.
2.
3.
4.
is always public
is always "friendly"
always defaults to the access modifier provided for the class.
depends on the compilation options of javac
Question 58.
Which of the following is the direct base class of java.awt.AWTEvent.
1. java.lang.Object.
2. java.util.EventObect
Question 59.
Interface methods can be declared with the following modifiers
1. public
2. none (i.e., no access modifier).
3. private.
4. static
5. native
6. synchronized.
Question 60.
Which of the following are true about the class defined inside an
interface
1. it is not possible in the java Laungage.
2. The class is always public.
3. The class is always static.
4. the class methods cannot call the methods declared in the interface.
5. the class methods can call only the static methods declared in the interface.
Question 61.
what does the following expression return?
(0.0 == -0.0)
1.
2.
true
false
Question 62.
What does the following expression print on the screen?
System.out.println(Math.min(Float.NaN, Float.POSITIVE_INFINITY));
1.
2.
3.
NaN
Infinity
The expression throws a runtime exception - NaN is an illegal argument.
Question 63.
What does the following expression return
Math.max(Float.POSITIVE_INFINITY,Double.POSITIVE_INFINITY);
1.
2.
3.
Float.POSITIVE_INFINITY
Double.POSITIVE_INFINITY
runtime Exception
Question 64.
Read the following code carefully.
public class AStringQuestion
{
static String s1;
static String s2;
public static void main(String args[])
{
s2 = s1+s2;
System.out.println(s2);
Question 65.
Read the code below carefully.
public class AQueryClass
{
public static synchronized void method1()
{
//lots and lots of code here.
}
Assuming that all the code inside the method is legal and cannot be a cause for
compilation error 1. An attempt to compile will cause a compilation error. Static methods cannot be
synchronized.
2. Compilation will be successfull. The Object instantiated will have a lock which
has to be gained by Threads calling any of the two methods.
3. Compilation will be successfull. There will exist a Class wide lock which will
have to be gained by the Thread calling method1 and an instance lock for each
instance which will have to be gained by the Thread making a call to method2 on
the Object instance. The class wide lock and the instance lock being independent
of each other.
4. Compilation will be successfull. There will exist a Class wide lock which will
have to be gained by the Thread calling method1 and an instance lock for each
instance which will have to be gained by the Thread making a call to method2 on
the Object instance. The class wide lock and the instance lock being mutually
exclusive.
Question 66.
Class fields with the following modifiers will not be serialized
1.
2.
3.
4.
private
static
transient
protected
Question 67.
Assume that Cat is a class and String[] args is the argument passed to
the public static void main(String args[]) method of the class. The class is
executed with the following command line string
c:\somedirectory> java Cat
3. does not exist after all non demon threads in the group have finished executing.
4. does not exist after all the threads in the group have finished executing.
Question 69.
Assume that th is an instance holding a thread object. th.start() causes
the thread to start running and eventually complete its execution. The
object reference by th is not accessable any more and is garbage
collected when the garbage collecter runs.
1. True
2. False
Question 70.
Read the code below carefully.
import java.io.*;
public class OutOut
{
public static void main(String args[]) throws IOException
{
PrintStream pr = new PrintStream(new FileOutputStream("outfile"));
System.out = pr;
System.out.println("Lets see what I see now??");
}
}
1. The code causes a compiler error. out is a declared final in System and cannot be
assigned to pr.
2. The code causes a runtime Exception due the assignment to a final variable.
3. The code compiles and runs success fully. A file called "outfile" is created and
"Lets see what I see now??" is printed in the same.
Question 71.
Read the following piece of code carefully
public abstract class AbstractClass
{
public AbstractClass()
{
System.out.println("this is an abstract class constructor!");
}
}
}
1.
2.
3.
4.
5.
Question 77.
If the finalize() method of an object is re-references an object so that
it becomes in-eligible for garbage collection
1. The compiler will throw an error.
2. The garbage collector "collects" the object anyway.
3. The garbage collector does not collect the object in the present sweep. But when
the object becomes eligible for garbage collection again, its finalize method will
not be called by the garbage collector (i.e., if the garbage collector gets a chance
to run.). It will simply be garbage collected.
4. The object can never be garbage collected and hence leads to memory-leak. Each
time the garbage collector calls finalize before "collecting" the object the object
"resurrects" itself.
Question 78.
If a Runtime Exception is thrown in the finalize method 1.
2.
3.
4.
Question 79.
The factory class java.util.Collections
1. is public
2. implements the java.util.Collection interface.
Question 80.
import java.io.*;
public class TransientWriter implements Externalizable
{
private transient String s = "Hope I can ever be persistant!";
public void writeExternal(ObjectOutput oOut) throws IOException
{
oOut.writeObject(s);
}
public void readExternal(ObjectInput oIn) throws IOException,
ClassNotFoundException
{
s=(String)oIn.readObject();
}
public String toString()
{
return s;
}
}
class K
{
public static void main(String args[]) throws IOException,
ClassNotFoundException
{
TransientWriter tw = new TransientWriter();
ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("tw.out"));
out.writeObject(tw);
ObjectInputStream in = new ObjectInputStream(new
FileInputStream("tw.out"));
TransientWriter tw2 = (TransientWriter) in.readObject();
System.out.println(tw2);
}
}
Question 81.
Read the code below carefully.
public class ATest
{
public static void main(String args[])
{
AnInterface ai = new AnInterfaceImpl();
ai.methodOne();
}
Will cause a compile time error. (Line 5 : Exception must be caught or thrown
by main(String))
2.
Will cause a compile time error for Class AnInterfaceImpl. The method
methodOne() be declared with "throws Exception".
3.
Will cause no compile time error and print "I will never throw and Exception
the screen".
4.
Will Cause a run time error .
Question 82.
Read the code below carefully.
public class ATest
{
public static void main(String args[])
{
AnInterfaceImpl ai = new AnInterfaceImpl();
ai.methodOne();
}
}
Attempting to compile and run the above code
1.
Will cause a compile time error. (Line 5 : Exception must be caught or
thrown by main(String))
2.
Will cause a compile time error for Class AnInterfaceImpl. The method
methodOne() be declared with "throws Exception".
3.
Will cause no compile time error and print "I will never throw and Exception
the screen".
4.
Will Cause a run time error .
Question 83
Read the code below carefully
//Contents of Constants.java
public class Constants
{
public static final String greetingString="Hello";
}
//Contents of SomeClass.java
public class SomeClass
{
public static void main(String[] args)
{
System.out.println("Greeting String is "+Constants.greetingString);
}
}
Question 84.
Read the code below carefully
import java.util.*;
public class AllFinals
{
final Vector v;
public AllFinals()
{
}
}
Question 85.
import java.util.*;
public class AllFinals
{
{
final Vector v;
v=new Vector();
}
public AllFinals()
{
}
public void someMethod()
{
System.out.println(v.isEmpty());
}
Question 86.
//Contents of File AllFinals.java
import java.util.*;
public class AllFinals
{
final Vector v;
public AllFinals()
{
v=new Vector();
}
public AllFinals(int i)
{
}
public void someMethod()
{
System.out.println(v.isEmpty());
}
}
Question 87.
public class A
{
final StringBuffer sb = new StringBuffer("I am final");
public A()
{
}
public StringBuffer getSb()
{
return this.sb;
}
public static void main(String[] args)
{
A a = new A();
StringBuffer localSB = a.getSb();
localSB.append("....");
localSB = new StringBuffer();
System.out.println(localSB.toString());
}
}
Question 88.
public class A
{
StringBuffer sb = new StringBuffer("I am final");
public A()
{
}
public final StringBuffer getSb()
{
return this.sb;
}
public static void main(String[] args)
{
A a = new A();
StringBuffer localSB = a.getSb();
localSB.append("....");
localSB = new StringBuffer();
System.out.println(localSB.toString());
}
}
Question 89.
public class A
{
StringBuffer sb = new StringBuffer("I am final");
public A()
{
}
public StringBuffer getSb()
{
return this.sb;
}
public static void main(String[] args)
{
A a = new A();
final StringBuffer localSB = a.getSb();
localSB.append("....");
localSB = new StringBuffer();
System.out.println(localSB.toString());
}
}
Question 90.
//contents of file A.java
public class A
{
A()
{
class B
{
static
{
}
}
A(int i)
{
System.out.println("No B this time");
}
}
class C
{
public static void main(String[] args)
{
A a = new A(1);
}
}
Question 91.
Read the following code excerpt carefully. //Contents of file A.java public
class A
{
A()
{
class B
{
{
}
A(int i)
{
class B
{
{
}
}
new B();
}
class C
{
public static void main(String[] args)
{
A a = new A(1);
}
}
Answers
1. 1
2. 2
3. 3
4. 1
5. 4
6. FALSE
7. 1,3,4
8. 1,2,3,4
9. 1,3
10.
2
11.
3
12.
3
13.
1
14.
1,2,4
15.
1
16.
1
17.
1
18.
1
19.
1,2
20.
1,2,3,5
21.
1
22.
1
23.
1
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
1,2,3
2
3
3
3
3
4
1
2
2
2
3
2
1
4
5
1
3,4
2
3
3
1
3
1,2,3,4
4
3
1,2,5,6,9
1
1
No
3
2,4
3,5
3
2
1,2
2,3,4
1
1
2
3
3
2,3
1
2,4
2
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
1
5
3
3
4
1,2,3
1
3
2
1
3
1
3
3
1
2
1
4
4
3
1
4,5