java scjp questions and answers

Objective type questions for preparation of scjp exam and answers in java

(1)

public class Loop {
    public static void main(String[] args){
       
    for(int i=0;false;i++){
             System.out.println("java");
         }
    }
}

What will be output of above program?

(a)java
(b) Null
(c)It will not print anything.
(d)Compiler error






Answer: (d)

(2)What will be output of following program?

public class Datatype {
    public static void main(String[] args) {
         byte b=127;
         b++;
         b++;
         System.out.println(b);
    }
}

(a)129
(b)2
(c)-127
(d)Compiler error






Answer: (c)

Explanation:

    Range of byte data in java is -128 to 127. But byte data type in java is cyclic in nature.

(3)What will be output of following program?

class StringLiteral{
    public static void main(String[] args){ 
         String str="local\national";
         System.out.print(str);
    }
}

(a)local\national
(b)local ational
(c)local
   ational
(d)Compiler error






Answer: (c)

(4)

public class TypeConversion {
    public static void main(String[] args) {
         double d=2D+2d+2.+2l+2L+2f+2F+2.f+2.D;
         System.out.println(d);
    }
}

(a)18.0
(b)9.0
(c)Run time exception
(d) Compiler error






Answer: (a)

(5)

public class UnaryOperator {
    public static void main(String[] args) {
         byte a=-5;
         int b=-5;
         int c=~a+(byte)~b;
         System.out.print(c);
    }
}

What will output when you compile and run the above code?

(a)0
(b)8
(c)Run time exception
(d)Compiler error






Answer: (b)

(6)

public class BreakDemo {
    public static void main(String[] args){
         int j=~-3;

         while(j<7){
             System.out.print(j);
            
             if(j==3){
                 j+=2;
                 continue;
             }

             j++;
         }
    }
}

What will be output of above program?

(a)2367
(b)23
(c)2356
(d)Compiler error






Answer: (c)

(7)What will be output of following program?

public class Datatype {
    public static void main(String[] args) {
         byte b=125;
         System.out.println(b*2);
    }
}

(a)250
(b)-6
(c)Runtime exception
(d)Compiler error

Explanation:



    
    
Answer: (a)

(8)

public class Identifier {
    public static void main(String[] args) {
         double strictfp=5.02;
         strictfp+=.333;
         System.out.print(strictfp);
    }
}

(a) 5.353
(b) 5.353D
(c) 5.353d
(d) Compiler error






Answer: (d)

    In java programming language variable name includes alphabet, digits few special characters like underscore (_), dollar singe ($) but it should not be any reserved word of java language. Here strictfp is keyword of java cannot be name of variable

(9)

public class TypeConversion {
    public static void main(String[] args) {
         float f1=11;
         float f2=11.f;
         f2=f1+f2;
         System.out.println(f2);
    }
}

What will output when you compile and run the above code?

(a)22
(b)22.0
(c)22.0f
(d) Compiler error





Answer: (b)

(10)

public class Operator {
    public static void main(String[] args) {
         byte a=5;
         int b=10;
         int c=a>>2+b>>2;
         System.out.print(c);
    }
}

What will output when you compile and run the above code?

(a)60
(b)3
(c)0
(d)Compiler error






Answer: (c)

(11)

class Mango{
    final int a=5;
}     

class Fruit extends Mango {
    final int a=10;
}

class DynamicDispatch  extends Fruit{
    final int a=20;
    public static void main(String[] args){
         Mango m=new DynamicDispatch();
         Fruit f=new DynamicDispatch();
         System.out.print(m.a|f.a);
    }
}

What will be output of above program?

(a)15
(b)20
(c)30
(d)Compiler error






Answer: (a)

(12)

class Mango{
    final int a=5;
}

class Fruit extends Mango {
    final int a=10;
}

class DynamicDispatch  extends Fruit{
    final int a=15;
   
    public static void main(String[] args){
         DynamicDispatch t=new Mango();
         System.out.print(t.a);
    }
}

What will be output of above program?

(a)5
(b)10
(c)15
(d)Compiler error






Answer: (d)

(13)

Which is not true java statement?

(a)Java deallocates memory automatically.
(b) Finalize method is just called before garbage collection.
(c)Garbage collection runs when there is reference with object and runs periodically.
(d)Inside finalize method we keep those code which must be executed before object is destroyed by garbage collection.






Answer: (c)

(14)

public class This {
     int i;
    
    This(){
         this.i++;
         i++;
    }
   
    public static void main(String[] args){
         This o=new This();
         System.out.print(o.i);
    }
}

What will be output of above program?

(a)Garbage value
(b)1
(c)2
(d)Non static variable must be initialized, Compiler error




Answer :(c)

(15)

class Try {

    void display() {
         this.show();
    }

    void show(){
         System.out.print("javadoubt.blogspot.com");
    }
}

public class This {
    public static void main(String[] args){
         Try t=new Try();
         t.display();
    }
}

What will be output of above program?

(a) javadoubt.blogspot.com
(b) Output will be nothing
(c)null
(d)Compiler error





Answer: (a)

(16)

public class This {
    int a=10;
    This(){
         int a=5;
         this.a--;
    }

    public static void main(String[] args) {
         This o=new This();
         System.out.println(o.a);
    }
}

What will be output of above program?

(a)4
(b)5
(c)9
(d) Duplicate local variable, Compiler error





Answer: (c)

(17)

public class TypeConversion {
    public static void main(String[] args) {

         {
             final int a='\15';
             System.out.println(a);
         }
         int a=25;byte b=0100;
         a=a+b;
         System.out.println(a);
    }
}

What will output when you compile and run the above code?

(a)15
   125
(b)13
   89
(c)Run time exception
(d) Compiler error

(18)

public class TypeConversion {
    public static void main(String[] args) {
         float f=12e-1F;
         final long l=12L;
         f=f+l;
         System.out.println(f);
    }
}

What will output when you compile and run the above code?

(a)18.9
(b)13.2
(c)Run time exception
(d) Compiler error





Answer: (b)

(19)

public class TypeConversion {
    public static void main(String[] args) {
         char a=5;
         short b=-++a;
         System.out.println(b);
    }
}

What will output when you compile and run the above code?

(a)-4
(b)-5
(c)-6
(d) Compiler error






Answer: (d)

(20)

public class TypeConversion {
    public static void main(String[] args) {
         boolean b=11>=11;
         String str=(String)b;
         System.out.println(str);
    }
}

What will output when you compile and run the above code?

(a)true
(b)false
(c)”true”
(d) Compiler error





Answer: (d)

(21)

public class TypeConversion {
    public static void main(String[] args) {
         byte b=016;
         short s=0x16;
         char c='\16';
         int a=b+s+c;
         System.out.println(a);
    }
}

What will output when you compile and run the above code?

(a)48
(b)50
(c)Run time exception
(d) Compiler error






Answer: (b)

No comments:

Post a Comment