Java MCQ Unit 1
Java MCQ Unit 1
3. With x = 0, which of the following are legal lines of Java code for changing the value of x to 1?
1. x++;
2. x = x + 1;
3. x += 1;
4. x =+ 1;
(c) 1, 2, 3 & 4
}
}
(c) 1.5 1
Answer:
Option (c)
7. What will be the output of the following Java program?
class Modulus
{
public static void main(String args[])
{
double a = 25.64;
int b = 25;
a = a % 10;
b = b % 10;
System.out.println(a + " " + b);
}
}
(a) 5.640000000000001 5
Answer:
Option (a)
8. What will be the output of the following Java program?
class increment
{
public static void main(String args[])
{
int g = 3;
System.out.print(++g * 8);
}
}
(c) 32
Answer:
Option (c)
9. Can 8 byte long data type be automatically type cast to 4 byte float data type?
(a) True
Answer:
Option (a)
10. What will be the output of the following Java program?
class Output
{
public static void main(String args[])
{
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
b++;
++a;
System.out.println(a + " " + b + " " + c);
}
}
(d) 3 4 4
Answer:
Option (d)
Option (d)
19.What will be the output of the following Java program?
class rightshift_operator
{
public static void main(String args[])
{
int x;
x = 10;
x = x >> 1;
System.out.println(x);
}
}
(b) 5
Answer:
Option (b)
20.What will be the output of the following Java program?
class Output
{
public static void main(String args[])
{
int a = 1;
int b = 2;
int c = 3;
a |= 4;
b >>= 1;
c <<= 1;
a ^= c;
System.out.println(a + " " + b + " " + c);
}
}
(a) 3 1 6
Answer:
Option (a)
Answer:
Option (c)
29.What will be the output of the following Java code?
class Output
{
public static void main(String args[])
{
int x , y = 1;
x = 10;
if (x != 10 && x / 0 == 0)
System.out.println(y);
else
System.out.println(++y);
}
}
(b) 2
Answer:
Option (b)
31.Which of these have highest precedence?
(a) ()
32.What should be expression1 evaluate to in using ternary operator as in this line?
expression1 ? expression2 : expression3
(c) Boolean
33.What is the value stored in x in the following lines of Java code?
int x, y, z;
x = 0;
y = 1;
x = y = z = 8;
(d) 8
34. What is the order of precedence (highest to lowest) of following operators?
1. &
2. ^
3. ?:
(a) 1 -> 2 -> 3
35.Which of these statements are incorrect?
(c) Division operator has higher precedence than multiplication operator
36.What will be the output of the following Java code?
class operators
{
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
int var3;
var3 = ++ var2 * var1 / var2 + var2;
System.out.print(var3);
}
}
(c) 12
37. What will be the output of the following Java code?
class operators
{
public static void main(String args[])
{
int x = 8;
System.out.println(++x * 3 + " " + x);
}
}
(d) 27 9
38. What will be the output of the following Java code?
class Output
{
public static void main(String args[])
{
int x=y=z=20;
}
}
d) compile time error
39. What will be the output of the following Java program?
class Output
{
public static void main(String args[])
{
int a,b,c,d;
a=b=c=d=20
a+=b-=c*=d/=20
System.out.println(a+" "+b+" "+c+" "+d);
}
}
(c) a=20 b=0 c=20 d=1
40. Which of the following is not OOPS concept in Java?
(d) Compilation
43.Which concept of Java is achieved by combining methods and attribute into a class?
(a) Encapsulation
44.Which component is used to compile, debug and execute java program?
(b) JDK
45.Which component is responsible for converting bytecode into machine specific code?
(a) JVM
46.Which component is responsible to run java program?
(d)JRE
47.Which component is responsible to optimize bytecode to machine code?
(c) JIT
48.Which statement is true about java?
(a) Platform independent programming language
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
(d) all of the mentioned
58.What is the numerical range of a char data type in Java?
(d) 0 to 65535
59.Which of these coding types is used for data type characters in Java?
(c) UNICODE
60.Which of these values can a boolean variable contain?
(a) True & False
61.Which one is a valid declaration of a boolean?
(c) boolean b3 = false;
62.What will be the output of the following Java program?
class mainclass {
public static void main(String args[])
{
char a = 'A';
a++;
System.out.print((int)a);
}
}
(a) 66
63.What will be the output of the following Java code?
class asciicodes {
public static void main(String args[])
{
char var1 = 'A';
char var2 = 'a';
System.out.println((int)var1 + " " + (int)var2);
}
(b) 65 97
64.Which of these is long data type literal?
(a) 0x99fffL
65.Which of these can be returned by the operator &?
(d) Integer or Boolean
66.Which of these can not be used for a variable name in Java?
(b) keyword
67.What will be the output of the following Java program?
class variable_scope
{
public static void main(String args[])
{
int x;
x = 5;
{
int y = 6;
System.out.print(x + " " + y);
}
System.out.println(x + " " + y);
}
}
(d) Compilation error
68.Which of these is an incorrect string literal?
(d) "Hello world"
69.Which of these is necessary condition for automatic type conversion in Java?
(b) The destination type is larger than source type
70.What will be the error in the following Java code?
byte b = 50;
b = b * 50;
(b) * operator has converted b * 50 into int, which can not be converted to byte without casting
71.If an expression contains double, int, float, long, then the whole expression will be promoted into wh
(c) double
72.What will be the output of the following Java code?
class char_increment
{
public static void main(String args[])
{
char c1 = 'D';
char c2 = 84;
c2++;
c1++;
System.out.println(c1 + " " + c2);
}
}
(a) EU
73.Java is a ........... language.
(b) strongly typed
74.How many primitive data types are there in Java?
(c) 8
75.Size of int in Java is
(b) 32 bit
76.Which one of these lists contains only Java programming language keywords?
(b) goto, instanceof, native, finally, default, throws
77.Which is a reserved word in the Java programming language?
(b) native
78.Which is a valid keyword in java?
(a) interface
79.Which is a valid declaration of a String?
(a) String s1 = null;
80.What will be the output of the program?
class Equals
{
public static void main(String [] args)
{
int x = 100;
double y = 100.1;
boolean b = (x = y);
System.out.println(b);
}
}
(c) Compilation fails
81.What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x=20;
String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
System.out.println(sup);
}}
(b) tiny
82.What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) || (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
(b) 8 2
83.What will be the output of the program?
class Bitwise
{
public static void main(String [] args)
{
int x = 11 & 9;
int y = x ^ 3;
System.out.println( y | 12 );
}
}
(d) 14
85.What would be the output of the following fraction of code ?
int Integer = 34 ;
char String = 'S' ;
System.out.print( Integer ) ;
System.out.print( String ) ;
(d) 34 S
86.What will be output of the following program code?
public class Test{
public static void main(String[] a){
short x = 10;
x = x*5;
System.out.print(x);
}
}
(c) Compilation Error
}
(c) -127
{
System.out.println("block");
}
public A(){
System.out.println("A");
}
if(1 + 1 + 1 + 1 + 1 == 5){
System.out.print("TRUE");
}
else{
System.out.print("FLASE");
}
(a) TRUE
94.What will be output?
public class Test
{
public static void main(String args[])
{
System.out.print(""=="");
System.out.print(" ");
System.out.print("A"=="A");
System.out.print(" ");
System.out.print("a==A");
}
}
(c) true true a==A
95.Determine output:
public class Test{
static int i = 5;
public static void main(String... args){
System.out.println(i++);
System.out.println(i);
System.out.println(++i);
System.out.println(++i+i++);
}
}
(c) 5 6 7 16