Sunday, February 5, 2012

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

No comments:

Post a Comment