0% found this document useful (0 votes)
320 views16 pages

Java MCQ Unit 1

The document contains 40 multiple choice questions about Java operators and concepts. The questions cover topics like arithmetic, relational, logical, bitwise and assignment operators. They also cover operator precedence and output of code snippets using different operators. Some questions test knowledge of OOPs concepts like polymorphism and how real world objects can be represented as classes in Java.

Uploaded by

Janvi Jani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
320 views16 pages

Java MCQ Unit 1

The document contains 40 multiple choice questions about Java operators and concepts. The questions cover topics like arithmetic, relational, logical, bitwise and assignment operators. They also cover operator precedence and output of code snippets using different operators. Some questions test knowledge of OOPs concepts like polymorphism and how real world objects can be represented as classes in Java.

Uploaded by

Janvi Jani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

1. Which of the following can be operands of arithmetic operators?

(d) Both Numeric & Characters

2. Modulus operator, %, can be applied to which of these?


(c) Both Integers and floating – point numbers

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

4. Decrement operator, --, decreases the value of a variable by what number?


(a) 1

5. Which of these statements are incorrect?


(d) None of the mentioned
Answer:
Option (d)
6. What will be the output of the following Java program?
class increment
{
public static void main(String args[])
{
double var1 = 1 + 5;
double var2 = var1 / 4;
int var3 = 1 + 5;
int var4 = var3 / 4;
System.out.print(var2 + " " + var4);

}
}
(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)

11.Which of these is not a bitwise operator?


(d) <=
Answer:
Option (d)
12.Which operator is used to invert all the digits in a binary representation of a number?
(a) ~
Answer:
Option (a)
13.On applying Left shift operator, <<, on integer bits are lost one they are shifted past which position bit?
(d) 31
Answer:
Option (d)
14.Which right shift operator preserves the sign of the value?
(d) >>=
Answer:
Option (b)

15.Which of these statements are incorrect?


(d) The right shift operator automatically fills the higher order bits with 0
Answer:
Option (d)
16.What will be the output of the following Java program?
class bitwise_operator
{
public static void main(String args[])
{
int var1 = 42;
int var2 = ~var1;
System.out.print(var1 + " " + var2);
}
}
(c) 42 -43
Answer:
Option (c)
17.What will be the output of the following Java program?
class bitwise_operator
{
public static void main(String args[])
{
int a = 3;
int b = 6;
int c = a | b;
int d = a & b;
System.out.println(c + " " + d);
}
}
(a) 7 2
Answer:
Option (a)
18.What will be the output of the following Java program?
class leftshift_operator
{
public static void main(String args[])
{
byte x = 64;
int i;
byte y;
i = x << 2;
y = (byte) (x << 2)
System.out.print(i + " " + y);
}
}
(d) 256 0

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)

21.What is the output of relational operators?


(b) Boolean
Answer:
Option (b)
22.Which of these is returned by “greater than”, “less than” and “equal to” operators?
(c) Boolean
Answer:
Option (c)

23. Which of the following operators can operate on a boolean variable?


1. &&
2. ==
3. ?:
4. +=
(d) 1, 2 & 3
Answer:
Option (d)

24.Which of these operators can skip evaluating right hand operand?


(d) &&
Answer:
Option (d)
25.Which of these statements is correct?
(d) true and false are non numeric values
Answer:
Option (d)
26.What will be the output of the following Java code?
class Relational_operator
{
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
System.out.print(var1 > var2);
}
}
(d) False
Answer:
Option (d)

27.What will be the output of the following Java code?


class bool_operator
{
public static void main(String args[])
{
boolean a = true;
boolean b = !true;
boolean c = a | b;
boolean d = a & b;
boolean e = d ? b : c;
System.out.println(d + " " + e);
}
}
(d) false true
Answer:
Option (d)
28.What will be the output of the following Java code?
class ternary_operator
{
public static void main(String args[])
{
int x = 3;
int y = ~ x;
int z;
z = x > y ? x : y;
System.out.print(z);
}
(c) 3

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

41.Which of the following is a type of polymorphism in Java?


(a) Compile time polymorphism
42.Which concept of Java is a way of converting real world objects in terms of class?
(c)abstarction

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

49.Which of the below is invalid identifier with the main method?


(c) private
50.What is the extension of java code files?
(b) .java
51.What is the extension of compiled java classes?
(a) .class
52.What is the range of short data type in Java?
(b) -32768 to 32767
53.What is the range of byte data type in Java?
(a) -128 to 127
54.Which of the following are legal lines of Java code?
1. int w = (int)888.8;
2. byte x = (byte)100L;
3. long y = (byte)100;
4. byte z = (byte)100L;
(d) All statements are correct
55.An expression involving byte, int, and literal numbers is promoted to which of these?
(a) int
56.Which of these literals can be contained in float data type variable?
(b) -3.4e+038
57.What will be the output of the following Java statement?
class output {
public static void main(String args[])
{
double a, b,c;
a = 3.0/0;
b = 0/4.0;
c=0/0.0;

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

87. What will the output of the following program?


public class Test{
public static void main(String args[]){
float f = (1 / 4) * 10;
int i = Math.round(f);
System.out.println(i);
}
}
(b) 0
88. What will be output of following program?
public class Test{
public static void main(String[] args){
byte b=127;
b++;
b++;
System.out.println(b);
}

}
(c) -127

89.What is the output for the below code ?


public class A{
static{
System.out.println("static");
}

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

public A(){
System.out.println("A");
}

public static void main(String[] args){


A a = new A();
}
}
(b) static block A
90.int x = 0, y = 0 , z = 0 ;
x = (++x + y-- ) * z++;
What will be the value of "x" after execution ?
(c) 0

84.Size of float and double in Java is


(a) 32 and 64
91.What is the output of the following program ?
class Numbers{
public static void main(String args[]){
int a=20, b=10;
if((a < b) && (b++ < 25)){
System.out.println("This is any language logic");
}
System.out.println(b);
}
}
(c) 10
92.Select from among the following character escape code which is not available in Java.
(c) \a
93.What will be the output?

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

You might also like