All Questions


#151What will happen when you attempt to compile and run the following code?

1 import java.util.*;
2 public class Pippin {
3 public static void main(String argv[]){
4 TreeMap tm = new TreeMap();
5 tm.put("one", new Integer(1));
6 tm.put("two",new Integer(3));
7 tm.put("three",new Integer(2));
8 Iterator it = tm.keySet().iterator();
9 while(it.hasNext()){
10 Integer iw = tm.get(it.next());
11 System.out.print(iw);
12 }
13 }

14.}



(1)Compile time error at line 10
(2)Compilation and output of 123
(3)Compilation and output of the digits "1", "2" and "3", but the order can't be determined
(4)Compilation and output of onetwothree
(5)Compilation but runtime error at line 10

Answer : -------------------------

#152The following is part of the code for an application that will run two time consuming processes in two Threads. We want to be sure that all threads get a chance to run.

Which statement at line 9 will accomplish this?

1 public class TestQ20 implements Runnable {
2
3 public static void main (String[] args){
4 new Thread( new TestQ20() ).start();
5 // start some other process here
6 }
7 public void run(){
8 for( int i = 0 ; i < 1000 ; i++ ){
9 // select statement to go here
10 someComplexProcess();
11 }
12 }
13 // remaining methods
14 }


(1)Thread.currentThread().yield();
(2)Thread.sleep(50);
(3)yield();
(4)Thread.yield();

Answer : -------------------------

#153What happens when we try to compile and run the following code.

1 public class TechnoSample {
2 public static void main (String[] args){
3 TechnoSample tq = new DerivedSample();
4 ((DerivedSample)tq).echoN( tq.paramA );
5 }
6 int paramA = 9 ;
7 }

8 class DerivedSample extends TechnoSample {
9 int paramA = 3 ;
10 void echoN( int n ){
11 System.out.println("number is " + n );
12 }
13 }


(1)output of "number is 3"
(2)output of "number is 9"
(3)a compiler error due to the cast in line 4
(4)a compiler error in line 3 due to the lack of a constructor for DerivedSample

Answer : -------------------------

#154You have a working class ClassA that has a method declared as follows:
protected float calculate( int x, int y )


Now you have to extend ClassA with ClassB and override the calculate method with a new version. This new calculate will have to call the processB() method which has the following declaration, where MyException is a custom Exception extending Exception.
protected float processB( int x ) throws MyException


What features should your overriding calculate method in ClassB have?

