java questions for viva

Java questions and answers for viva 


(1)

public class Literal {
    public static void main(String[] args) {
         String str$="world";
         char str_='X';
         String $_=str$+str_;
         System.out.print($_);
            
    }
}

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

(a)world
(b)worldX
(c)world’X’
(d)Compiler error





Answer: (b)

Explanation:

    In java programming language operator + can add two numeric data type as well as it can concatenate two string or character data type.
Here $_=”world”+’X’ ,Operator + is working as concatenating one string and char data type.
Note: $_ is valid variable name. 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. 
Integer Literals

(2)

public class Literal {
    public static void main(String[] args) {
         int a=012;
         int c=a*2;
         System.out.print(c);
            
    }
}

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

(a) 24
(b) 024
(c) 20
(d) Compiler error

Answer: (c)

(3)

public class Literal {
    public static void main(String[] args) {
         byte a=0110;
         int c=a*2;
         System.out.print(c);
            
    }
}

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

(a)1100
(b)220
(c)144
(d) Java doesn’t support binary number.Compiler error

Answer: (c)

(4)

public class Literal {
    public static void main(String[] args) {
         byte a=08;
         int c=a>>2;
         System.out.print(c);
            
    }
}

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

(a)32
(b)2
(c)02
(d) Compiler error

Answer: (d)

(5)

public class Literal {
    public static void main(String[] args) {
         byte a=0x11;
         byte b=0X22;
         int c=a&&b;
         System.out.print(c);
            
    }
}

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

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





Answer:  (d)

(6) Numerical value of false in java is :

(a)  0
(b) All values except one
(c) zero and all negative integers
(d) java doesn’t support any numerical value of false





Answer: (d)

(7)

public class Literal {
    public static void main(String[] args) {
         boolean b =5==5;
         int a=(int)b;
         System.out.println(a);
    }
}

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

(a)0
(b)1
(c)NaN
(d) Compiler error





Answer:(d)

(8) Which of the following is not valid java floating point literal?

(a) 4.5f
(b) .033D
(c) 5.0e^2
(d) 6.8





Answer: (c)

(9) Which of the following is a not valid java floating point literal?

(e) 8.09F
(f) .033f
(g) 5.0E25
(h) 6.8e^2





Answer: (h)

(10)

public class Scope {
    public static void main(String[] args) {
         int a=5;
         {
             a=10;
             int b=15;
             a=a+b;
         }
         System.out.println(a);
    }
}

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

(a)5
(b)25
(c)Run time exception
(d) Compiler error





Answer: (b)

(11)

public class Scope {
    public static void main(String[] args) {
         {
             static int a=5;
         }
         {
             int a=10;
             a=++a;
         }
         System.out.println(a);
    }
}

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

(a)5
(b)21
(c)11
(d) Compiler error





Answer: (d)

(12)

public class Scope {
    public static void main(String[] args) {
         {
             int a=5;
             System.out.println(a);
         }
         int a=6;
         a=~a;
         System.out.println(a);
    }
}

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

(a)5
  -7
(b)5
  -6
(c)Run time exception
(d) Compiler error




No comments: