Showing posts with label SCJP. Show all posts
Showing posts with label SCJP. Show all posts

Monday, February 6, 2012

SCJP model question answer pagr 8


70)
if(0.0 == -0.0) {
  System.out.println("true");
}
else{
    System.out.println("false");
}

prints false

prints true

Ans: 2

71)
class C {
public static void main(String[] args) {
double d1 = Math.floor(0.5);
double d2 = Math.floor(1.5);
System.out.print(d1 + "," + d2);
}}

Prints: 0.0,1.0

Prints: 0.0,2.0

Prints: 1.0,1.0

Prints: 1.0,2.0

None of the above

Ans: 1


72)
System.out.println("String".substring(0,4));
This statement will Print

will print "Strin"

will print "Stri"

will cause compiler error

none of the above

Ans: 2


73)
if("String".replace('t','T') == "String".replace('t','T'))
System.out.println("Equal");
else
System.out.println("Not Equal");

will Print Equal

will Print Not Equal

compile time error

none of the above

Ans: 2


74)
Which of the following classes  will not allow unsynchronized read operations by multiple threads?

Vector

TreeMap

TreeSet

HashMap

HashSet

Ans: 1

75)
StringBuffer objects once created can not be modified

true

false

Ans: 2

SCJP model question answer page 7


61)
interface I{
final class C1{     //1  
                static int i=9;;  //2
                }
}
class C2 implements I{
 public static void main(String a[]){
                System.out.println(I.C1.i);  ///3
}
}

compile time error at line 1

compile time error at line 2

compile time error at line 3

prints 9

Runtime exception

Explanation:
interfaces classes are by default static,final. so,no compile time errors are genearated
Ans: 4


62)
class C{
public static void main(String[] args) {
  try
    {
        try
           {
               try
                 {
                   }
                 catch(RuntimeException e)
                 {
                  }
             }
           catch(Exception e)
           {
            }
             
      }
    catch(NullPointerException e)
    {
        }
     finally
    {
              System.out.println("finally");

      }
}}


Prints finally

compile time error

Runtime Exception

None of the above

Ans: 1

63)
class C {
  public static void main ( String ka [ ] ) {
          while ( false ) {
                  System.out.println ( "Value" ) ;
          }
  }
}

compile time error

prints Value infinitely

Runtime Exception

None of the above

Ans: 1


64)
class C {
            public static void main(String[] args)
            {
              System.out.println(4+5+"String");
            }
}

prints 9string

prints 45string

compile time error

Runtime exception

None of the above

Explanation:
arguments to the method are evalutated from left to right so 4+5+"string" ==> 9+string==>9string
Ans: 1


65)
class H {
  public static void main (String[] args) {
    String s1 = "HHH";
    StringBuffer sb2 = new StringBuffer(s1);
    System.out.print(sb2.equals(s1) + "," + s1.equals(sb2));
}}


Prints: false,false

Prints: true,false

Prints: false,true

Prints: true,true

None of the above

Explanation:
s1 and sb2 are pointing to different object references
Ans: 1


66)
The relationship between a class and its superclass is
has-a

is -a

None of the above
Ans: 2

67)

class A extends Thread {
  private int i;
  public void run() {i = 1;}
  public static void main(String[] args) {
    A a = new A();
    a.run();
    System.out.print(a.i);
}}
How many threads are created in this Program?

Ans: 1

Ans: 2

Ans: 3

Ans:None of the above
Explanation:
Main thread is a thread and calling run methods will not creat thread.
Ans: 1

68)
String objects once created can not be modified
true

false

Ans: 1


69)
Which of the following modifiers can be applied to a class that is not a nested class?


public

protected

private

static

Ans: 1

SCJP model question answer page 6


51)
Which of the following methods are static members of the Thread class?

join

run

sleep

start

wait

Ans: 3


