Java Certification Model Question & Answer - 3
(Answer are in BOLD)


1. Which declarations are true about inner classes?

  1. new InnerClass(){
  2. public abstract class Innerclass{
  3. new Ineerclass() extends Mainclass{

  4.  

     
     
     
     
     

2. Which access modifier is used to restrict the methods scope to itself and still allows other classes to subclass that class?
  1. private
  2. final
  3. protected
  4. friend

  5.  

     
     
     
     
     

3. Which one of the following will equate to true?

Float f1 = new Float(0.9f);

Float f2 = new Float(0.9f);

Double d = new Double(0.9);

  1. f1 == f2;
  2. f2 == d;
  3. f1.equals(f2);
  4. f2.equals(f1)
  5. f2.equals(d); // will return false not error

  6.  

     
     
     
     
     

4. Which statement below is true regarding the above code?

The following shows class hierarchy

Derived1 , Derived2 extends from Mainclass.

Mainclass m;

Derived1 one;

Derived2 two;

one = (Derived1) m;

A. Compilation error

B. Comiplation is legal but generates runtime error

C. Compilation is legal but generates ClassCastException during execution

  1. Compilation is legal probably okay during execution

  2.  

     
     
     
     
     

5. What will be printed when the follwoing code is executed?

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

inner : for ( int j = 1;j<3;j++){

if ( j ==2) continue outer; System.out.println("i is "+i+" j is "+j);

}

}

A. i = 1 j = 1

B. i = 1 j = 2

C. i = 1 j = 3

D. i = 2 j = 1

E. i = 2 j = 2

F. i = 2 j = 3

G. i = 3 j = 1

H. i = 3 j = 2

I. i = 3 j = 3

6. Check the correct class decalration for the Car.java

A. public class Car{

static int x =0;

public static void method(int y){

x = y;

} }

B. public class Car extends Myclass,Hisclass{

static int x;

public void method(int y){

x = y;

} }

C. public class Car {

static int x;

public abstract void method(int y){

x = y;

} }

D. import java.awt.*;

public class Car extends Object{

static int x;

public void method(int y){

x = y;

} }

7. Employee is a Person. Employee has details stored in Vector, name as String and educated as boolean flag. Using the words given below make correct signature for Employee class.

Import static void extends Employee public class Person Vector String

public class Employee extends Person

8. Shape is a Drawable one.Shape has details stored in Vector, fill color and flag having boolean state.

What will be included in the construction of Shape class?

A. Drawable

B. Vector

C. String

D. Color

E. boolean

F. int

9. The class definition is as below:

1 public class Test{

2 public static void main(String args[]){

3 StringBuffer sb1 = new StringBuffer("Hello");

4 StringBuffer sb2 = new StringBuffer("I am here");

5 method(sb1,sb2);

6 System.out.println("sb1 "+sb1+" sb2 "+sb2); // this is ok

7 }

8 static void method(StringBuffer s1,StringBuffer s2){

9 s1 = s1+"hello"; // this is a error

10 s2 = s1;

11 }

12 }

What will be the output of the above ?

A. Compilation error at line 5

B. Compilation error at line 6 as StringBuffer does not override '+' operator

Code compiles and output will be

sb1 Hello sb2 I am here

D. Code compiles and output will be

sb1 Hellohello sb2 Hellohello

10. Which modifier will you use to restrict the access of instance variable to the class only and not to any other classes?

A. protected

B. default

C. final

D. private

11. What is the octal representation of 7 (Answer within 4 characters )

07

12. Class Test is defined as below

public class Test{

int x,y;

public Test(int a,int b){

// some complex calculations

x = a;

Y = b;

}

public Test(int a, int b, String s){

// some complex calculations as two integer constructor

// and finally assigns x =a, y=b

System.out.println("sdfd");

}

}

Write a line of code in the 2nd constructor such that 1st constructor codings need not be repeated

this(a,b);

13. What will happen if you try to compile and run the following program

import java.awt.*

import java.awt.event.*;

public class Test extends Frame implements ActionListener{

public Test(){

Button b = new Button("Press");

String s = "Message";

b.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent evt){
System.out.println("Message is "+s); // s is not final! } } );

add(b,BorderLayout.NORTH);

setSize(300,300);

setVisible(true);

}

public static void main(String args[]){

Test t = new Test();

}

}

A. Compilation error while adding Button as BorderLayout is not default layout of Test

B. Compilation error at adding ActionListener

C. Compilation error at actionPerformed method as 's' is not accessible inside this method

D. Message is Message is printed on pressing Button

14. The classes are defined as follows in different java files

public class First{

static int x;

public void method(int y){

x = y;

}

}

public class Second extends First{ }

What are the legal method declarations in Second class?

A. int method(int x){}

B. public void method(String x){}

C. public abstract void method(int y);

D. int anotheeMethod(int x){}

15. The following code lists class hierarchy

class First{

String primary,secondary;

public First(String s1,String s2){

primary = s1;

secondary = s2;

}

String toString(){

return primary; }

}

class Second extends First{

String primary,secondary;

public Second(String s1,String s2){

super(s1,s2);

primary = s1;

secondary = s2;

}

String toString(){

return primary + secondary; }

public static void main(String args[]){

First f1 = new First("First","1st");

First f2 = new Second("Second","2nd");

System.out.println(f1.toString());

System.out.println(f2.toString());

} }

What will be the output?

A. First 1st

Second 2nd

B. First

Second 2nd

C. First 1st

First 1st

D. Second 2nd

Second 2nd

16. Where a button to be placed so that its width remains same and height changes on resizing?

A. Using GridLayout with single Button

B. Placing in North,South in Borderlayout

C. Placing in East,West in BorderLayout

D. Placing at center in FlowLayout

17. What is true about GridBagLayout? (Radio button)

A.If component has to be the last one i a row the gridX value should be REMAINDER

B.Fill value has no effect if anchor value is set to center

C.Anchor value has no effect if Fill Value is Both
 
 

18. What will be the output?

String s = "hello";

String s1 = "there";

s.concat(s1);

s.toUpperCase();

s += "here";

System.out.println(s);

A. hello THERE

B. hello there

C. hello here

D. HELLO THERE here

E. HELLO here

19. Which modifier for a local variable cannot be used inside a method declarations ?

A. final

B. public // No Access modifiers allowed

C. default

20. A Socket s is established with a port and the output from the Socket to be read as lines of data from the socket which one you will use?

A. FileInutStrean f = new FileInputStream(s.getInoutStream());

B. DataInputStream d = new DataInputStream(s.getInputStream());

C. ByteArrayInputStream b = new ByteArrayInputStream(s.getInputStream());

D. BufferedReader b= new BufferedReader(s.getInputStream());

E. BufferedInputStream bis = new BufferedInputStream(s.getInputStream(),"8859_1");

21. What is the argument for all MouseMotionListener interface methods?

MouseEvent

22. Part of main method is defined as follows:

{

int i =0;

while(i-- > 0){

System.out.println("i is "+i);

}

System.out.println("Finished");

}

What will be the output of the above code?

A. i is 0

B. Infinite loop

C. Finished

D. i is 1

23. getID() method of AWTEvent refers to what?

Nature or Type of Event 24. What are the correct declarations for 2 dimensional ararys?

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

B. int []a[] = new int[4][4];

C. int a[][] = new int[4][4];

D. int a[4][4] = new int[][];

E. int [][]a = new int[4][4];

25. What causes current thread to stop executing?( which statements are true about thread?)

A. Threads created from same class finish together

B. Thread can be created only by subclassing java.lang.Thread

C. Thread execution of specific thread can be suspended indefinitely if required.

D. Java interpreter exits when main thread exits even if the other threads are running.

E. Uncoordination of multiple threads will affect data integrity .

26. What statements are true about gc?

A. gc releases memory at predictable rates.

B. gc requires additional code in case multiple threads are running

C. Programmer has a mechanism that explicitly & immediately frees memory used by java objects

D. gc system never reclaims memory from objects which are still accessible to a running user thread

27. What are true about Listeners?

A. Return value is boolean

B. Most components allow multiple listeners to be added

C. A copy of original event passed to listener method

D. Multiple listeners added to single component, they must be made friends

E. The order of invocation of the listener is specified to be in which they are added

28. What is the correct way of declaring native methods?

A. public abstract native method() {}

B. public native void method();

C. native void method(){}

D. public native void method() {}

29. What are the java keywords?

A. friendly

B. extends

C. synchronized

D. sizeof

E. interfaceof

30. What is the range for char variable?

0 to 216-1

31. What is the range for int variable?

-231 to 231-1

32. Which are valid java identifiers?

A. thisfinal

B. %great

C. intern

D. 3fun

  1. z_fal

  2.  

     
     
     
     
     

33. The main method for class Test is given below:

try{

state();

}

catch(ArithmeticException ex)

{

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

finally{

Sytem.out.println("finally"); }

System.out.println("done");

What will be the output of the above code (Choose the correct one) if method state() throws NullPointerException?

A. Arithmetic

B. finally

C. done

D. Exception not caught

34. At what point will the String referenced at line 1 is available for garbage collection in this method?

1. String s1 = "abc";

2. String s2 = "bdc";

3. s1.concat(s2);

4. s1 = null;

5. s1 += s2;

6. System.out.println(s1);

A. Just Before 4

B. Just Before 5

C. Just Before 6

D. never

35. Which cannot be added to Container?

A. Applet

B. Panel

C. Container

D. MenuComponent

36. Which of the following code statement will throw NullPointerException

String s = "hello";

s == null;

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

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

c. if(s != null || s.length() >0)

d. if(s == null | s.length() >0)

37. which of the following are true?

class x {

int x;

public static void main(String args[]) {

x = 10;

System.out.println(" value of x "+x);

}

}

a. prints "value of x 10"

b. compilation error

c. Runtime Error

38. what are the valid codes that come in //Point X place declared in Test.java

// Point X

public class Test {}

a. import java.awt.*;

b. package local.util;

c. class someclass {}

d. protected class myclass {}

e. private static final int more = 1000;

39. Which of the following evaluate to true?

float f = 10.0f

long l = 10L

a. f == 10.0f

b. f = 10.0 // 10.0 is a double value

c. f == 10.0 // converted to parent class, here double

d. f == l

e. l == 10.0

40. FilterInputStream is subclassed by DataInputStream, BufferedInputStream, ByteArrayInputStream.

What is the valid argument for FilterInputStream constructor?

a. File

b. FileInputStream

c. PrintStream

d. BufferedReader

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.