(1)Define new calculate so that MyException is handled by printing an error mesg:
protected float calculate(int x, int y){
try{
processB(x);
catch(Exception e){
System.out.println("ClassB.calculate throws " + e);
}
..
(2)Declare the new calculate this way:
protected float calculate( int x, int y ) throws MyException
(3)No special exception handling is needed, just make the new calculate private.
private float calculate(int x, int y)
(4)Declare the new calculate this way:
public float calculate(int x, int y) throws RuntimeException

Answer : -------------------------

#155The general contract for the hashCode method states that under certain conditions, the hashCode method for a given object must always produce the same int value as long as data that affects operation of the equals method remains the same.


Which of the following correctly describes the conditions during which this must be true?

(1)During the execution of a single program
(2)Every execution of the program on a given machine
(3)Every execution of the program using a given SDK version
(4)Every execution of the program on any SDK version from 1.4.0 on

Answer : -------------------------

#156The following code fragments show various uses of the key word synchronized.


Which of them could be used to protect a resource against modification by multiple Threads at the same time.


(1)public synchronized class BankAccount {
  // methods and variables to be protected
}
(2)public synchronized void adjustBalance( float f ){
  // variables to be protected are modified here
}
(3)public void adjustBalance( float f ){
   synchronized(f){
    //variables to be protected are modified here
  }
}
(4)private Object lock = new Object();
public void adjustBalance(float f){
  synchronized(lock){
    // variables to be protected are modified here
  }
}

Answer : -------------------------

#157Which of the following statements are true?

(1)A call to a method that throws an exception must always be enclosed in a try/catch block
(2)variables created within a try or catch block will be local to that block
(3)A try statement must always be matched with a catch statement
(4)A finally block may not contain try/catch statements

Answer : -------------------------

#158The strictfp modifier can be applied to what Java entities?

(1)a class
(2)a method
(3)a variable
(4)a code block within a method

Answer : -------------------------

#159Which of the following statements about inner classes is incorrect. (select incorrect statements).

(1)An inner class can have the same name as the enclosing class
(2)An anonymous inner class can only be declared as implementing a single interface
(3)An instance of an inner class that is not static or in a static context can always reference the associated instance of the enclosing class
(4)All anonymous inner classes have Object as their immediate superclass

Answer : -------------------------

#160The following diagram shows the relationship between some classes you have to work with. Note that BaseWidget is declared abstract and that FastWidget is the only class that implements Runnable.

Object
+ BaseWidget (abstract)
+-- SlowWidget
+-- FastWidget (implements Runnable)


The options show signatures of various methods in another class. Choose the methods you would be able to call with an instance of SlowWidget.

(1)methodA(Object obj)
(2)methodB(BaseWidget bw)
(3)methodC(Runnable rr)
(4)methodD(FastWidget fw)
(5)methodE(SlowWidget sw)

Answer : -------------------------

#161The following code has a nested class named Nested. What is the proper format to declare and create an instance of Nested in the main method?

1 public class TopLevel {
2
3 public static void main (String[] args){
4 // what goes here?
5 System.out.println("Created " + nt );
6 }
7
8 static class Nested {
9 String id ;
10 Nested( String s ){ id = s ; }
11 } // end Nested
12 }

Select all options for line 4 that would create a local instance of Nested.

(1)Nested nt = new Nested("one");
(2)TopLevel.Nested nt = new Nested("two");
(3)Nested nt = new TopLevel().new Nested("three");
(4)TopLevel.Nested nt = new TopLevel().new Nested("four");

Answer : -------------------------

#162Which of the following are valid uses of the assert statement? (SCJP 1.4)

(1)assert (i > 10) : System.out.print("i is bigger than 10");
(2)assert (i > 10);
(3)assert (i > 10) : "i is bigger than 10";
(4)assert (i = 10);

Answer : -------------------------

#163Consider the following outline of the declaration of a normal class with an inner class.

public class NormClass {
long startTime ;

public class NestedClass {
// methods and variables of NestedClass
}
// other methods and variables of NormClass
}


Which of the following can be used by a method inside NestedClass to refer to the startTime variable in the enclosing instance of NormClass?

(1)this.startTime
(2)NormClass.this.startTime
(3)this.NormClass.startTime
(4)startTime

Answer : -------------------------

#164What will happen when you attempt to compile and run the following code?

import java.util.*;
public class Laxton{
public static void main(String argv[]){
HashMap hm = new HashMap();
hm.put("1","one");
hm.put("2","two");
hm.put("3","one");
Iterator it = hm.keySet().iterator();
while(it.hasNext()){
System.out.print(it.next());
}
}
}


(1)Compile time error, the HashMap class has an add method not a put method
(2)Compilation but runtime error due to attempt to add duplicate element
(3)Compilation and output of onetwothree
(4)Compilation and output of 123
(5)Compilation and output of 1, 2 and 3 but in an indetermined order

Answer : -------------------------

#165Which of the following are public variables or methods that belong to an instance of Thread?

Do not select static methods or deprecated methods.

(1)yield() method
(2)stop() method
(3)run() method
(4)toString() method
(5)priority - an int variable

Answer : -------------------------

#166Suppose we try to compile and run the following:

public class TechnoSample {
public static void main (String[] args){
int j ;
for( int i = 10, j = 0 ; i > j ; j++ ){
i = i - 1 ;
}
// Statement can go here
}
}

Which of the following statements about this code are correct?

(1)The loop initializer uses the correct form to set the values of i and j
(2)The loop initializer is not legal and the code will not compile for that reason
(3)If we put the following statement after the loop, it would report "i = 5 j = 5"
System.out.println("i = " + i + " j = " + j );
(4)If we put the following statement after the loop, it would report "j = 5"
System.out.println( "j = " + j );

Answer : -------------------------

#167Which of the following statements are true?

(1)The TreeMap class implements the Collection interface
(2)The Vector class allows elements to be accessed using the get(int offset) method
(3)The TreeSet class stores values in sorted order and ensures that the values are unique
(4)The elements in the LinkedHashMap are kept in sorted order

Answer : -------------------------

#168The GenericFruit class declares the following method to return a float number of calories in the average serving size.
public float aveCalories( )


Your Apple class, which extends GenericFruit, overrides this method.
In a DietSelection class which extends Object, you want to use the GenericFruit method on an Apple object instead of the method in the Apple class.

Select the correct way to finish the statement in the following code fragment so that the GenericFruit version of aveCalories is called using the gf reference.

1. GenericFruit gf = new Apple();
2. float cal = // finish this statement using gf


(1)gf.aveCalories();
(2)((GenericFruit)gf).aveCalories();
(3)gf.super.aveCalories();
(4)There is no way to use gf to call the GenericFruit method

Answer : -------------------------

#169What will happen when you attempt to compile and run the following code?

class FrequencyException extends Exception{
public String getMessage(){
return "Frequency Exception";
}
}
public class Note{
public static void main(String argv[]){
Note n = new Note();
System.out.print(n.tune());
}
public int tune(){
try{
return play(444);
}catch(Exception e){
System.out.println(e.getMessage());
}catch(FrequencyException fe){
System.out.println(fe.getMessage());

}finally{
return 2;
}
}
public int play(int iFrequency)throws FrequencyException{
return 1;
}
}


(1)Compile time error problem with catch statements
(2)Compilation and output of 2
(3)Compilation and output of 1
(4)Compile time error, Exception class is final
(5)Compile time error fault with method call after return statement

Answer : -------------------------

#170You have a class, MyThing, that implements a finalize method. You have a method which creates a MyThing array and fills it with MyThing objects. When all of the contents of the array become eligible for garbage collection, which of the following statements about the process of garbage collection are true?

(1)The first MyThing created, as the oldest, will be collected first
(2)The last MyThing created, as the youngest, will be collected first
(3)Java does not guarantee the order of collection
(4)The finalize method of each MyThing will be run as soon as the garbage collection mechanism determines it is unreachable

Answer : -------------------------

#171What happens when we try to compile and run the following code?

public class TestQ38 {
public static void main (String[] args){
Object obj = buildTest( 3 ) ;
System.gc();
System.out.println("exiting");
}

public static TestQ38 buildTest(int n ){
TestQ38 t = null ;
for( int i = 0 ; i < n ; i++ ){
t = new TestQ38( i ) ;
}
return t ;
}

String name ;
public TestQ38( int n ){
name = "Number " + n ;
}

public void finalize(){
System.out.print("finalize " + name ) ;
}
}


Assume that garbage collection runs and all elligible objects are collected and finalized.

(1)It does not compile due to incorrect signature of the finalize method
(2)It compiles but no text is written when it runs due to incorrect signature of the finalize method
(3)Before "exiting" is written, 2 messages from finalize will be printed
(4)Before "exiting" is written, 3 messages from finalize will be printed

Answer : -------------------------

#172Which of the following statements about wrapper classes is correct?

(1)Each wrapper class for an integer primitive, such as Integer and Long, has a max and a min method
(2)The Math class has static max and min methods for integer primitives
(3)Each wrapper class for an integer primitive, such as Integer and Long, has one or more methods to parse values out of Strings
(4)The Math class provides static String parsing methods for all integer primitves

Answer : -------------------------

#173What will happen when you attempt to compile and run the following code?

public class Test {
int iTime=7;
public static void main(String argv[]){
Test t = new Test();
t.calc(10);
}
public void calc(int iTime){
start:
for(int i =0; i < 2; i++){
if(i >1){
break start;
System.out.println(iTime);
}
System.out.print(i);
System.out.print(iTime);
}
}
}


(1)Compile time error
(2)Compilation and output of 010110
(3)Compilation and output of 0717
(4)Compilation and endless output of 010110

Answer : -------------------------

#174What happens when we try to compile and run the following code?

public class Test {

public static final StringBuffer style =
new StringBuffer("original");

public static void main (String[] args){
Test tq = new Test();
tq.modify( style );
System.out.println("Now " + style );
}

public void modify( StringBuffer sb ){
sb.append(" is modified" );
}
}


(1)Compiler objects to modification of a static final variable
(2)Output of "Now original"
(3)Output of "Now original is modified"
(4)Output of "Now is modified"

Answer : -------------------------

#175Consider the variables declared in the following code:

public class Test {

static Object theObj ;
static Object[] someObj ;
static String letters[] = {"A", "B", "C", "D" };
static char[] caps = {'A', 'B', 'C', 'D' };

public static void main (String[] args){
someObj = new Object[ 3 ] ;
int[] theInts = null ;
// what can go here?
}


Select the statements that would cause a compiler error when used to replace the comment.

(1)theObj = letters;
(2)someObj = letters;
(3)theInts = (int[]) caps;
(4)theObj = theInts;

Answer : -------------------------

#176Which three are true for class java.util.ArrayList? (Choose three.)

(1)It can contain duplicates
(2)Its methods are thread-safe
(3)Can be iterated bi-directionally
(4)It implements java.util.Set
(5)It is well-suited for fast random access
(6)It implements java.util.Collections

Answer : -------------------------

#177Given:

1 public class TechnoSample {
2 public static void main(String [] args) {
3 double num = 7.4;
4 int a = (int) Math.abs(num + .5);
5 int b = (int) Math.ceil(num + .5);
6 int c = (int) Math.floor(num + .5);
7 int d = (int) Math.round(num + .5);
8 int e = (int) Math.round(num - .5);
9 int f = (int) Math.floor(num -.5);
10 int g = (int) Math.ceil(num -.5);
11 int h = (int) Math.abs(num - .5);
12
13 System.out.println("" + a + b + c + d + e + f + g + h);
14 }
15 }

What is the result?

(1)56
(2)78787676
(3)78788777
(4)77787776
(5)Compilation fails
(6)An exception is thrown at runtime

Answer : -------------------------

#178Given:

10 int i=3, j=0, result=1;
11 result += i-- * --j ;
12 System.out.println( result );

What is the result?

(1)0
(2)-1
(3)-2
(4)-3
(5)Compilation fails
(6)An exception is thrown at runtime

Answer : -------------------------

#179How can you destroy an object?

(1)null all the references to the object
(2)call Runtime.getRuntime().gc
(3)set all of the object's references to null
(4)call x.remove() , where x is the object's name
(5)call x.finalize() , where x is the object's name
(6)only the garbage collection system can destroy an object

Answer : -------------------------

#180Given:

1 class Bool {
2 static boolean b;
3 public static void main(String [] args) {
4 int x=0;
5 if (b ) {
6 x=1;
7 }
8 else if (b = false) {
9 x=2;
10 }
11 else if (b) {
12 x=3;
13 }
14 else {
15 x=4;
16 }
17 System.out.println("x = " + x);
18 }
19 }

What is the result?

(1)x = 0
(2)x = 1
(3)x = 2
(4)x = 3
(5)x = 4
(6)Compilation fails

Answer : -------------------------

#181
The presence of a mapping for a given key within
this collection instance will not prevent the key
from being recycled by the garbage collector.

Which concrete class provides the specified features?

(1)Vector
(2)Hashtable
(3)TreeMap
(4)TreeSet
(5)HashMap
(6)HashSet
(7)WeakHashMap
(8)None of the above

Answer : -------------------------

#182

class A extends Thread {
public void run() {
try {
synchronized (this) {
wait();
}
} catch (InterruptedException ie) {
System.out.print(interrupted());
}
}
public static void main(String[] args) {
A a1 = new A();
a1.start();
a1.interrupt();
}
}

What are the possible results of attempting to compile and run the program?

(1)Prints: true
(2)Prints: false
(3)Compiler error
(4)Run time error
(5)None of the above

Answer : -------------------------

#183

class C {
public static void main(String[] args) {
Boolean b1 = Boolean.valueOf(true);
Boolean b2 = Boolean.valueOf(true);
Boolean b3 = Boolean.valueOf("TrUe");
Boolean b4 = Boolean.valueOf("tRuE");
System.out.print((b1==b2) + ",");
System.out.print((b1.booleanValue()==b2.booleanValue()) + ",");
System.out.println(b3.equals(b4));
}
}

What is the result of attempting to compile and run the program?


(1)Prints: false,false,false
(2)Prints: false,true,true
(3)Prints: true,false,false
(4)Prints: true,false,true
(5)Prints: true,true,false
(6)Prints: true,true,true
(7)Compile-time error
(8)Run-time error

Answer : -------------------------

#184Which of the follow are true statements?

(1)An anonymous class can extend only the Object class
(2)An anonymous class can not implement an interface
(3)An anonymous class can be abstract
(4)An anonymous class is implicitly final
(5)An anonymous class can be static
(6)The class instance creation expression for an anonymous class must never include parameters
(7)An anonymous class must declare at least one constructor
(8)None of the above

Answer : -------------------------

#185Given:

abstract class TechnoSuper {
abstract void TechnoMethod();
}

class TechnoSub extends TechnoSuper {
void TechnoMethod();
}

Which of the following statements are legal instantiations of a TechnoSuper variable?


(1)TechnoSuper TSup = new TechnoSuper();
(2)TechnoSuper TSup = new TechnoSub();
(3)None of the above

Answer : -------------------------

#186Given the following code, which of the following options if inserted after the comment //here will allow the code to compile without error?

interface Remote{
public void test();

}
public class Moodle{
public static void main(String argv[]){
Moodle m = new Moodle();
}
public void go(){
//here
}
}


(1)Remote r = new Remote(){ public void test(){} };
(2)Remote remote = new Remote();
(3)test();
(4)this.main();

Answer : -------------------------

#187What will be the result if you attempt to compile and run the following code?

public class Flip{
public static void main(String argv[]){
System.out.println(~4);
}
}


(1)Compile time error
(2)compilation and output of 11
(3)Compilation and output of -4
(4)Compilation and output of -5

Answer : -------------------------

#188What will happen when you attempt to compile and run the following code?

public class Mickle extends Thread implements Runnable{
public static void main(String argv[]){
Mickle m = new Mickle();
m.start();

}
public void run(){
go();
}
public void go(){
int i;
while(true){
try{
wait();
System.out.println("interrupted");
}catch (InterruptedException e) {}
}

}
}


(1)Compile time error
(2)Compilation but runtime exception
(3)Compilation but no output at runtime
(4)Compilation and output of "interrupted" at runtime

Answer : -------------------------

#189Given the following code, which of the options if inserted after the comment //here will allow the code to compile without error?

class Wchapel{
String sDescription = "Aldgate";
public String getDescription(){
return "Mile End";
}
public void output(int i ){
System.out.println(i);
}
}
interface Liz{

}
public class Wfowl extends Wchapel implements Liz{
private int i = 99;
public static void main(String argv[]){
Wfowl wf = new Wfowl();
wf.go();
}
public void go(){
//here

}
}


(1)super().output(100);
(2)new Wfowl().output(i);
(3)class test implements Liz{}
(4)System.out.println(sDescription);
(5)new Wfowl();
(6)getDescription();

Answer : -------------------------

#190Which of the following statements are true of the HashMap class?

(1)It does not permit null values
(2)It does not permit null keys
(3)It stores information as key/value pairs
(4)Elements are returned in the order they were added

Answer : -------------------------

#191Which of the following statements are true?

(1)Interface methods cannot be static
(2)Interface methods must have a return type of void
(3)An interface cannot extend another class
(4)An interface method cannot be marked as final

Answer : -------------------------

#192"If two objects return the same hashCode value they must be equal according to the equals method".

True Or False?

(1)True
(2)False

Answer : -------------------------

#193Which of the following statements are true?

(1)An overriden method must have the same return type as the version in the parent class
(2)An overriden method must throw the same exceptions as the version in the parent class
(3)An overriden method must have the same method parameter types and names as that in the parent version
(4)An overriden method cannot be less visible than the version in the parent class

Answer : -------------------------

#194What will happend when you attempt to compile and run the following code?

public class Tux extends Thread{
static String sName = "vandeleur";
public static void main(String argv[]){
Tux t = new Tux();
t.piggy(sName);
System.out.println(sName);
}
public void piggy(String sName){
sName = sName + " wiggy";
start();
}
public void run(){
for(int i=0;i < 4; i++){
sName = sName + " " + i;
}
}
}


(1)Compile time error
(2)Compilation and output of "vandeleur wiggy"
(3)Compilation and output of "vandeleur wiggy 0 1 2 3"
(4)Compilation and probably output of "vandelur" but possible output of "vandeleur 0 1 2 3"

Answer : -------------------------

#195Suppose we have two classes defined as follows:

class Base extends Object implements Runnable
class Derived extends Base implements Observer


Given 2 variables created as follows:

Base base = new Base();
Derived derived = new Derived();


Which of the Java code fragments will compile and execute without errors?

(1)Object obj = base;
Runnable run = obj;
(2)Object obj = base;
Runnable run = (Runnable) obj;
(3)Object obj = base;
Observer ob = (Observer) base;
(4)Object obj = derived;
Observer ob2 = obj;

Answer : -------------------------

#196What happens on trying to compile and run the following code?

1 public class EqualsTest {
2 public static void main(String[] args) {
3 Long L = new Long(5);
4 Integer I = new Integer(5);
5 if (L.equals(I)) System.out.println("Equal");
6 else System.out.println("Not Equal");
7 }
8 }

(1)compiles and prints "Equal"
(2)compiles and prints "Not Equal"
(3)compilation error at line 5
(4)run time cast error at line 5

Answer : -------------------------

#197Given: Derived.java

------------
 
1 public class Base extends Object {
2 String objType;
3 public Base() {
4 objType = "Base";
5 }
6 }
7
8 public class Derived extends Base {
9 public Derived() {
10 objType = "Derived";
11 }
12 public static void main(String[] args) {
13 Derived derived = new Derived();
14 }
15 }

What will happen when this file is compiled?


(1)Two class files, Base.class and Derived.class will be created
(2)compilation error at line 1
(3)compilation error at line 8

Answer : -------------------------

#198Trying to compile the following source code produces a compiler warning to the effect that the variable tmp may not have been initialized.

1 class Demo {
2 String msg = "Type is ";
3 public void showType(int n) {
4 String tmp;
5 if (n > 0) tmp = "positive";
6 System.out.println(msg + tmp);
7 }
8 }

Which of the following line revisions would eliminate this warning?


(1)4 String tmp = null;
(2)4 String tmp = "";
(3)Insert line: 6 else tmp = "negative";
(4)Remove line 4 and insert a new line after 2 as follows:
3 String tmp;

Answer : -------------------------

#199The greek letter Pi in the Unicode used by Java has the value 3c0 in hexadecimal representation.

Write the Unicode literal which would be used to initialize a char variable to Pi. Just write the Unicode literal char, not the complete expression. In other words, what should replace XXXX in the following statement?
char PiChar = XXXX;



Answer : -------------------------

#200The following method definition is designed to parse and return an integer from an input string which is expected to look like "nnn, ParameterName". In the event of a NumberFormatException, the method is to return -1.

1 public int getNum(String S) {
2 try {
3 String tmp = S.substring(0, S.indexOf(','));
4 return (Integer.parseInt(tmp));
5 } catch(NumberFormatException e) {
6 System.out.println("Problem in " + tmp);
7 }
8 return -1;
9 }

What happens when we try to compile this code and execute the method with an input string which does not contain a comma (,) separating the number from the text data?

(1)compilation error @line 6
(2)prints the error message to standard output and returns -1
(3)a NullPointerException is thrown @line 3
(4)a StringIndexOutOfBoundsException is thrown @line 3

Answer : -------------------------


2001-2003 Technopark. Developed by: Giri Mandalika