52)
class C {
public static void main(String[] args) {
  try
    {
        int i1=3/0;
    }
    System.out.println("hai");
    catch(NullpointerException e)
    {
     }
    finally
    {
              System.out.println("finally);
    }
}
}

compile time errors

print hai and finally

the code compiles and runs fine

none of the above

Ans: 1


53)
class c1
{
            public static void main(String a[])
            {
                System.out.println(Double.NaN==Double.NaN);
                System.out.println(Double.NaN!=Double.NaN);
                System.out.println(Float.NaN==Double.NaN);
            }
}


prints false true false

print true false true

prints true true true

prints false false false

compiletime error

Ans: 1


54)
class C {
  public static void main ( String ka [ ] )
            {
                      Thread t = Thread . currentThread ( ) ;
                      t . setPriority ( - 1 ) ;
                      System . out . println ( " Done ! " ) ;
             }
};


compile time error

Runtime Exception

The code compiles and runs fine

None of the above

Ans: 2


55)
class c1
{
   public void m1()
    {
       System.out.println("m1 method in C1 class");
     }
}
class c2
{
  public c1 m1()
   {
     return new c1(){
       public void m1()
          {
              System.out.println("m1 mehod in anonymous class");
          }
};
}
public static void main(String a[])
 {
        c1 ob1 =new c2().m1();
        ob1.m1();
}
}

prints m1 method in C1 class

prints m1 method in anonumous class

compile time error

Runtime error

none of the above

Explanation:
the anonymous class overrides the m1 method in c1.so according to the dynamic dispatch the m1 method in the anonymous is called
Ans: 2


56)
class c1 extends Exception
{
}
class c2
{
   static void m1()
    {
       throw new c1();
     }
  public static void main(String a[])
   {
         
    }
}


compile time error

Runtime Exception

The code compiles and runs fine

None of the above

Ans: 1


57)
class C {
            static void m1(Object x) {System.out.print("Object");}
            static void m1(String x) {System.out.print("String");}
            public static void main(String[] args) {
            m1(null);
            }
}

Prints Object

Prints String

compiletime error

None of the above

Explanation:
The more specific of the two, m(String x), is chosen
Ans: 2


58)
all classes of the java.lang package are automatically imported

true

false

Ans: 1

59)
The constructor for the Math class is private, so it cannot be instaniated

true

false

Ans: 1


60)
class C {
public static void main(String[] args) {
 int x=2;
 int y=3;
   if((y==x++)|(x<++y)){                    
         System.out.println(x+""+y);
        }
}}

prints  34

prints  33

compile time error

Runtime exception

None of the above

Ans: 1

SCJP model question answer 5

41)
abstract class C1{
public void m1(){        //1
}}
abstract class C2{
 public void m2(){      //2
}}

compile time error at line1  

compile time error at line2

The code compiles fine

None of the above

Explanation:
since the class C2 is abstract it can contain abstract methods
Ans: 3


42)
interface I{
    void f1();              // 1
    public void f2();       // 2
    protected void f3();    // 3
    private void f4();      // 4
    abstract void f5();     // 5
}


line  1,2,3,4

line  3,4

line  3

line  2,3,4

line  3,4,5

Explanation:
all methods declared within an interface are implicitly public, a weaker access level can not be declared
Ans: 2

43)
abstract class vehicle{
    abstract public void speed();
}
class car extends vehicle{
public static void main (String args[])
    {
    vehicle ob1;       
    ob1=new car();   //1
}
}


compiletime error at line  1

forces the class car to be declared as abstract

Runtime Exception

None of the above

Explanation:
No Explanation Available
Ans: 2


44)
class command {
    public static void main (String[] a1)
    {
    System.out.println(a1.length());        
    System.out.println(a1[0]);              
    System.out.println(a1);                 
      }
}

compile time error at line1

compile time error at line2

compile time error at line3

Runtime exception

Explanation:
length is not a method. it's a variable
Ans: 1

45)
abstract class A {}   // 1
transient class B {}  // 2
private class C {}    // 3
static class D {}     // 4
Which of these declarations will not produce a compile-time error?


Ans: 1

Ans: 2

Ans: 3

Ans: 4

None of the above

Explanation:
The modifiers, private and static, can be applied to a nested class, but can not be applied to a class that is not nested transient is allowed only for variables.
Ans: 1

46)
class c1
{
    public void m1(Object o1)
    {
      System.out.println("object");
    }
public void m1(String o1)
{
        System.out.println("string");
}
public int m1(int c)
{
        return c;
}
public static void main(String a[])
{
       c1 ob1=new c1();
      ob1.m1("hai");
           
}
}

print object

prints string

compile time error

non of the above

Explanation:
No Explanation Available
Ans: 2


47)
public static double sin(double angle)
What are the units of the "angle" argument?

Degrees

Radians

Both

None of the above

Explanation:
No Explanation Available
Ans: 2


48)
class base
{
    base()
    {
     System.out.println("base");
    }
    base(int i1)
    {
    }
}
class Super extends base
{
 Super()
 {
     System.out.println("super");
      super(1);
   
  }
public static void main(String [] a)
{
      base b1=new  Super();
}
}

compile time error

prints base and super

prints super and base

none of the above

Explanation:
No Explanation Available
Ans: 1


49)
The Throwable class is the superclass of all exceptions in the Java language.


true

false

Ans: 1


50)
class C {
    private String get ( String str )
    {
              try
              {
                  throw new Exception ( ) ;
                  return str ;
              }
        catch ( Exception e )
              {
                  return null ;
              }
  }
  public static void main ( String peace [ ] ) {
          try
          {
                  System.out.println ( ( new C ( ) ).get ( " C " ) ) ;
          }
          catch ( Exception e )
          {
                  System.out.println( "Exception" ) ;
          }
  }
};


compile time error

prints Exception

the code compiles and runs fine

none of the above

Ans: 1

Sunday, February 5, 2012

SCJP model question answer page 4


31)
class c2{
            final int i1;
            c2()
            {
                        i1=i1+1;
            }
            {
                        i1=2;
            }
            public static void main(String a[])
            {
                        c2 ob1=new c2();
                        System.out.println(ob1.i1);
            }
}


compile time error

prints 3

prints 2

none of the above

Ans: 1


32)
class C{
             public static void main(String a[])    
             {
              int i1=9;
              int i2;
              if(i1>3)
             {        
                    i2=8;
             }
               System.out.println(i2);
            }
}


compile time error

Runtim error

prints 8

prints 0

None of the above

Explanation:
since variable i1 is not final the value is not known at compiletime itself.so generate compile time error
Ans: 1


33)
class A extends Thread {
            public void run()
            {
                        System.out.print("A");
            }
}
class B {
public static void main (String[] args)
            {
            A a = new A();
            a.start();
            a.start();  // 1
            }
}


compile time error

Runtime Exception

the code compile and runs fine

none of the above

Explanation:
If the start method is invoked on a thread that is already running, then an IllegalThreadStateException will probably be thrown
Ans: 2


34)
class c1
{
            static{
                        System.out.println("static");
                    }
public static void main(String a[])
{
            System.out.println("main");
}
}


prints static main

prints main

prints main static

compiletime error

none of the above

Explanation:
No Explanation Available
Ans: 1


35)
class A{
            static String m(float i) {return "float";}
            static String m(double i) {return "double";}
            public static void main (String[] args) {
            int a1 = 1; long b1 = 2;
            System.out.print(m(a1)+","+ m(b1));
            }
}


prints float,foat

prints float,double

prints double,double

compile time error

None of the above

Explanation:
No Explanation Available
Ans: 1

36)
When a byte is added to a char, what is the type of the result?

byte

int

long

non of the above

Explanation:
The result of all arithmetic performed with the binary operators (not the assignment operators) is an int, a long, a float, or a double. Here byte and char are promoted to int, so the result is an int.
Ans: 2


37)
 class C extends Thread{
            public static void main(String argv[]){
            C b = new C();
            b.run();
  }
  public void start(){
          for (int i = 0; i <10; i++){
                  System.out.println("Value of i = " + i);
          }
  }

}



compile time error

runtime Exception

prints values from 0 to 9

None of the above

Explanation:
No Explanation Available
Ans: 4


