Java Certification Model Question
& Answer - 2
(Answers are in BOLD)
1. What makes a Thread to stop
execution
-
An interrupted exception is thrown
-
Invoke sleep() method
-
Higher priority thread takes
over
-
While a thread does a read()
using inputstream
2. Which is the correct expression
to be used to read line by line and store the result into a String object
a) File f = new File("test.txt");
-
FileInputStream f = new FileInputStream("test.txt");
-
DataInputStream d = new DataInputStream(new
FileInputStream("test.txt"));
-
BufferedReader br = new BufferedReader(new
InputStreamReader(new FileInputStream("test.txt")));
-
BufferedReader br = new BufferedReader(new
InputStreamReader(new FileInputStream("test.txt", "8859_1)));
3. Which of the following will
not throw a NullPointerException?
String s = null;
-
if ((s!=null) & (s.length()
> 0))
-
if ((s!=null) && (s.length()
> 0))
-
if ((s==null) | (s.length() ==
0))
-
if ((s==null) || (s.length()
== 0))
4. Which of the following describes
a fully encapsulated class?
-
Methods cannot be private
-
Variables cannot be private
-
The state of the object can
only be modified through accessor method.
-
All variables are private.
-
All variables and all methods
are private
5. Which state decide a var. ’a’
which is suitable for referring to an array of 50 string objects?
-
char a[ ] [ ];
-
String a [ ];
-
String [ ] a;
-
Object a [50];
-
String a[50];
6. In the following program, if
somecondition()
is true, then only line number 3 must throw a custom exception MyException.
MyException
is not a subclass of runtime exceptions. What are the changes to be
made to the given method.
-
Public void method() {
-
If (somecondition()) {
-
}
-
}
-
Add throws new MyException();
in line number 3
-
Add throws new MyException();
in line number 5
-
Add throw new MyException();
in line number 3
-
Modify the method declaration
such that an object of type Exception is to be thrown
7. What is the output of the following
program
public class Example{
private int i;
public static void main(String
args[]) {
method() ;
}
public static void method()
{
int j;
j= i;
System.out.println("The value
of i is " +i);
}
}
-
The program prints The value of
i is 0;
-
The program gives a compilation
error. By changing the private int i to public int i the problem gets resolved
-
The program gives a compilation
error. By changing the private int i to private static int i the problem
gets resolved
-
The program gives a compilation
error. By changing the public static of the method to public only and then
calling this method from the main using an instance of Example, the problem
gets resolved.
8. Which of the following are
true?
Long L9 = new Long(9);
Long L10 = new Long(9);
Long L11 = L10;
long A = 9;
long B = 9;
-
L9 == L10
-
L9 == L11
-
L10 == L11
-
A == L9
-
A == B
9. How should we make a class
to handle the events generated by a single user interface component?
-
Subclassing a adapter class is
inappropriate in this case - False
-
Implements class which handles
all the user interface listener methods - True
10. What are the assignments which
are valid in the line XXXX
class Super{
private int i;
public int ma_cd(float f){
i = (int) f;
return i; }}
class Sub extends Super{
int j;
float g;
public Sub(){
Super s = new Super();
XXXX }
}
-
j = i;
-
g = f;
-
j = s.ma_cd(3.2f);
-
j = s.i;
-
g = s.f;
11. What is the output of the
above code?
outer : for(int x = 0 ; x<2
; x++){
inner: for(int a = 0 ; a <
2 ; a++){
if (a==1) {
break inner;
}
System.out.println(" a is "
+ a + " x is " + x );
}
}
-
a is 0 x is 0
-
a is 0 x is 1
-
a is 0 x is 2
-
a is 1 x is 0
-
a is 1 x is 1
-
a is 1 x is 2
-
a is 2 x is 0
-
a is 2 x is 1
-
a is 2 x is 2
12. Which is the earliest possible
instance that Garbage Collection is activated for String created in line
1.
public static void main(String
args[ ]){
-
String s = "abcd";
-
String s1 = "efghi";
-
String s2 = s+ s1;
-
s = null;
-
s = s1;
-
s1 = s;
-
Before Line 3.
-
Before Line 4.
-
Before Line 5.
-
Before line 6.
13. There is a plan to prepare
a class which is going to used in many unrelated parts of the project.
The class should be Polygon object which is a Shape. The polygon has a
information about the coordinates stored in a vector, color status which
states whether true or false. Write a class declaration which describes
the above
public class Polygon extends
Shape{
vector coord;
boolean colorstatus;
}
14. There is an Employee who is
a Person. The employee stores details in a Vector, date employed and number
of instances. What are the type of
variables that is to be added
to the Employee class
-
Vector
-
Date
-
Object
-
Person
-
Employee
-
Int
15. What is the constructor argument
for the FilterInputStream
-
File
-
InputStream
-
DataInputStream
-
BufferedReader
-
InputStreamReader
16. What is the hexadecimal representation
of 7 (not more that 4 characters)
0x7
17. What is the output of the
following program Example if args[0] is printed
java Example cat dog
-
dog
-
cat
-
Example
-
java
-
Null PointerException is thrown
18. What is the modifier for all
the listener methods
-
private
-
default (no access modifier specified)
-
protected
-
public
-
static
19. Which of the following is
true regarding GridBagLayout
-
if a component has been given
fill both is true, then as the container resizes the component resizes
-
The number of rows and columns are fixed while loading the components.
-
The number of rows and columns are fixed while loading the layout itself
.
-
if a component has a non-zero weighty value, then the component grows
in height as the container is resized.
20. What is the value of x, if
"Test 3" to be printed
Switch(x){
Case 1: System.out.println("Test
1");
Case 2:
Case 3: System.out.println("Test
3"); break;
default : System.out.println("Test
4"); break;
}
-
1
-
2
-
3
-
4
-
0
21. If the string array name is
argc in the arguments in the main method, which is invoked by the interpreter
when a program is executed
-
char string [ ] [ ]
-
char [ ] a [ ]
-
char a[ ]
-
String argc
-
String argv[ ]
-
String argc[ ]
22. What is the argument of the
KeyListener methods
KeyEvent
23. What is the correct declaration
of an abstract method
-
public abstract method(); // no
ret type
-
public void abstract method():
// position ret type
-
public abstract void method()
{} // no body
-
public final abstract void method();
// final abs cannot come together
-
public abstract void method
() ;
24. How will you override or overload
the following method method1 of the Super class in the class Sub?
class Super{
protected void method1() {}
}
class Sub extends Super{}
-
public void method1() { return
0; }
-
public int method1() { return
0; }
-
public void method1(StringBuffer
s) { }
-
public void method1() { }
-
public void method1(String
s) { }
25. What is the value of x if
Test 3 is to be printed
if (x > 4) {
System.out.println( "Test
1" );
} else if (x> 9){
System.out.println("Test 2");
} else
System.out.println("Test 3");
-
0 to 4
-
Less than 0
-
5 to 9
-
Greater than 10
26. What is the range of a char
type?
0 – 216-1
27. What does >> and >>> denotes
-
>> is right shift >>> is round
-
>> is signed right shift >>> round
-
>> is signed shift and >>>
is unsigned shift
-
>> is unsigned >>> is unsigned
28. What are valid keywords
-
NULL
-
TRUE
-
implements
-
interface
-
instanceof
-
sizeof
29. Which of the following should
be used to have No Order, Duplication or Perfect retrieval mechanism
-
Map // no order, no dupe, perfect
retrive
-
List //order, dupe, no ret
-
Set //no order, no dupe, not perfect retrieve
-
Collection //no order, dupes, no ret
30. In which LayoutManager, a
component to be added to have only the width resized but not the height
-
FlowLayout
-
GridLayout
-
GridBagLayout
-
East or West of BorderLayout
-
North or South of BorderLayout
31. What is the access modifier
for a class member variable which is to be used in the same package where
the class is defined
-
protected
-
private
-
public
-
no access modifier
-
static
32. What is the body of a Thread
-
run
-
begin
-
execute
-
start
-
resume
33. What is the output of the
following code?
int i = 0;
do {
System.out.println("The value
of i is " + i);
}while(--i > 0)
System.out.println("Finished");
-
The value of i is 1
-
The value of i is 0
-
Finished
-
Compilation Error ( no ; after
while condn);
-
Runtime Error
34. What is the output of the
following program
class Example{
static int arr[] = new int[10];
public static void main(String
args[]){
System.out.println(" The value
4th element is " + arr[4]);
} }
-
The value of 4th element
is 4
-
The value of 4th element
is null
-
The value of 4th
element is 0
-
Null Pointer exception is raised
-
Runtime error, because the class
is not instantiated
35. Which of the following is
the correct class declaration for Car.java. See to that it is a case-sensitive
system
-
public class Car{
int in;
public Car(int inn){
in = inn
} }
-
public class Car extends Truck,
Vehicle{
int in;
public Car(int inn){
in = inn;
} }
-
public class Car{
int in;
public abstract void raceCar()
{System.out.println("RaceCar");}
public Car(int inn){
in = inn;
} }
-
import java.io.*;
public class Car extends Object{
int in;
public Car(int inn){
in = inn;
} }
36. Which of the following
are true about Threads?
-
Threads can only be created by
extending the java.lang.Thread
-
Threads of the same program end
together
-
A thread which is suspended, cannot
be restarted
-
Uncoordination of threads,
will result in the data be corrupted
-
Java Interepter will exit only
when all non-demon threads are not ended
37. What is true about Garbage
Collection mechanism
-
Garbage Collection releases memory
at predictable times and at predictable rates
-
A correct program is one in
which does not depend on the memory release by the garbage collector
-
A programmer can indicate to
the gc mechanism by making reference to a variable as null
-
Garbage collection ensures that
there may not be any leakage of memory.
38. A question on Equality operator
Ok
39. What is the output of the
following program?
class Super{
String name;
Super(String s){
name =s ;
} }
class Sub extends Super{
String name;
Sub(String s){
name=s;
}
public static void main(String
args[]){
Super sup = new Super("First");
Sub sub = new Sub("Second");
System.out.println(sub.name
+ " " + sup.name);
}
}
-
First Second
-
Second First
-
Compilation error
-
Runtime error stating same name
found in Super as well as in the Sub class
40. An IOException needs to be
thrown in some method of a program. Select the correct expression for the
raising an exception
-
new Exception();
-
throw Exception();
-
throw new(new IOException());
-
throw new Exception(); // its
actually throw new IOException();
-
throws new Exception();
41. A Question on Method Overriding
OK
42. What is the output of the
following program if method1() does not throw any exception?
try{
method1();
System.out.println("First");
}catch (Exception e){
System.out.prinln("Second");
}finally{
System.out.println("Finally");
} System.out.println("Last");
-
First followed by Finally
-
First followed by Finally followed
by Last
-
First followed by Last
-
First
43. What are the modifier which
cannot be used for a local automatic variable
-
default ( no access modifier )
-
final
-
static
-
public
44. What does getID() method of
the event return
-
time of the event
-
source of the event
-
nature of the cause of the
event
-
place of the event
45. What cannot be added to a
container
-
Applet
-
Panel
-
Component
-
Container
-
MenuItem // can be added only
to a Menu
46. How to invoke the constructor
from a constructor in the same class
class Super{
float x;
float y;
float z;
Super(float a, float b){
x = a;
y = b;
}
Super(float a, float b, float
c){
XXXX
z = c;
}
}
-
super(a,b);
-
Super(a,b);
-
this(a,b);
-
This(a,b);
47. What is true about inner classes?
-
Inner classes have to be instantiated
only in the enclosing class
-
Inner classes can access all
the final variables of the enclosing class
-
Inner classes cannot be static
-
Inner classes cannot be anonymous
class
-
Inner classes can extend another
class.
48. What are the correct declaration
for an inner class
-
private class C
-
new simpleinterface() { //Anonymous
so v can use new
-
new complexinterface(x) {
-
new innerclasses() extends someotherclass {
-
new innerclasses extends someotherclass { //Not as we have given a name
and so v cannot use new
49. What are true about Listener
interfaces
-
A component can have only one
listener attached to it
-
If a component has multiple
listeners attached, the order in which the listeners are invoked are unknown
-
If a component has multiple listeners,
then all the listeners have to be friends
50. If a class member variable
is initialized and the value is not be changed at all, what is the modifier
to be used during the declaration of the variable
-
const
-
fixed
-
public
-
static
-
final
51. What will happen if the following
assignment is made, take the above into account?
class Parent
class Derived1 extends Parent
class Derived2 extends Parent
Parent p;
Derived d1;
p = d1;
-
Legal at compile time and illegal
at run time
-
Legal at compile time and possibly
legal at run time
-
Legal at compile time and definitely
legal at run time
-
Illegal at compile time
52. Which modifiers are to be
used to obtain the lock on the object
-
public
-
private
-
static
-
synchornized
-
lock
53. What can be possibly used
at Point X
// Point X
public class Example
-
import java.awt.*;
-
package local.util;
-
class NoExample{}
-
protected class SimpleExample
-
public static final double PI = 3.14;
54. What is the range of int type
-231 to 231-1
55. What is the output of the
following program?
class Super{
String firstname;
String secondname;
Super(){}
Super(String f, String s){
firstname = f;
secondname = s;
}
public String toString(){
return "It is Monopoly Mr.
" + firstname;
}}
class Sub extends Super{
String firstname;
String secondname;
Sub(String f, String s){
firstname = f;
secondname = s;
}
public String toString(){
return "It is Monopoly Mr.
" + secondname + " "+ firstname;
}}
class Test{
public static void main(String
args[]){
Super first = new Super("Scott",
"McNealy");
Super second = new Sub("Bill","Gates");
System.out.println( second
+ " \n " +first);
}}
-
It is Monopoly Mr. Bill Gates
It is Monopoly Mr. Scott
-
It is Monopoly Mr. Gates Bill
It is Monopoly Mr. McNealy
-
It is Monopoly Mr. Gates Bill
It is Monopoly Mr. Scott
-
It is Monopoly Mr. Gates
It is Monopoly Mr. Scott
McNealy
56. How to instantiate the class
Inner at point XXXX
class Outer{
class Inner{
}
public static void main(String
args[]){
XXXX
}
}
-
new Inner();
-
Inner i = Outer.new Inner();
-
Outer.Inner i = new Outer().new
Inner();
-
Inner i = Outer(new Inner());
57. What is the output of the
following program
public class Example{
Stack s1;
Stack s2;
public static void main(String
args[]){
new Example();
}
public Example() {
s1 = new Stack();
s2 = new Stack();
method1(s1,s2);
System.out.println(s1 + " "
+ s2);
}
public void method1(Stack ms1,
Stack ms2){
ms2.push(new Long(100));
ms1 = ms2;
}
}
-
Compilation error since ms2 cannot
be assigned to ms1.
-
s1 [ ] s2 [ ]
-
s1 [ ] s2 [100]
-
s1 [100] s2 [100]
58. What are the legal identifers
in Java?
-
%employee
-
$employee
-
employ-ee
-
employee1
-
_employee
59. Which of the following are
true about the listener mathods in AWT? (C)
-
In awt listener menthods generally
takes an argument which is an instance of some subclass of java.awt.AWTEvent
-
When multiple listeners are
added to a single component the order of invocation of the listeners is
not specified.
-
A single component can have
multiple listeners added to it.
Do you have a Java Problem?
Ask It in The Java
Forum
Java Books
Java Certification,
Programming, JavaBean and Object Oriented Reference Books
Return to : Java
Programming Hints and Tips
All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
All product names are trademarks of their respective
companies.
The site www.erpgreat.com is not affiliated with or endorsed
by any company listed at this site.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
The content on this site may not be reproduced
or redistributed without the express written permission of
www.erpgreat.com or the content authors.
|