java questions for beginners

Simple java objective type questions and answers for beginners

(1)

public class Loop {
    public static void main(String[] args){
         int i=1;
         int j=5;

         for(;;){
             System.out.println(j>>>i);
         }
    }
}

What will be output of the above java program?

(a)2
   1
(b)2
   1
   0
(c)Infinite loop
(d)Compiler error






Answer: (c)

(2)

public class ClassDemo {
    int i;
    double d;
    char c;

    public static void main(String[] args){
         ClassDemo o=new ClassDemo();
         System.out.println(o.i+","+o.d+","+(int)o.c);
    }
}

What will be output of the above java program?

(a)null,null,null
(b)0,0.0,null
(c)0,0.0,0
(d)Compiler error






Answer: (c)

(3)What will be output of the following java program?

class StringLiteral{
    public static void main(String[] args){ 
         String str="india\12usa";
         System.out.println(str);
    }
}

(a)India\12usa
(b)India usa
(c)India
   usa
(d)Compiler error






Answer: (c)

(4)

public class UnaryOperator {
    public static void main(String[] args) {
         int j=5,k;
         k=++j + j++ + ++j;
         System.out.print(k+" "+j);
    }
}

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

(a)19 8
(b)19 7
(c)20 8
(d)20 7






Answer: (c)

(5)

class StaticDemo {
    static int a=0;
    int b=5;
    public static void main(String[] args){
         StaticDemo t1,t2,t3;
         t1=new StaticDemo();
         t2=new StaticDemo();
         t3=new StaticDemo();
         t1.increment();
         t2.increment();
         t3.increment();
    }

    void increment(){
         System.out.print(a+++b++);
    }
}

What will be output of above program?

(a)777
(b)789
(c)567
(d)Compiler error





Answer: (c)

(6)

public class Loop {
    public static void main(String[] args){
         Double d=5.0;
         for(int i=2;i>=-1;){
             System.out.println(d/i);
             --i;
         }
    }
}

What will be output of above program?

(a)2.5
   5.0
(b)2.5
   5.0
   Infinity
   -5.0
(c)Run time exception
(d)Compiler error






Answer: (b)

(7)

class One extends Try{
    One(){
         System.out.println("One");
    }
}

class Two extends One{
    Two(){
         System.out.println("Two");
    }
}

class Try {
    public static void main(String[] args){
         if(new One() instanceof Try){
             System.out.println("True");
         }
         else{
             System.out.println("False");
         }
    }

    Try(){
         System.out.println("Try");
    }
}

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

         
         
         
         
         
Output:
Try
One
True

(8)What will be output of following program?

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

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

(a)india\rome
(b)indiarome
(c)india
   ome
(d)Compiler error






Answer: (c)

(9)

public class UnaryOperator {
    public static void main(String[] args) {
         int j=10;
         double k=j + ~5+ 0x20L;
         System.out.print(k);
    }
}

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

(a)24.0
(b)36.0
(c)24.000000
(d)Compiler error






Answer: (b)

(10)

class StaticDemo {
    static int a=0,b=5;
    public static void main(String[] args){
         increment();
    }

    static void increment(){
         System.out.print(+a+++b++);
    }
}

What will be output of above program?

(a)5
(b)6
(c)7
(d)Compiler error






Answer: (a)

(11)

public class Loop {
    public static void main(String[] args){
         Double d=1.5D;
         for(int i=2;i>=-1;){
             System.out.println(d/i);
             --i;
             --d;
         }
    }
}

What will be output of above program?

(a) 0.75
    0.5
    -Infinity
    1.5
(b) 0.75
    0.5
(c) 0.75
    0.5
    NaN
    1.5
(d)Compiler error






Answer: (a)

(12)What will be output of following program?

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

(a)130
(b) 131
(c)-126
(d)Compiler error





Answer: (d)

Explanation:

    Range of byte data in java is -128 to 127.

Here we are assigning value which is beyond the range of byte data type.

(13)What will be output of following program?

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

(a)japan\taskand
(b)japantaskand
(c)japam askand
(d)Compiler error

Answer: (c)

(14)

Why java uses 2’s complement for storing –ve number?

         
         
         
         
         
Answer:
In 1’s complement method there is different notation for positive and negative zero. Mathematically it is not true. But in 2’s complement methods positive zero and negative zero is same. Hence java uses 2’s complement method to sore negative numbers.

(15)

class StaticDemo {
    static int a=0,b=5;
    public static void main(String[] args){
         ++a;b--;
         increment();
    }

    static void increment(){
         StaticDemo t=new StaticDemo();
         System.out.print(++a^b--);
         t.display();
    }

    void display(){
         System.out.print(a-~b);
    }
}

What will be output of the above java program?

(a)1 6
(b)6 6
(c)5 6
(d)Compiler error






No comments: