1. What is the result when you compile and run the following code?
class Top {
static void myTop() {
System.out.println("Testing myTop
method in Top class");
}
}
public class Down extends Top {
void myTop() {
System.out.println("Testing myTop
method in Down class");
}
public static void main(String [] args)
{
Top t = new Down();
t.myTop();
}
}
A) Compile Time error
B) Runtime error
C) Prints Testing myTop method in Top class
on the console
D) Prints Testing myTop method in Down
class on the screen
Answer 1:
2. Which of the
code fragments will throw an "ArrayOutOfBoundsException" ?
A) for (int i = 0; i < args.length; i ++
) {
System.out.print( i ) ;
}
B) System.out.print(args.length);
C) for ( int i = 0; i < 1; i++ ) {
System.out.print(args[i]);
}
D) None of the above
Answer 2:
3. What is the
result of the following program, when you compile and run?
public class MyTest {
final int x;
public MyTest() {
System.out.println( x + 10 );
}
public static void main( String args[]
) {
MyTest mt = new MyTest();
}
}
A) Compile time error
B) Runtime error
C) Prints on the screen 10
D) Throws an exception
Answer 3:
4. What is the
output when you compile and run the following code fragment?
class MyTest {
public void myTest() {
System.out.println("Printing
myTest in MyTest class");
}
public static void myStat() {
System.out.println("Printing
myStat in MyTest class");
}
}
public class Test extends MyTest {
public void myTest() {
System.out.println("Printing
myTest in Test class");
}
public static void myStat() {
System.out.println("Printing
myStat in Test class");
}
public static void main ( String args[]
) {
MyTest mt = new Test();
mt.myTest();
mt.myStat();
}
}
A) Printing myTest in MyTest class
followed by Printing myStat in MyTest class
B) Printing myTest in Test class followed
by Printing myStat in MyTest class
C) Printing myTest in MyTest class followed
by Printing myStat in MyTest class
D) Printing myStat in MyTest class followed
by Printing myStat in MyTest class
Answer 4:
5. Select all
the exceptions thrown by wait() method of an Object class, which you can
replace in the place of xxx legally?
class T implements Runnable {
public void run() {
System.out.println(
"Executing run() method" );
myTest();
}
public synchronized void myTest()
{
try {
wait(-1000);
System.out.println(
"Executing the myTest() method" ) ;
}
XXX
}
}
public class MyTest {
public static void main ( String
args[] ) {
T t = new T();
Thread th = new Thread ( t
);
th.start();
}
}
A) catch ( InterruptedException ie) {}
B) catch ( IllegalArgumentException il ) {}
C) catch ( IllegalMonitorStateException im
) {}
D) Only catch ( InterruptedException e ) {}
exception
Answer 5:
6. Which of the
following are examples of immutable classes , select all correct answer(s)?
A) String
B) StringBuffer
C) Double
D) Integer
Answer 6:
7. Select the
correct answer for the code fragment given below?
public class TestBuffer {
public void myBuf( StringBuffer s,
StringBuffer s1) {
s.append(" how are you") ;
s = s1;
}
public static void main ( String
args[] ) {
TestBuffer tb = new TestBuffer();
StringBuffer s = new
StringBuffer("Hello");
StringBuffer s1 = new
StringBuffer("doing");
tb.myBuf(s, s1);
System.out.print(s);
}
}
A) Prints Hello how are you
B) Prints Hello
C) Prints Hello how are you doing
D) Compile time error
Answer 7:
8. What is the
result when you compile and run the following code?
public class MyTest {
public void myTest( int[] increment )
{
increment[1]++;
}
public static void main ( String args[]
) {
int myArray[] = new int[1];
MyTest mt = new MyTest();
mt.myTest(myArray);
System.out.println(myArray[1]);
}
}
A) Compile time error
B) Runtime error
C) ArrayOutOfBoundsException
D) Prints 1 on the screen
Answer 8:
9. Chose all
valid identifiers?
A) int100
B) byte
C) aString
D) a-Big-Integer
E) Boolean
F) strictfp
Answer 9:
10. Select the
equivalent answer for the code given below?
boolean b = true;
if ( b ) {
x = y;
} else {
x = z;
}
A) x = b ? x = y : x = z ;
B) x = b ? y : z ;
C) b = x ? y : z ;
D) b = x ? x = y : x = z ;
Answer 10:
11. Chose all
correct answers?
A) int a [][] = new int [20][20];
B) int [] a [] = new int [20][];
C) int [][] a = new int [10][];
D) int [][] a = new int [][10];
Answer 11:
12. Consider
the following code and select the correct answer?
class Vehicle {
String str ;
public Vehicle() {
}
public Vehicle ( String s ) {
str = s;
}
}
public class Car extends Vehicle {
public static void main (String
args[] ) {
final Vehicle v = new Vehicle
( " Hello" );
v = new Vehicle ( " How are
you");
v.str = "How is going";
System.out.println( "Greeting is : "
+ v.str );
}
}
A) Compiler error while subclassing the
Vehicle
B) Compiler error , you cannot assign a
value to final variable
B) Prints Hello
C) Prints How is going
Answer 12:
13. Java source
files are concerned which of the following are true ?
A) Java source files can have more than
one package statements.
B) Contains any number of non-public
classes and only one public class
C) Contains any number of non-public
classes and any number of public classes
D) import statements can appear anywhere in
the class
E) Package statements should appear only in
the first line or before any import statements of source file
Answer 13:
14. Select all
correct answers from the following?
int a = -1;
int b = -1;
a = a >>> 31;
b = b >> 31;
A) a = 1, b =1
B) a = -1, b -1
C) a = 1, b = 0
D) a = 1, b = -1
Answer 14:
15. What is the
value of a , when you compile and run the following code?
public class MyTest {
public static void main (
String args[] ) {
int a = 10;
int b = 9;
int c = 7;
a = a ^ b ^ c;
System.out.println ( a );
}
}
A) 10
B) 9
C) 7
D) 4
Answer 15:
16. The
following code has some errors, select all the correct answers from the
following?
public class MyTest {
public void myTest( int i ) {
for ( int x = 0; x < i;
x++ ) {
System.out.println(
x ) ;
}
}
public abstract void Test() {
myTest(10);
}
}
A) At class declaration
B) myTest() method declaration
C) Test() method declaration
D) No errors, compiles successfully
Answer 16:
17. At what
point the following code shows compile time error?
class A {
A() {
System.out.println("Class A
constructor");
}
}
class B extends A {
B() {
System.out.println("Class B
constructor");
}
}
public class C extends A {
C() {
System.out.println("Class C
constructor");
}
public static void main (
String args[] ) {
A a = new A(); // Line 1
A a1 = new B(); // Line 2
A a2 = new C(); // Line 3
B b = new C(); // Line 4
}
}
A) A a = new A(); // Line 1
B) A a1 = new B(); // Line 2
C) A a2 = new C(); // Line 3
D) B b = new C(); // Line 4
Answer 17:
18. Which of
the following statements would return false? if given the following
statements.
String s = new String ("New year");
String s1 = new String("new Year");
A) s == s1
B) s.equals(s1);
C) s = s1;
D) None of the above
Answer 18:
19. Select all
correct answers about what is the definition of an interface?
A) It is a blue print
B) A new data type
C) Nothing but a class definition
D) To provide multiple inheritance
Answer 19:
20. Select all
correct answers from the following code snippets?
A) // Comments
import java.awt.*;
package com;
B) import java.awt.*;
// Comments
package com;
C) package com;
import java.awt.*;
// Comments
D) // Comments
package com;
import java.awt.*;
public class MyTest {}
Answer 20:
21. What is the
result when you compile and run the following code?
public class MyError {
public static void main ( String
args[] ) {
int x = 0;
for ( int i = 0; i < 10; i++
) {
x = new Math( i );
new System.out.println(
x );
}
}
}
A) Prints 0 to 9 in sequence
B) No output
C) Runtime error
D) Compile time error
Answer 21:
22. There are
two computers are connected to internet, one computer is trying to open a
socket connection to read the home page of another computer, what are the
possible exceptions thrown while connection and reading InputStream?.
A) IOException
B) MalformedURLException
C) NetworkException
D) ConnectException
Answer 22:
23. What is the
result from the following code when you run?
import java.io.*;
class A {
A() throws Exception {
System.out.println
("Executing class A constructor");
throw new IOException();
}
}
public class B extends A {
B() {
System.out.println
("Executing class B constructor");
}
public static void main (
String args[] ) {
try {
A a = new B();
} catch ( Exception e) {
System.out.println( e.getMessage() );
}
}
}
A) Executing class A constructor
B) Executing class B constructor
C) Runtime error
D) Compile time error
Answer 23:
24. What is the
result from the following code when you run?
import java.io.*;
class A {
A() {
System.out.println
("Executing class A constructor");
}
A(int a) throws Exception {
System.out.println
("Executing class A constructor");
throw new IOException();
}
}
public class B extends A {
B() {
System.out.println
("Executing class B constructor");
}
public static void main (
String args[] ) {
try {
A a = new B();
} catch ( Exception e) {
System.out.println( e.getMessage() );
}
}
}
A) Executing class A constructor
followed by Executing class B constructor
B) No output
C) Runtime error
D) Compile time error
Answer 24:
25. What is the
result when you compile and run the following code?
byte Byte = 10;
byte Double = 12;
byte Integer = Byte * Double;
A) 120;
B) Compile time error while declaring
variables
C) Compile time error while multiplication
D) None of the above
Answer 25:
26. Select all
valid methods for Component class?
A) setBounds(), setVisible(), setFont()
B) add(), remove()
C) setEnabled(), setVisible()
D)addComponent()
Answer 26:
27. Which
subclasses of the Component class will display the MenuBar?
A) Window, Applet
B) Applet, Panel
C) Frame
D) Menu, Dialog
Answer 27:
28. Select all
correct answers from the following statements?
A) Frame's default layout manager is
BorderLayout
B) CheckBox, List are examples of non
visual components
C) Applets are used to draw custom drawings
D) Canvas has no default behavior or
appearance
Answer 28:
29. Select all
the methods of java.awt.List?
A) void addItem(String s), int getRows()
B) void addItem(String s, int index), void
getRows()
C) int[] getSelectedIndexes(), int
getItemCount()
D) int[] getSelectedIndexes(), String[]
getSelectedItems()
Answer 29:
30. Please
select all correct answers?
A) java.awt.TextArea.SCROLLBARS_NONE
B) java.awt.TextArea does not generate Key
events
C) java.awt.TextField generates Key events
and Action events
D) java.awt.TextArea can be scrolled using
the <-- and --> keys.
Answer 30:
31. What is the
result if you try to compile and run the following code ?
public class MyTest {
public static void myTest() {
System.out.println( "Printing
myTest() method" );
}
public void myMethod() {
System.out.println( "Printing
myMethod() method" );
}
public static void main(String[] args) {
myTest();
myMethod();
}
}
A) Compile time error
B) Compiles successfully
C) Error in main method declaration
D) Prints on the screen Printing myTest()
method followed by Printing myMethod() method
Answer 31
32. What line
of a given program will throw FileNotFoundException?
import java.io.*;
public class MyReader {
public static void main ( String
args[] ) {
try {
FileReader
fileReader = new FileReader("MyFile.java");
BufferedReader
bufferedReader = new BufferedReader(fileReader);
String strString;
fileReader.close();
while ( ( strString =
bufferedReader.readLine()) != null ) {
System.out.println ( strString );
}
} catch ( IOException ie) {
System.out.println (
ie.getMessage() );
}
}
}
A) This program never throws
FileNotFoundException
B) The line fileReader.close() throws
FileNotFoundException
C) At instantiation of FileReader object.
D) While constructing the BufferedReader
object
Answer 32
33. When the
following program will throw an IOException?
import java.io.*;
class FileWrite {
public static void main(String args[])
{
try {
String strString = "Now is
the time to take Sun Certification";
char buffer[] = new
char[strString.length()];
strString.getChars(0,
strString.length(), buffer, 0);
FileWriter f = new FileWriter("MyFile1.txt");
FileWriter f1 = f;
f1.close();
for (int i=0; i <
buffer.length; i += 2) {
f.write(buffer[i]);
}
f.close();
FileWriter f2 = new
FileWriter("MyFile2.txt");
f2.write(buffer);
f2.close();
} catch ( IOException ie ) {
System.out.println(
ie.getMessage());
}
}
}
A) This program never throws IOException
B) The line f1.close() throws IOException
C) While writing to the stream
f.write(buffer[i]) throws an IOExcpetion
D) While constructing the FileWriter
object
Answer 33:
34. Which line
of the program could be throwing an exception, if the program is as listed
below. Assume that "MyFile2.txt" is a read only file.
Note: MyFile2.txt is read only
file..
import java.io.*;
class FileWrite {
public static void main(String args[])
{
try {
String strString = "Updating
the critical data section"
char buffer[] = new
char[strString.length()];
strString.getChars(0,
strString.length(), buffer, 0);
FileWriter f = new FileWriter("MyFile1.txt");
FileWriter f1 = f;
for (int i=0; i <
buffer.length; i += 2) {
f1.write(buffer[i]);
}
f1.close();
FileWriter f2 = new
FileWriter("MyFile2.txt");
f2.write(buffer);
f2.close();
} catch ( IOException ie ) {
System.out.println(
ie.getMessage());
}
}
}
A) This program never throws IOException
B) The line f1.close() throws IOException
C) While writing to the stream
f1.write(buffer[i]) throws an IOException
D) While constructing the FileWriter f2 =
new FileWriter("MyFile2.txt");
Answer 34:
35. Select all
the correct answers about File Class?
A) A File class can be used to create
files and directories
B) A File class has a method mkdir() to
create directories
C) A File class has a method mkdirs() to
create directory and its parent directories
D) A File cannot be used to create
directories
Answer 35:
36. Using File
class, you can navigate the different directories and list all the files
in the those directories?
A) True
B) False
Answer 36:
37. Select all
the constructor definitions of "FileOutputStream"?
A) FileOutputStream(FileDescriptor fd)
B) FileOutputStream(String fileName,
boolean append)
C) FileOutputStream(RandomAccessFile raFile)
D) FileOutputStream( String dirName, String
filename)
Answer 37:
38. Select all
correct answers for Font class?
A) new Font ( Font.BOLD, 18, 16)
B) new Font ( Font.SERIF, 24, 18)
C) new Font ( "Serif", Font.PLAIN, 24);
D) new Font ( "SanSerif", Font.ITALIC, 24);
E) new Font ( "SanSerif",
Font.BOLD+Font.ITALIC, 24);
Answer 38:
39. In an
applet programing the requirement is that , what ever the changes you do
in the applets graphics context need to be accumulated to the previous
drawn information. Select all the correct code snippets?
A) public void update ( Graphics g) {
paint( g) ;
}
B) public void update ( Graphics g) {
update( g) ;
}
C) public void update ( Graphics g) {
repaint( g) ;
}
D) public void update ( Graphics g) {
print( g) ;
}
Answer 39:
40. How can you
load the image from the same server where you are loading the applet,
select the correct answer form the following?
A) public void init() {
Image i = getImage (
getDocumentBase(), "Logo.jpeg");
}
B) public void init() {
Image i = getImage ( "Logo.jpeg");
}
C) public void init() {
Image i = new Image ( "Logo.jpeg");
}
D) public void init() {
Image i = getImage ( new Image(
"Logo.jpeg") );
}
Answer 40:
41. Which of
the following answers can be legally placed in the place of XXX?
class Check {
Check() { }
}
public class ICheck extends Check {
public static void main ( String[] args)
{
Object o = new ICheck();
Check i = new ICheck();
Check c = new Check();
if ( o instanceof
XXX) {
System.out.println("True");
}
}
}
A) Object, ICheck only
B) Check , ICheck only
C) Object only
D) Object, Check, ICheck
Answer 41:
42. There are
20 threads are waiting in the waiting pool with same priority, how can
you invoke 15th thread from the waiting pool?.
A) By calling resume() method
B) By calling interrupt() method
C) Calling call() method
D) By calling notify(15) method on the
thread instance
E) None of the above
Answer 42:
43. Select all
the correct answers regarding thread synchronization ?
A) You can synchronize entire method
B) A class can be synchronized
C) Block of code can be synchronized
D) The notify() and notifyAll() methods are
called only within a synchronized code
Answer 43:
44. The thread
run() method has the following code, what is the result when the thread
runs?
try {
sleep( 200 );
System.out.println( "Printing from
thread run() method" );
} catch ( IOException ie) { }
A) Compile time error
B) Prints on the console Printing from
thread run() method
C) At line 2 the thread will be stop
running and resumes after 200 milliseconds and prints
Printing from thread run() method
D) At line 2 the thread will be stop
running and resumes exactly 200 milliseconds elapsed
Answer 44:
45. What is the
result when you compile and run the following code?
import java.awt.*;
public class TestBorder extends Frame {
public static void main(String args[])
{
Button b = new Button("Hello");
TestBorder t = new TestBorder();
t.setSize(150,150);
t.add(b);
}
}
A) A Frame with one big button named
Hello
B) A small button Hello in the center of
the frame
C) A small button Hello in the right corner
of the frame
D) Frame does not visible
Answer 45:
46. Select all
correct answers from the following?
A) public abstract void Test() { }
B) public void abstract Test();
C) public abstract void Test();
D) native void doSomthing( int i );
Answer 46:
47. Please
select all correct statements from the following?
A) toString() method is defined in
Object class.
B) toString() method is defined in Class
class.
C) wait(), notify(), notifyAll() methods
are defined in Object class and used for Thread communication.
D) toString() method provides string
representation of an Object state.
Answer 47:
48. From the
following declarations select all correct variables/method
declarations?
Button bt = new Button ("Hello");
A) public transient int val;
B) public synchronized void Test() ;
C) bt.addActionListener( new ActionListener
() );
D) synchronized ( this ) {
// Assume that "this" is an
arbitrary object instance.
}
Answer 48:
49. Which of
the following classes will throw "NumberFormatException"?
A) Double
B) Boolean
C) Integer
D) Byte
Answer 49:
50. Fill all
the blanks from the following ?
A) Math.abs(3.0) returns 3.0
Math.abs(-3.4) returns --------
B) Math.ceil(3.4) returns --------
Math.ceil(-3.4) returns -3.0
C) Math.floor(3.4) returns --------
Math.ceil(-3.4) returns -4.0
D) Math.round(3.4) returns 3
Math.round(-3.4) returns -------
Answer 50:
51. Select from
the following which is legal to put in the place of XXX?
public class OuterClass {
private String s = "I am outer class
member variable";
class InnerClass {
private String s1 = "I am
inner class variable";
public void innerMethod() {
System.out.println(s);
System.out.println(s1);
}
}
public static void outerMethod() {
// XXX legal code here
inner.innerMethod();
}
}
A) OuterClass.InnerClass inner = new
OuterClass().new InnerClass();
B) InnerClass inner = new InnerClass();
C) new InnerClass();
D) None of the above
Answer 51:
52. If you save
and compile the following code, it gives compile time error. How do you
correct the compile time error?
public class OuterClass {
final String s = "I am outer class
member variable";
public void Method() {
String s1 = "I am inner class
variable";
class InnerClass {
public void
innerMethod() {
int xyz =
20;
System.out.println(s);
System.out.println("Integer value is" + xyz);
System.out.println(s1); // Illegal, compiler error
}
}
}
}
A) By making s1 as static variable
B) By making s1 as public variable
C) By making s1 as final variable
D) By making InnerClass as static
Answer 52:
53. What is the
reason using $ in inner class representation?
A) Because the inner classes are defined
inside of any class
B) Due to the reason that inner classes can
be defined inside any method
C) This is convention adopted by Sun, to
insure that there is no ambiguity between packages and inner classes.
D) Because if use getClass().getName() will
gives you the error
Answer 53:
54. What is the
result when you compile and run the following code?
import java.util.*;
public class MyVector {
public Vector myVector () {
Vector v = new Vector();
return v.addElement( "Adding
element to vector");
}
public static void main ( String
[] args) {
MyVector mv = new MyVector();
System.out.println(mv.myVector());
}
}
A) Prints Adding element to vector
B) Compile time error
C) Runtime error
D) Compiles and runs successfully
Answer 54:
55. What is the
output on the screen when compile and run the following code?
public class TestComp {
public static void
main(String args[]) {
int x = 1;
System.out.println("The
value of x is " + (~x >> x) );
}
}
A) 1
B) 2
C) -1
D) -2
Answer 55:
56. The method
getWhen() is defined in which of the following class?
A) AWTEvent
B) EventObject
C) InputEvent
D) MouseEvent
Answer 56:
57. Select all
correct answers from the following?
A) getSource() method is defined in
java.awt.AWTEvent class
B) getSource() method is defined in
java.util.EventObject class
C) getID() method is defined in
java.awt.AWTEvent class
D) getID() method is defined in
java.util.EventObject class
Answer 57:
58. Which of
the following are correct answers?
A) A listener object is an instance of a
class that implements a listener interface.
B) An event source is an object , which can
register listener objects and sends notifications whenever event occurs.
C) Event sources fires the events.
D) Event listeners fires events.
Answer 58:
59. What are
possible ways to implement LinkedList class?
A) As a HashMap
B) As a Queue
C) As a TreeSet
D) As a Stack
Answer 59:
60. Please
select the correct answer from the following?
public class ThrowsDemo {
static void throwMethod() throws
Exception {
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwMethod();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
A) Compilation error
B) Runtime error
C) Compile successfully, nothing is
printed.
D) inside throwMethod. followed by caught:
java.lang.IllegalAccessException: demo
Answer 60:
Answers
Answer 1:
A) Compile Time error
Explanation:
Generally static methods can be overridden
by static methods only .. Static methods may not be overridden by non
static methods..
Back to Question
1:
Answer 2:
C) for ( int i = 0; i < 1; i++ ) {
System.out.print(args[i]);
}
Explanation:
Answer C) will cause an "ArrayOutOfBoundsException"
if you do not pass the command line arguments to the Java Program. A) and
B) will work without any problem.
Back to Question
2:
Answer 3:
A) Compile time error
Explanation:
The final variables behaves like constants,
so the final variables must be initialized before accessing them. They can
be initialized where they are declared or in every "constructor" if the
class. ( even if class has one or more constructors defined ).
Back to Question
3:
Answer 4:
B) Printing myTest in Test class
followed by Printing myStat in MyTest class
Explanation:
Static methods are determined at compile
time but the non static ( instance methods ) methods are identified at
runtime using Run Time Type Identification ( RTTI).
Back to Question
4:
Answer 5:
A) catch ( InterruptedException ie) {}
B) catch ( IllegalArgumentException il ) {}
C) catch ( IllegalMonitorStateException im
) {}
Explanation:
The wait() method of an Object class throws
InterruptedException when the thread moving from running state to wait
state. If the value of timeout is negative or the value of nanos is not in
the range 0-999999 then wait() method throws IllegalArgumentException
exception at runtime. If the current thread is not the owner of this
object's monitor then it throws IllegalMonitorStateException exception.
Click
here for more information from Java Documentation.
Back to Question
5:
Answer 6:
A) String
C) Double
D) Integer
Explanation:
String, Integer, Double are immutable
classes, once assign a values it cannot be changed. Please refer the
wrapper classes for more information on Integer, and Double.
Back to Question
6:
Answer 7:
A) Prints Hello how are you
Explanation:
Assigning or interchanging the object
references does not change the values, but if you change the values
through object references , changes the values .
Back to Question
7:
Answer 8:
B) Runtime error
C) ArrayOutOfBoundsException
Explanation:
This piece of code throws an
ArrayOutOfBoundsException at runtime . If you modify the code int myArray[]
= new int[1]; to int myArray[] = new int[2]; , it prints 1 on the screen.
The changes you made on the array subscript seen by the caller.
Back to Question
8:
Answer 9:
A) int 100
C) aString
E) Boolean
Explanation:
The byte, strictfp are Java keywords and
cannot be defined as identifiers, the a-Big-Integer has "-" which is not a
valid identifier. The identifiers must starts with letters, $, or _ (
underscore), subsequent characters may be letters, dollar signs,
underscores or digits, any other combination will gives you the compiler
error.
Back to Question
9:
Answer 10:
B) x = b ? y : z ;
Explanation
If b is true the value of x is y, else the
value is z. This is "ternary" operator provides the way to simple
conditions into a single expression. If the b is true, the left of ( : )
is assigned to x else right of the ( : ) is assigned to x. Both side of
the ( : ) must have the data type.
Back to
Question 10:
Answer 11:
A) int a [][] = new int [20][20];
B) int [] a [] = new int [20][];
C) int [][] a = new int [10][];
Explanation:
Multidimensional arrays in Java are just
arrays within arrays. Multidimensional arrays are defined as rows and
columns. The outer array must be initialized. If you look at the answers
the outer arrays are initialized.
Back to
Question 11:
Answer 12:
B) Compiler error , you cannot assign a
value to final variable
Explanation:
In Java final variables are treated as
constants ( comparing to other languages like Visual Basic and etc.) ,
once it is initialized you cannot change the values of primitive, if final
variables are object references then you cannot assign any other
references.
Back to
Question 12:
Answer 13:
B) Contains any number of non-public
classes and only one public class
E) Package statements should appear only in
the first line or before any import statements of source file
Explanation:
The source files always contains only one
package statement, you cannot define multiple package statements and these
statements must be before the import statements. At any point of time Java
source files can have any number of non-public class definitions and only
one public definition class. If you have any import statements those
should be defined before class definition and after the package
definition.
Back to
Question 13:
Answer 14:
D) a = 1, b = -1
Explanation:
The operator >>> is unsigned right shift,
the new bits are set to zero, so the -1 is shifted 31 times and became 1 (
because a is defined as integer ). The operator >> is signed right shift
operator, the new bits take the value of the MSB ( Most Significant Bit )
. The operator << will behave like same as >>> operator. The sifting
operation is applicable to only integer data types.
Back to
Question 14:
Answer 15:
D) 4
Explanation:
The operator is bitwise XOR operator. The
values of a, b, c are first converted to binary equivalents and calculated
using ^ operator and the results are converted back to original format.
Back to
Question 15:
Answer 16:
C) Test() method declaration
Explanation:
The abstract methods cannot have body. In
any class if one method is defined as abstract the class should be defined
as abstract class. So in our example the Test() method must be redefined.
Back to
Question 16:
Answer 17:
D) B b = new C(); // Line 4
Explanation:
According to the inheritance rules, a
parent class references can appear to the child class objects in the
inheritance chain from top to bottom. But in our example class B, and
class C are in the same level of hierarchy and also these two classes does
not have parent and child relationship which violates the inheritance
rules.
Back to
Question 17:
Answer 18:
A) s == s1
B) s.equals(s1);
Explanation:
The string objects can be compared for
equality using == or the equals() method ( even though these two have
different meaning ). In our example the string objects have same wording
but both are different in case. So the string object object comparison is
case sensitive.
Back to
Question 18:
Answer 19:
A) It is a blue print
B) A new data type
D) To provide multiple inheritance
Explanation:
One of the major fundamental change in Java
comparing with C++ is interfaces. In Java the interfaces will provide
multiple inheritance functionality. In Java always a class can be derived
from only one parent, but in C++ a class can derive from multiple parents.
Back to
Question 19:
Answer 20:
C) package com;
import java.awt.*;
// Comments
D) // Comments
package com;
import java.awt.*;
public class MyTest {}
Explanation
In a given Java source file, the package
statement should be defined before all the import statement or the first
line in the .java file provided if you do not have any comments or
JavacDoc definitions. The sequence of definitions are:
// Comments ( if any)
Package definition
Multiple imports
Class definition
Back to
Question 20:
Answer 21:
D) Compile time error
Explanation:
The code fails at the time Math class
instantiation. The java.lang.Math class is final class and the default
constructor defined as private. If any class has private constructors , we
cannot instantiate them from out the class ( except from another
constructor ).
Back to
Question 21:
Answer 22:
A) IOException
B) MalformedURLException
Explanation:
In Java the the URL class will throw "MalformedURLException
while construncting the URL, and while reading incoming stream of data
they will throw IOException..
Back to
Question 22:
Answer 23:
D) Compile time error
Explanation:
In Java the constructors can throw
exceptions. If parent class default constructor is throwing an exception,
the derived class default constructor should handle the exception thrown
by the parent.
Back to
Question 23:
Answer 24:
A) Executing class A constructor
followed by Executing class B constructor
Explanation:
In Java the constructors can throw
exceptions. According to the Java language exceptions, if any piece of
code throwing an exception it is callers worry is to handle the exceptions
thrown by the piece of code. If parent class default constructor is
throwing an exception, the derived class default constructor should handle
the exception thrown by the parent. But in our example the non default
constructor is throwing an exception if some one calls that constructor
they have to handle the exception.
Back to
Question 24:
Answer 25:
C) Compile time error while
multiplication
Explanation:
This does not compile because according to
the arithmetic promotion rules, the * ( multiplication ) represents
binary operator. There are four rules apply for binary operators. If one
operand is float,double,long then other operand is converted to
float,double,long else the both operands are converted to int data type.
So in our example we are trying put integer into byte which is illegal.
Back to
Question 25:
Answer 26:
A) setBounds(), setVisible(), setFont()
B) add(), remove()
C) setEnabled(), setVisible()
Explanation:
The component class is the parent class of
all AWT components like Button, List, Label and etc. Using these methods
you can set the properties of components. The add(), remove() methods are
used to add PopMenu and to remove MenuComponent.
Back to
Question 26:
Answer 27:
C) Frame
Explanation:
Java supports two kinds of menus, pull-down
and pop-up menus. Pull-down menus are accessed are accessed via a menu
bar. Menu bars are only added to Frames.
Back to
Question 27:
Answer 28:
A) Frame's default layout manager is
BorderLayout
D) Canvas has no default behavior or
appearance
Explanation:
In Java AWT each container has it's own
default layout manager implemented as part of implementation. For example
Frame has default layout manager is BorderLayout , Applet has FlowLayout
and etc. The Canvas is kind of component where you can draw custom
drawings. The Canvas generates Mouse, MouseMotion, and Key events .
Back to
Question 28:
Answer 29:
A) void addItem(String s), int getRows()
C) int[] getSelectedIndexes(), int
getItemCount()
D) int[] getSelectedIndexes(), String[]
getSelectedItems()
Explanation:
The java.awt.List has methods to select ,
count the visible rows.
void addItem(String s) --> adds an
item to the bottom of the list
int getRows() -->
returns the number of visible lines in the list
int[] getSelectedIndexes() --> returns
array of indexes currently selected items
int getItemCount() -->
returns the number of items in the list
String[] getSelectedItems() --> returns
array of string values of currently selected items
Back to
Question 29:
Answer 30:
A) java.awt.TextArea.SCROLLBARS_NONE
C) java.awt.TextField generates Key events
and Action events
D) java.awt.TextArea can be scrolled using
the <-- and --> keys.
Explanation:
The TextArea and TextField are the
subclasses of TextComponent class. The TextArea has static fields to give
you the functionality of horizontal and vertical scroll bars. These are
the following fields:
java.awt.TextArea.SCROLLBARS_BOTH
java.awt.TextArea.SCROLLBARS_NONE
java.awt.TextArea.SCROLLBARS_HORIZONTAL_ONLY
java.awt.TextArea.SCROLLBARS_VERTICAL_ONLY
The TextArea and TextField will generate
Key events and TextField will generate Action events apart from the Key
events.
Back to
Question 30:
Answer 31:
A) Compile time error
Explanation:
In Java there are two types of methods ,
static and non static methods. Static methods are belong to class and non
static methods are belongs to instances. So from a non static method you
can call static as well as static methods, but from a static method you
cannot call non static methods ( unless create a instance of a class ) but
you can call static methods.
Back to
Question 31:
Answer 32:
C) At instantiation of FileReader
object.
Explanation:
While constructing the FileReader object,
if the file is not found in the file system the "FileNotFoundException" is
thrown. If the input stream is closed before reading the stream throws
IOException.
Back to
Question 32:
Answer 33:
C) While writing to the stream
f.write(buffer[i]) throws an IOException
Explanation:
While writing to a IO stream if the stream
is closed before writing throws an IOException. In our example the f (
stream ) is closed via f1 reference variable before writing to it.
Back to
Question 33:
Answer 34:
D) While constructing the FileWriter f2
= new FileWriter("MyFile2.txt");
Explanation:
Constructing the FileWriter object, if the
file already exists it overrides it (unless explicitly specified to append
to the file). FileWriter will create the file before opening it for output
when you create the object. In the case of read-only files, if you try to
open and IOException will be thrown.
Back to
Question 34:
Answer 35:
A) A File class can be used to create
files and directories
B) A File class has a method mkdir() to
create directories
C) A File class has a method mkdirs() to
create directory and its parent directories.
Explanation:
The File class has
three methods to create empty files, those are createNewFile(),
createTempFile(String prefix, String suffix) and createTempFile(String
prefix, String suffix, File directory).
File class has two utility methods mkdir()
and mkdirs() to create directories. The mkdir() method creates the
directory and returns either true or false. Returning false indicates that
either directory already exists or directory cannot be created because the
entire path does not exists. In the situation when the path does not
exists use the mkdirs() method to create directory as well as parent
directories as necessary.
Back to
Question 35:
Answer 36:
A) True
Explanation:
File class can be used to navigate the
directories in the underlying file system. But in the File class there is
no way you change the directory . Constructing the File class instance
will always point to only one particular directory. To go to another
directory you may have to create another instance of a File class.
Back to
Question 36:
Answer 37:
A) FileOutputStream(FileDescriptor fd)
B) FileOutputStream(String fileName,
boolean append)
Explanation:
The valid FileOutputStream constructors
are:
- FileOutputStream(String fileName)
- FileOutputStream(File file)
- FileOutputStream(FileDescriptor fd)
- FileOutputStream(String fileName,
boolean append)
Back to
Question 37:
Answer 38:
C) new Font ( "Serif", Font.PLAIN, 24);
D) new Font ( "SanSerif", Font.ITALIC, 24);
E) new Font ( "SanSerif",
Font.BOLD+Font.ITALIC, 24);
Explanation:
The Font class gives you to set the font of
a graphics context. While constructing the Font object you pass font name,
style, and size of the font. The font availability is dependent on
platform. The Font class has three types of font names called " Serif", "SanSerif",
Monospaced" these are called in JDK 1.1 and after "Times Roman",
Helavatica" and "Courier".
Back to
Question 38:
Answer 39:
A) public void update ( Graphics g) {
paint( g) ;
}
Explanation:
If you want accumulate the previous
information on the graphics context override the update() and inside the
method call the paint() method by passing the graphics object as an
argument. The repaint() method always calls update() method
Back to
Question 39:
Answer 40:
A) public void init() {
Image i = getImage (
getDocumentBase(), "Logo.jpeg");
}
Explanation:
The Applet and Toolkit classes has a method
getImage() , which has two forms:
- getImage(URL file)
- getImage(URL dir, String file)
These are two ways to refer an image in
the server . The Applet class getDocumentBase() methods returns the URL
object which is your url to the server where you came from or where your
image resides.
Back to
Question 40:
Answer 41:
D) Object, Check, ICheck
Explanation:
The instanceof operator checks the class of
an object at runtime. In our example o refers to Object class and Check
and ICheck refers to the subclasses of Object class. Due to the
inheritance hierarchy Check and ICheck returns true.
Back to
Question 41:
Answer 42:
E) None of the above
Explanation:
There is no way to call a particular thread
from a waiting pool. The methods notify() will calls thread from waiting
pool, but there is no guaranty which thread is invoked. The method
notifyAll() method puts all the waiting threads from the waiting pool in
ready state.
Back to
Question 42:
Answer 43:
A) You can synchronize entire method
C) Block of code can be synchronized
D) The notify() and notifyAll() methods are
called only within a synchronized code
Explanation:
The keyword controls accessing the single
resource from multiple threads at the same time. A method or a piece of
code can be synchronized, but there is no way to synchronize a calls. To
synchronize a method use synchronized keyword before method definition. To
synchronize block of code use the synchronized keyword and the arbitrary
instance.
Back to
Question 43:
Answer 44:
A) Compile time error
Explanation:
The IOException never thrown here. The
exception is thrown is InterruptedException. To correct instead of
catching IOException use InterruptedException.
Back to
Question 44:
Answer 45:
D) Frame does not visible
Explanation:
The Frame is not going to be visible unless
you call setVisible(true) method on the Frame's instance. But the frame
instance is available in computers memory. If do not set the size of the
Frame you see default size of the frame ( i.e.. in minimized mode)
Back to
Question 45:
Answer 46:
C) public abstract void Test();
D) native void doSomthing( int i );
Explanation:
The abstract methods does not have method
bodies. In any given class if one method is defined as abstract the class
must defined as abstract class.
Back to
Question 46:
Answer 47:
A) toString() method is defined in
Object class.
C) wait(), notify(), notifyAll() methods
are defined in Object class and used for Thread communication.
D) toString() method provides string
representation of an Object state.
Explanation:
The toString() is defined in Object class
the parent all classes which will gives you the string representation of
the object's state. This more useful for debugging purpose. The wait(),
notify(), notifyAll() methods are also defined in Object class are very
helpful for Thread communication. These methods are called only in
synchronized methods.
Back to
Question 47:
Answer 48:
A) public transient int val;
D) synchronized ( this ) {
// Assume that "this" is an
arbitrary object instance.
}
Explanation:
To define transient variables just include
"transient" keyword in the variable definition. The transient variables
are not written out any where, this is the way when you do object
serialization not to write the critical data to a disk or to a database.
The "synchronized" keyword controls the
single resource not to access by multiple threads at the same time. The
synchronized keyword can be applied to a method or to a block of code by
passing the arbitrary object instance name as an argument.
Back to
Question 48:
Answer 49:
A) Double
C) Integer
D) Byte
Explanation:
In Java all the primitive data types has
wrapper classes to represent in object format and will throw "NumberFormatException".
The Boolean does not throw "NumberFormatException" because while
constructing the wrapper class for Boolean which accepts string also as an
argument.
Back to
Question 49:
Answer 50:
A) Math.abs(3.0) returns 3.0
Math.abs(-3.4) returns 3.4
B) Math.ceil(3.4) returns 4.0
Math.ceil(-3.4) returns -3.0
C) Math.floor(3.4) returns 3.0
Math.floor(-3.4) returns -4.0
D) Math.round(3.4) returns 3
Math.round(-3.4) returns -3
Explanation:
The Math class abs() method returns the
absolute values, for negative values it just trips off the negation and
returns positive absolute value. This method returns always double value.
The method ceil(), returns double value
not less than the integer ( in our case 3 ). The other ways to say this
method returns max integer value . ( All the decimals are rounded to 1 and
is added to integer value ). For negative values it behaves exactly
opposite.
The method floor() is exactly reverse
process of what ceil() method does.
The round() method just rounds to
closest integer value.
Back to
Question 50:
Answer 51:
A) OuterClass.InnerClass inner = new
OuterClass().new InnerClass();
Explanation:
The static methods are class level methods
to execute those you do not need a class instance. If you try to execute
any non static method or variables from static methods you need to have
instance of a class. In our example we need to have OuterClass reference
to execute InnerClass method.
Back to
Question 51:
Answer 52:
C) By making s1 as final variable
Explanation:
In Java it is possible to declare a class
inside a method. If you do this there are certain rules will be applied to
access the variables of enclosing class and enclosing methods. The classes
defined inside any method can access only final variables of enclosing
class.
Back to
Question 52:
Answer 53:
C) This is convention adopted by Sun ,
to insure that there is no ambiguity between packages and inner classes.
Explanation:
This is convention adopted to distinguish
between packages and inner classes. If you try to use Class.forName()
method the call will fail instead use getCLass().getName() on an instance
of inner class.
Back to
Question 53:
Answer 54:
B) Compile time error
Explanation:
The method in Vector class , addElement()
returns type of void which you cannot return in our example. The myVector()
method in our MyVector class returns only type of Vector.
Back to
Question 54:
Answer 55:
C) -1
Explanation:
Internally the x value first gets inverted
( two's compliment ) and then shifted 1 times. First when it is inverted
it becomes negative value and shifted by one bit.
Back to
Question 55:
Answer 56:
C) InputEvent
Explanation:
The InputEvent class has method getWhen()
which returns the time when the event took place and the return type is
long.
Back to
Question 56:
Answer 57:
B) getSource() method is defined in
java.util.EventObject class
C) getID() method is defined in
java.awt.AWTEvent class
Explanation:
The super class of all event handling is
java.util.EventObject which has a method called getSource() , which
returns the object that originated the event.
The subclass of EventObject is AWTEvent
has a method getID() , which returns the ID of the event which specifies
the nature of the event.
Back to
Question 57:
Answer 58:
A) A listener object is an instance of a
class that implements a listener interface.
B) An event source is an object , which can
register listener objects and sends notifications whenever event occurs.
C) Event sources fires the events.
Explanation:
The event listeners are instance of the
class that implements listener interface . An event source is an instance
of class like Button or TextField that can register listener objects and
sends notifications whenever event occurs.
Back to
Question 58:
Answer 59:
B) As a Queue
D) As a Stack
Explanation:
This implements java.util.List interface
and uses linked list for storage. A linked list allows elements to be
added, removed from the collection at any location in the container by
ordering the elements. With this implementation you can only access the
elements in sequentially.You can easily treat the LinkedList as a stack,
queue and etc., by using the LinkedList methods.
Back to
Question 59:
Answer 60:
A) Compilation error
Explanation:
The method throwMethod() is throwing and
type Exception class instance, while catching the exception you are
catching the subclass of Exception class.
Back to
Question 60:
|