38)
What is the signature of the run() method of the Runnable interface?

void run()

public void run(Runnable target)

public void run()

public static void run()

Ans: 3

39)
class C{
public static void main(String args[])
            {
            int a = 1;
            a += ++a + a++;
            System.out.print(a);
            }
}


compile time error

Runtime Exception

Prints 5

Prints 4

None of the above

Ans: 3

40)
interface I {           //1
  public class Inner {   ///2
         
          Inner ( ) {
                  System .out . println ( "Inner Created" ) ;
          }
  };
};


compile time error at line 1

compile time error at line 2

the code compiles fine

none of the above

Ans: 3

SCJP model question answer page 3


21)
class C{
            public static void main (String[] args)
            {
            byte b1=33;              //1
            b1++;                    //2
            byte b2=55;              //3
            b2=b1+1;                 //4
            System.out.println(b1+""+b2);
            }
}

compile time error at line   2

compile time error at line   4

prints 34,56

runtime exception

none of the above

Explanation:
b1+1 returns an integer value which can not be assigned to a byte variable
Ans: 2


22)
import java.util.*;
class  C
{
  final Vector v;
  C()
  {
          v=new Vector();
  }
   C(int i)
  {
      
  }
  public void someMethod()
  {
          System.out.println(v.isEmpty());
  }
}


compile time error

runtime exception

the code compiles and runs fine

none of the above

Explanation:
No Explanation Available
Ans: 1

23)
class C1{
            public void m1()
            { 
            }
}
class C2 extends C1
{
            private void  m1()
            {  
            }
}

compile time error at  line1

compile time error at  line2

Runtime exception

None of the above

Explanation:
tending to assign weaker access not allowed
Ans: 2


24)
interface I{
int i;                            // line 1
}
class C implements I{
public static void main(String a[]){
   System.out.println(i);
   System.out.println(i++);  //line 2
}
}


compile time error at line 1,2

compile time error at line 2

Runtime exception

Noneofthe above

Explanation:
interface constants are final so,they must be initialized when declaring it and they can not be altered
Ans: 1


25)
An abstract class must have at least one abstract method

true

true

Explanation:
An abstract without abstract methods is allowed
Ans: 2

26)

<![CDATA[
class C {
public static void main(String[] args) {
  boolean b1;
  b1=3<4<5;                //1
  System.out.println(b1);  //2
}}
]]>


compile time error at line 1

compile time error at line 2

Runtime exception

None of the above

Explanation:
<![CDATA[
3<4<5 evaulates to true<5 -->it's a wrong expression so it results in compiletime error
]]>
Ans: 1


27)
class C{
public static void main(String[] args) {
  try
    {
        int i1=3/0;
      }
    catch(Exception e)
    {
     System.out.println("exception1");
     }
    catch(NullPointerException e)
    {
    System.out.println("exception2");
        }
     finally
    {
              System.out.println("finally");

      }
}}

compile time error

runtime exception

prints exception1 and finally

prints exception1,exception2 and finally

None of the above

Explanation:
No Explanation Available
Ans: 1


28)
class C {
public static void main(String[] args) {
   char  c1=65;
  switch(c1){
       case 'A':                             
               System.out.println("one");
        default:                             
               System.out.println("two");
      case 'b':                              
               System.out.println("three");
}}}


prints one twot hree

prints two three

compile time error

Runtime exception

None of the above

Explanation:
char is a legal value for switch clause
Ans: 1


29)
class c1{
               void go(){}
            }
class c2 extends c1
{
             String go()
            {
              return null;
            }
}


compile time error

runtime exceptione

the code compiles and runs fine

None of the above

Explanation:
No Explanation Available
Ans: 1


30)
class base
{
            base(int c)
            {
             System.out.println("base");
            }
}
class Super extends base
{
             Super()
            {
                System.out.println("super");
            }
            public static void main(String [] a)
            {
                  base b1=new  Super();
            }
}

compile time error

runtime exceptione

the code compiles and runs fine

None of the above

Explanation:
No Explanation Available
Ans: 1