www.studyjava.org inteview home
1. Which declarations are true about inner classes?
A.
new InnerClass(){
B.
public abstract class Innerclass{
C.
new Ineerclass() extends Mainclass{
2. Which access modifier is used to restrict the methods scope to itself and still allows other classes to subclass that
class? A.
private B.
final C.
protected D.
friend 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); A.
f1 == f2; B.
f2 == d; C.
f1.equals(f2); D.
f2.equals(f1) E.
f2.equals(d); // will return false not error 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 D.
Compilation is legal probably okay during execution 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 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?