0% found this document useful (0 votes)
693 views

Oops MCQ Answers

The document contains 40 multiple choice questions about Java operators and precedence. It tests knowledge of arithmetic, assignment, relational, logical, bitwise and ternary operators. For each question, the possible answers are provided along with the correct answer. The questions cover topics like operator precedence, arithmetic operations, logical and bitwise operations, relational operators and conditional/ternary operators. Sample code snippets are provided to demonstrate the expected output.
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)
693 views

Oops MCQ Answers

The document contains 40 multiple choice questions about Java operators and precedence. It tests knowledge of arithmetic, assignment, relational, logical, bitwise and ternary operators. For each question, the possible answers are provided along with the correct answer. The questions cover topics like operator precedence, arithmetic operations, logical and bitwise operations, relational operators and conditional/ternary operators. Sample code snippets are provided to demonstrate the expected output.
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/ 131

JAYA COLLEGE OF ENGG & TECHNOLOGY

DEPARTMENT COMPUTER SCIENCE AND ENGINEERING

MCQ_Answers OOPS/CS8392 DEP : CSE/EEE STAFF NAME : T.VIJAYARASU

Introduction to java
MCQ No - 1

Which of the following can be operands of arithmetic operators?


(A) Numeric
(B) Boolean
(C) Characters
(D) Both Numeric & Characters

D
MCQ No - 2

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


(A) Integers
(B) Floating – point numbers
(C) Both Integers and floating – point numbers
(D) None of the mentioned

C
MCQ No - 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;

(A) 1, 2 & 3
(B) 1 & 4
(C) 1, 2, 3 & 4
(D) 3 & 2

C
JAYA COLLEGE OF ENGG & TECHNOLOGY
DEPARTMENT COMPUTER SCIENCE AND ENGINEERING
MCQ No - 4

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


(A) 1
(B) 2
(C) 3
(D) 4

A
MCQ No - 5

Which of these statements are incorrect?


(A) Assignment operators are more efficiently implemented by Java run-time system than
their equivalent long forms
(B) Assignment operators run faster than their equivalent long forms
(C) Assignment operators can be used only with numeric and character data type
(D) None of the mentioned

MCQ No - 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);

}
}
(A) 1 1
(B) 0 1
(C) 1.5 1
(D) 1.5 1.0

C
MCQ No - 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
(B) 5.640000000000001 5.0
(C) 5 5
(D) 5 5.640000000000001

A
MCQ No - 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);
}
}

(A) 25
(B) 24
(C) 32
(D) 33

C
MCQ No - 9

Can 8 byte long data type be automatically type cast to 4 byte float data type?
(A) True
(B) False

A
MCQ No - 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);
}
}

(A) 3 2 4
(B) 3 2 3
(C) 2 3 4
(D) 3 4 4

MCQ No - 11
Which of these is not a bitwise operator?
(A) &
(B) &=
(C) |=
(D) <=

D
MCQ No - 12

Which operator is used to invert all the digits in a binary representation of a number?
(A) ~
(B) <<<
(C) >>>
(D) ^

A
MCQ No - 13

On applying Left shift operator, <<, on integer bits are lost one they are shifted past which
position bit?
(A) 1
(B) 32
(C) 33
(D) 31

D
MCQ No - 14

Which right shift operator preserves the sign of the value?


(A) <<
(B) >>
(C) <<=
(D) >>=

B
MCQ No - 15

Which of these statements are incorrect?


(A) The left shift operator, <<, shifts all of the bits in a value to the left specified number of
times
(B) The right shift operator, >>, shifts all of the bits in a value to the right specified number
of times
(C) The left shift operator can be used as an alternative to multiplying by 2
(D) The right shift operator automatically fills the higher order bits with 0

MCQ No - 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);
}
}

(A) 42 42
(B) 43 43
(C) 42 -43
(D) 42 43

C
MCQ No - 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
(B) 7 7
(C) 7 5
(D) 5 2

A
MCQ No - 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);
}
}

(A) 0 64
(B) 64 0
(C) 0 256
(D) 256 0

D
MCQ No - 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);
}
}

(A) 10
(B) 5
(C) 2
(D) 20

B
MCQ No - 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
(B) 2 2 3
(C) 2 3 4
(D) 3 3 6

MCQ No - 21

What is the output of relational operators?


(A) Integer
(B) Boolean
(C) Characters
(D) Double

B
MCQ No - 22

Which of these is returned by “greater than”, “less than” and “equal to” operators?
(A) Integers
(B) Floating – point numbers
(C) Boolean
(D) None of the mentioned

C
MCQ No - 23

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


1. &&
2. ==
3. ?:
4. +=

(A) 3 & 2
(B) 1 & 4
(C) 1, 2 & 4
(D) 1, 2 & 3

D
MCQ No - 24

Which of these operators can skip evaluating right hand operand?


(A) !
(B) |
(C) &
(D) &&

D
MCQ No - 25
Which of these statements is correct?
(A) true and false are numeric values 1 and 0
(B) true and false are numeric values 0 and 1
(C) true is any non zero value and false is 0
(D) true and false are non numeric values

MCQ No - 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);
}
}

(A) 1
(B) 0
(C) True
(D) False

D
MCQ No - 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);
}
}

(A) false false


(B) true true
(C) true false
(D) false true

D
MCQ No - 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);
}
}

(A) 0
(B) 1
(C) 3
(D) -4

C
MCQ No - 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);
}
}

(A) 1
(B) 2
(C) Runtime error owing to division by zero in if condition
(D) Unpredictable behavior of program

B
MCQ No - 30

What will be the output of the following Java code?


class Output
{
public static void main(String args[])
{
boolean a = true;
boolean b = false;
boolean c = a ^ b;
System.out.println(!c);
}
}

(A) 0
(B) 1
(C) false
(D) true

MCQ No - 31

Which of these have highest precedence?


(A) ()
(B) ++
(C) *
(D) >>

A
MCQ No - 32

What should be expression1 evaluate to in using ternary operator as in this line?


expression1 ? expression2 : expression3

(A) Integer
(B) Floating – point numbers
(C) Boolean
(D) None of the mentioned

C
MCQ No - 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;

(A) 0
(B) 1
(C) 9
(D) 8

D
MCQ No - 34

What is the order of precedence (highest to lowest) of following operators?


1. &
2. ^
3. ?:

(A) 1 -> 2 -> 3


(B) 2 -> 1 -> 3
(C) 3 -> 2 -> 1
(D) 2 -> 3 -> 1
A
MCQ No - 35

Which of these statements are incorrect?


(A) Equal to operator has least precedence
(B) Brackets () have highest precedence
(C) Division operator has higher precedence than multiplication operator
(D) Addition operator and subtraction operator have equal precedence

MCQ No - 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);
}
}

(A) 10
(B) 11
(C) 12
(D) 56

C
MCQ No - 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);
}
}

(A) 24 8
(B) 24 9
(C) 27 8
(D) 27 9

D
MCQ No - 38

What will be the output of the following Java code?


class Output
{
public static void main(String args[])
{
int x=y=z=20;

}
}

(A) compile and runs fine


(B) 20
(C) run time error
(D) compile time error

D
MCQ No - 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);
}
}

(A) compile time error


(B) runtime error
(C) a=20 b=0 c=20 d=1
(D) none of the mentioned

C
MCQ No - 40

Which of the following is not OOPS concept in Java?


(A) Inheritance
(B) Encapsulation
(C) Polymorphism
(D) Compilation

MCQ No - 41

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


(A) Compile time polymorphism
(B) Execution time polymorphism
(C) Multiple polymorphism
(D) Multilevel polymorphism

A
MCQ No - 42

Which concept of Java is a way of converting real world objects in terms of class?
(A) Polymorphism
(B) Encapsulation
(C) Abstraction
(D) Inheritance

C
MCQ No - 43
Which concept of Java is achieved by combining methods and attribute into a class?
(A) Encapsulation
(B) Inheritance
(C) Polymorphism
(D) Abstraction

A
MCQ No - 44

Which component is used to compile, debug and execute java program?


(A) JVM
(B) JDK
(C) JIT
(D) JRE

B
MCQ No - 45

Which component is responsible for converting bytecode into machine specific code?
(A) JVM
(B) JDK
(C) JIT
(D) JRE

MCQ No - 46

Which component is responsible to run java program?


(A) JVM
(B) JDK
(C) JIT
(D) JRE

D
MCQ No - 47

Which component is responsible to optimize bytecode to machine code?


(A) JVM
(B) JDK
(C) JIT
(D) JRE

C
MCQ No - 48

Which statement is true about java?


(A) Platform independent programming language
(B) Platform dependent programming language
(C) Code dependent programming language
(D) Sequence dependent programming language

A
MCQ No - 49

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


(A) public
(B) static
(C) private
(D) final

C
MCQ No - 50

What is the extension of java code files?


(A) .class
(B) .java
(C) .txt
(D) .js

MCQ No - 51

What is the extension of compiled java classes?


(A) .class
(B) .java
(C) .txt
(D) .js

A
MCQ No - 52

What is the range of short data type in Java?


(A) -128 to 127
(B) -32768 to 32767
(C) -2147483648 to 2147483647
(D) None of the mentioned

B
MCQ No - 53

What is the range of byte data type in Java?


(A) -128 to 127
(B) -32768 to 32767
(C) -2147483648 to 2147483647
(D) None of the mentioned

A
MCQ No - 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;

(A) 1 and 2
(B) 2 and 3
(C) 3 and 4
(D) All statements are correct

D
MCQ No - 55

An expression involving byte, int, and literal numbers is promoted to which of these?
(A) int
(B) long
(C) byte
(D) float

MCQ No - 56

Which of these literals can be contained in float data type variable?


(A) -1.7e+308
(B) -3.4e+038
(C) +1.7e+308
(D) -3.4e+050

B
MCQ No - 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);
}
}

(A) Infinity
(B) 0.0
(C) NaN
(D) all of the mentioned
D
MCQ No - 58

What is the numerical range of a char data type in Java?


(A) -128 to 127
(B) 0 to 256
(C) 0 to 32767
(D) 0 to 65535

D
MCQ No - 59

Which of these coding types is used for data type characters in Java?
(A) ASCII
(B) ISO-LATIN-1
(C) UNICODE
(D) None of the mentioned

C
MCQ No - 60

Which of these values can a boolean variable contain?


(A) True & False
(B) 0 & 1
(C) Any integer value
(D) true

MCQ No - 61

Which one is a valid declaration of a boolean?


(A) boolean b1 = 1;
(B) boolean b2 = ‘false’;
(C) boolean b3 = false;
(D) boolean b4 = ‘true’

C
MCQ No - 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
(B) 67
(C) 65
(D) 64

A
MCQ No - 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);
}
}

(A) 162
(B) 65 97
(C) 67 95
(D) 66 98

B
MCQ No - 64

Which of these is long data type literal?


(A) 0x99fffL
(B) ABCDEFG
(C) 0x99fffa
(D) 99671246

A
MCQ No - 65

Which of these can be returned by the operator &?


(A) Integer
(B) Boolean
(C) Character
(D) Integer or Boolean

MCQ No - 66

Which of these can not be used for a variable name in Java?


(A) identifier
(B) keyword
(C) identifier & keyword
(D) none of the mentioned

B
MCQ No - 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);
}
}
(A) 5 6 5 6
(B) 5 6 5
(C) Runtime error
(D) Compilation error

D
MCQ No - 68

Which of these is an incorrect string literal?


(A) “Hello World”
(B) “Hello\nWorld”
(C) “\”Hello World\””
(D) "Hello world"

D
MCQ No - 69

Which of these is necessary condition for automatic type conversion in Java?


(A) The destination type is smaller than source type
(B) The destination type is larger than source type
(C) The destination type can be larger or smaller than source type
(D) None of the mentioned

B
MCQ No - 70

What will be the error in the following Java code?


byte b = 50;
b = b * 50;

(A) b cannot contain value 100, limited by its range


(B) * operator has converted b * 50 into int, which can not be converted to byte without
casting
(C) b cannot contain value 50
(D) No error in this code

MCQ No - 71
If an expression contains double, int, float, long, then the whole expression will be promoted
into which of these data types?
(A) long
(B) int
(C) double
(D) float

C
MCQ No - 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) E U
(B) U E
(C) V E
(D) U F

A
MCQ No - 73

Java is a ........... language.


(A) weakly typed
(B) strongly typed
(C) moderate typed
(D) None of these

B
MCQ No - 74
How many primitive data types are there in Java?
(A) 6
(B) 7
(C) 8
(D) 9

C
MCQ No - 75

Size of int in Java is


(A) 16 bit
(B) 32 bit
(C) 64 bit
(D) Depends on execution environment

MCQ No - 76

Which one of these lists contains only Java programming language keywords?
(A) class, if, void, long, Int, continue
(B) goto, instanceof, native, finally, default, throws
(C) try, virtual, throw, final, volatile, transient
(D) strictfp, constant, super, implements, do

B
MCQ No - 77

Which is a reserved word in the Java programming language?


(A) method
(B) native
(C) subclasses
(D) reference

B
MCQ No - 78

Which is a valid keyword in java?


(A) interface
(B) string
(C) Float
(D) unsigned

A
MCQ No - 79

Which is a valid declaration of a String?


(A) String s1 = null;
(B) String s2 = 'null';
(C) String s3 = (String) 'abc';
(D) String s4 = (String) '\ufeed';

A
MCQ No - 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);
}
}

(A) true
(B) false
(C) Compilation fails
(D) An exception is thrown at runtime

MCQ No - 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);
}
}

(A) small
(B) tiny
(C) huge
(D) Compilation fails

B
MCQ No - 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);
}
}

(A) 5 3
(B) 8 2
(C) 8 3
(D) 8 5

B
MCQ No - 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 );
}
}

(A) 0
(B) 7
(C) 8
(D) 14

D
MCQ No - 84

Size of float and double in Java is


(A) 32 and 64
(B) 64 and 64
(C) 32 and 32
(D) 64 and 32

A
MCQ No - 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 ) ;
(A) Does not compile as Integer and String are API class names.
(B) S
(C) 34
(D) 34 S

MCQ No - 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);
}
}

(A) 50
(B) 10
(C) Compilation Error
(D) None of these

C
MCQ No - 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);
}
}

(A) 2
(B) 0
(C) 3
(D) 2.5
B
MCQ No - 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);
}
}

(A) 2
(B) 129
(C) -127
(D) Compiler error

C
MCQ No - 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();
}
}
(A) A block static
(B) static block A
(C) static A
(D) A

B
MCQ No - 90
int x = 0, y = 0 , z = 0 ;
x = (++x + y-- ) * z++;

What will be the value of "x" after execution ?


(A) -2
(B) -1
(C) 0
(D) 1

MCQ No - 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);
}
}

(A) 12
(B) 11
(C) 10
(D) 23

C
MCQ No - 92
Select from among the following character escape code which is not available in Java.
(A) \t
(B) \r
(C) \a
(D) \\

C
MCQ No - 93

What will be the output?


if(1 + 1 + 1 + 1 + 1 == 5){
System.out.print("TRUE");
}
else{
System.out.print("FLASE");
}

(A) TRUE
(B) FALSE
(C) Compiler Error
(D) None of these

A
MCQ No - 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");
}
}

(A) "==" A"=="A a==A


(B) true true false
(C) true true a==A
(D) Compilation Fails

C
MCQ No - 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++);
}
}

(A) 6 6 6 16
(B) 6 7 6 16
(C) 5 6 7 16
(D) 5 6 6 16

MCQs of Selections , Mathematical functions and loops

MCQ No - 1

Which of the following is used with the switch statement?


(A) Continue
(B) Exit
(C) break
(D) do

C
MCQ No - 2
What is the valid data type for variable “a” to print “Hello World”?
switch(a)
{
System.out.println("Hello World");
}

(A) int and float


(B) byte and short
(C) char and long
(D) byte and char

D
MCQ No - 3

Which of the following is not a decision making statement?


(A) if
(B) if-else
(C) switch
(D) do-while

D
MCQ No - 4

Which of the following is not a valid jump statement?


(A) break
(B) goto
(C) continue
(D) return

B
MCQ No - 5

From where break statement causes an exit?


(A) Only from innermost loop
(B) Terminates a program
(C) Only from innermost switch
(D) From innermost loops or switches

D
MCQ No - 6

Which of the following is not a valid flow control statement?


(A) exit()
(B) break
(C) continue
(D) return

A
MCQ No - 7
switch(x)
{
default:
System.out.println("Hello");
}

Which two are acceptable types for x?


1. byte
2. long
3. char
4. float
5. Short
6. Long

(A) 1 and 3
(B) 2 and 4
(C) 3 and 5
(D) 4 and 6

A
MCQ No - 8
public void test(int x)
{
int odd = 1;
if(odd)
{
System.out.println("odd");
}
else
{
System.out.println("even");
}
}

Which statement is true?


(A) Compilation fails.
(B) "odd" will always be output.
(C) "even" will always be output.
(D) "odd" will be output for odd values of x, and "even" for even values.

A
MCQ No - 9
public class While
{
public void loop()
{
int x= 0;
while ( 1 ) /* Line 6 */
{
System.out.print("x plus one is " + (x + 1)); /* Line 8 */
}
}
}

Which statement is true?


(A) There is a syntax error on line 1.
(B) There are syntax errors on lines 1 and 6.
(C) There are syntax errors on lines 1, 6, and 8.
(D) There is a syntax error on line 6.

D
MCQ No - 10

Determine output:
public class Test{
public static void main(String args[]){
int i;

for (i=1;i<6;i++){
if(i>3) contine;
}
System.out.println(i);
}
}

(A) 2
(B) 3
(C) 4
(D) 6

MCQ No - 11

In java, ............ can only test for equality, where as .......... can evaluate any type of the
Boolean expression.
(A) switch, if
(B) if, switch
(C) if, break
(D) continue, if

A
MCQ No - 12

Which of the following for loops will be an infinite loop?


(A) for (;;)
(B) for (i=0;i<1;i--)
(C) for(i=0;;i++)
(D) All of the above

D
MCQ No - 13
Determine output:
public class Test{
public static void main(String args[]){
int i;
for(i = 1; i < 6; i++){
if(i > 3) continue ;
}
System.out.println(i);
}
}

(A) 2
(B) 3
(C) 4
(D) 6

D
MCQ No - 14

Consider the following program written in Java.


class Test{
public static void main(String args[]){
int x=7;
if(x==2);
System.out.println("NumberSeven");
System.out.println("NotSeven");
}
}

What would the output of the program be?


(A) NumberSeven NotSeven
(B) NumberSeven
(C) NotSeven
(D) Error

A
MCQ No - 15
Determine output:
public class Test{
public static void main(String args[]){
int i, j;
for(i=1, j=0;i<10;i++) j += i;
System.out.println(i);
}
}

(A) 10
(B) 11
(C) 9
(D) 20

MCQ No - 16

What will be the value of y after execution of switch statement?


public class Test{
public static void main(String[] args){
int x = 3, y = 4;
switch(x + 3){
case 6: y = 0;
case 7: y = 1;
default: y += 1;
}
}
}

(A) 1
(B) 2
(C) 3
(D) 4

B
MCQ No - 17
What is the printout of the following switch statement?
char ch = 'a';
switch (ch){
case 'a':
case 'A': System.out.print(ch); break;
case 'b':
case 'B': System.out.print(ch); break;
case 'c':
case 'C': System.out.print(ch); break;
case 'd':
case 'D': System.out.print(ch);
}

(A) abcd
(B) aa
(C) a
(D) ab

C
MCQ No - 18

How many times will the following code print "Welcome to DIET"?
int count = 0;
do {
System.out.println("Welcome to DIET");
count++;
} while (count < 10);

(A) 8
(B) 9
(C) 10
(D) 11

C
MCQ No - 19

What will be the result of the following code?


public class Test{
static public void main(String args[]){ //line 2
int i, j;
for(i=0; i<3; i++){
for(j=1; j<4; j++){
i%=j;
System.out.println(j);
}
}
}
}

(A) 1 2 3 1
(B) 1 2 3 2
(C) Repeatedly print 1 2 3 and cause infinite loop.
(D) Compilation fails because of line 2

C
MCQ No - 20

What all gets printed when the following program is compiled and run.
public class Test{
public static void main(String args[]){
int i, j=1;
i = (j>1)?2:1;
switch(i){
case 0: System.out.println(0); break;
case 1: System.out.println(1);
case 2: System.out.println(2); break;
case 3: System.out.println(3); break;
}
}
}

(A) 0
(B) 1
(C) 2
(D) 1 2

MCQ No - 21
What will be the output?
public class Test{
public static void main(String args[]){
int i = 1;
do{
i--;
}while(i > 2);
System.out.println(i);
}
}

(A) 1
(B) 2
(C) -1
(D) 0

D
MCQ No - 22

What will be the result?


int i = 10;
while(i++ <= 10){
i++;
}
System.out.print(i);

(A) 10
(B) 11
(C) 12
(D) 13

D
MCQ No - 23

What will be the result of compiling and runnig the following code:
public class Test{
public static void main(String... args) throws Exception{
Integer i = 34;
int l = 34;
if(i.equals(l)){
System.out.println("true");
}else{
System.out.println("false");
}
}
}

(A) true
(B) false
(C) Compiler error
(D) None of these

A
MCQ No - 24
1. public class Test{
2. public static void main(String [] args){
3. int x = 0;
4. // insert code here
5. do{ } while(x++ < y);
6. System.out.println(x);
7. }
8. }

Which option, inserted at line 4, produces the output 12?


(A) int y = x;
(B) int y = 10;
(C) int y = 11;
(D) int y = 12;

C
MCQ No - 25

What will be the result?


1. int i = 10;
2. while(i++ <= 10){
3. i++;
4. }
5. System.out.print(i);
(A) Line 5 will be never reached.
(B) 11
(C) 12
(D) 13

MCQ No - 26

What is true about do statement?


(A) do statement executes the code of a loop at least once
(B) do statement does not get execute if condition is not matched in the first iteration
(C) do statement checks the condition at the beginning of the loop
(D) do statement executes the code more than once always

A
MCQ No - 27

What is true about a break?


(A) Break stops the execution of entire program
(B) Break halts the execution and forces the control out of the loop
(C) Break forces the control out of the loop and starts the execution of next iteration
(D) Break halts the execution of the loop for certain time frame

B
MCQ No - 28

What would be the output of the following code snippet if variable a=10?
if(a<=0)
{
if(a==0)
{
System.out.println("1 ");
}
else
{
System.out.println("2 ");
}
}
System.out.println("3 ");

(A) 1 2
(B) 2 3
(C) 1 3
(D) 3

D
MCQ No - 29

What will be the output of the following Java program?


class Output
{
public static void main(String args[])
{
int a = 5;
int b = 10;
first:
{
second:
{
third:
{
if (a == b >> 1)
break second;
}
System.out.println(a);
}
System.out.println(b);
}
}
}

(A) 5 10
(B) 10 5
(C) 5
(D) 10

D
MCQ No - 30

What will be the output of the following Java program?


class Output
{
public static void main(String args[])
{
final int a=10,b=20;
while (a

(A) Hello
(B) run time error
(C) Hello world
(D) compile time error

MCQ No - 31

What will be the output of the following Java program?


class jump_statments
{
public static void main(String args[])
{
int x = 2;
int y = 0;
for ( ; y < 10; ++y)
{
if (y % x == 0)
continue;
else if (y == 8)
break;
else
System.out.print(y + " ");
}
}
}
(A) 1 3 5 7
(B) 2 4 6 8
(C) 1 3 5 7 9
(D) 1 2 3 4 5 6 7 8 9

C
MCQ No - 32

What will be the output of the following Java program?


class comma_operator
{
public static void main(String args[])
{
int sum = 0;
for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
sum += i;
System.out.println(sum);
}
}

(A) 5
(B) 6
(C) 14
(D) compilation error

B
MCQ No - 33

What will be the output of the following Java program?


class selection_statements
{
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
System.out.print(var2);
else
System.out.print(++var2);
}
}

(A) 1
(B) 2
(C) 3
(D) 4

B
MCQ No - 34

Which of this statement is incorrect?


(A) switch statement is more efficient than a set of nested ifs
(B) two case constants in the same switch can have identical values
(C) switch statement can only test for equality, whereas if statement can evaluate any type of
boolean expression
(D) it is possible to create a nested switch statements

B
MCQ No - 35

Which of these jump statements can skip processing the remainder of the code in its body for
a particular iteration?
(A) break
(B) return
(C) exit
(D) continue

MCQ No - 36

Which of the following loops will execute the body of loop even when condition controlling
the loop is initially false?
(A) do-while
(B) while
(C) for
(D) none of the mentioned
A
MCQ No - 37

Which of these are selection statements in Java?


(A) if()
(B) for()
(C) continue
(D) break

A
MCQ No - 38

Which of these selection statements test only for equality?


(A) if
(B) switch
(C) if & switch
(D) none of the mentioned

MCQs of Methods and Arrays

MCQ No - 1

When does method overloading is determined?


(A) At run time
(B) At compile time
(C) At coding time
(D) At execution time

B
MCQ No - 2

When Overloading does not occur?


(A) More than one method with same name but different method signature and different
number or type of parameters
(B) More than one method with same name, same signature but different number of signature
(C) More than one method with same name, same signature, same number of parameters but
different type
(D) More than one method with same name, same number of parameters and type but
different signature

D
MCQ No - 3

What will be the output of the following Java code?


class average {
public static void main(String args[])
{
double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
double result;
result = 0;
for (int i = 0; i < 6; ++i)
result = result + num[i];
System.out.print(result/6);

}
}

(A) 16.34
(B) 16.566666644
(C) 16.46666666666667
(D) 16.46666666666666

C
MCQ No - 4

What will be the output of the following Java program?


class array_output {
public static void main(String args[])
{
char array_variable [] = new char[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = 'i';
System.out.print(array_variable[i] + "" );
i++;
}
}
}

(A) i i i i i
(B) 0 1 2 3 4
(C) i j k l m
(D) None of the mentioned

A
MCQ No - 5

What will be the output of the following Java program?


class evaluate
{
public static void main(String args[])
{
int a[] = {1,2,3,4,5};
int d[] = a;
int sum = 0;
for (int j = 0; j < 3; ++j)
sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]);
System.out.println(sum);
}
}

(A) 38
(B) 39
(C) 40
(D) 41

MCQ No - 6

What will be the output of the following Java program?


class array_output
{
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = i/2;
array_variable[i]++;
System.out.print(array_variable[i] + " ");
i++;
}

}
}

(A) 0 2 4 6 8
(B) 1 2 3 4 5
(C) 0 1 2 3 4 5 6 7 8 9
(D) 1 2 3 4 5 6 7 8 9 10

B
MCQ No - 7

What will be the output of the following Java program, if we run as “java main_arguments 1
2 3”?
class main_arguments
{
public static void main(String [] args)
{
String [][] argument = new String[2][2];
int x;
argument[0] = args;
x = argument[0].length;
for (int y = 0; y < x; y++)
System.out.print(" " + argument[0][y]);
}
}

(A) 1 1
(B) 1 0
(C) 1 0 3
(D) 1 2 3

D
MCQ No - 8
Which will legally declare, construct, and initialize an array?
(A) int [] myList = {"1", "2", "3"};
(B) int [] myList = (5, 8, 2);
(C) int myList [] [] = {4,9,7,0};
(D) int myList [] = {4, 3, 7};

D
MCQ No - 9

Which three are legal array declarations?


1. int [] myScores [];
2. char [] myChars;
3. int [6] myScores;
4. Dog myDogs [];
5. Dog myDogs [7];

(A) 1, 2, 4
(B) 2, 4, 5
(C) 2, 3, 4
(D) All are correct.

A
MCQ No - 10

Which one of the following will declare an array and initialize it with five numbers?
(A) Array a = new Array(5);
(B) int [] a = {23,22,21,20,19};
(C) int a [] = new int[5];
(D) int [5] array;

MCQ No - 11

Which cause a compiler error?


(A) int[ ] scores = {3, 5, 7};
(B) int [ ][ ] scores = {2,7,6}, {9,3,45};
(C) String cats[ ] = {"Fluffy", "Spot", "Zeus"};
(D) boolean results[ ] = new boolean [] {true, false, true};
B
MCQ No - 12

What is the widest valid returnType for methodA in line 3?


public class ReturnIt
{
returnType methodA(byte x, double y) /* Line 3 */
{
return (long)x / y * 2;
}
}

(A) int
(B) byte
(C) long
(D) double

D
MCQ No - 13

Which one creates an instance of an array?


(A) int[ ] ia = new int[15];
(B) float fa = new float[20];
(C) char[ ] ca = "Some String";
(D) int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 };

A
MCQ No - 14

Which two cause a compiler error?


1. float[ ] f = new float(3);
2. float f2[ ] = new float[ ];
3. float[ ]f1 = new float[3];
4. float f3[ ] = new float[3];
5. float f5[ ] = {1.0f, 2.0f, 2.0f};

(A) 2, 4
(B) 3, 5
(C) 4, 5
(D) 1, 2
D
MCQ No - 15

What will be the output of the program?


class Test
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}

void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}

boolean fix(boolean b1)


{
b1 = true;
return b1;
}
}

(A) true true


(B) false true
(C) true false
(D) false false

MCQ No - 16

Which of these operators is used to allocate memory to array variable in Java?


(A) malloc
(B) alloc
(C) new
(D) new malloc
C
MCQ No - 17

What will be the output of the following Java code?


int arr[] = new int [5];
System.out.print(arr);

(A) 0
(B) value stored in arr[0]
(C) 00000
(D) Class name@ hashcode in hexadecimal form

D
MCQ No - 18

Which of these is an incorrect Statement?


(A) It is necessary to use new operator to initialize an array
(B) Array can be initialized using comma separated expressions surrounded by curly braces
(C) Array can be initialized when they are declared
(D) None of the mentioned

A
MCQ No - 19

Which of these is necessary to specify at time of array initialization?


(A) Row
(B) Column
(C) Both Row and Column
(D) None of the mentioned

A
MCQ No - 20

What will be the output of the following Java code?


class array_output
{
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = i;
System.out.print(array_variable[i] + " ");
i++;
}
}
}

(A) 0 2 4 6 8
(B) 1 3 5 7 9
(C) 0 1 2 3 4 5 6 7 8 9
(D) 1 2 3 4 5 6 7 8 9 10

MCQ No - 21

What will be the output of the following Java code?


class multidimention_array
{
public static void main(String args[])
{
int arr[][] = new int[3][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
arr[i][j] = j + 1;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
sum + = arr[i][j];
System.out.print(sum);
}
}

(A) 11
(B) 10
(C) 13
(D) 14

B
MCQ No - 22

What will be the output of the following Java code?


class evaluate
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System.out.println(arr[n] / 2);
}
}

(A) 3
(B) 0
(C) 6
(D) 1

D
MCQ No - 23

What will be the output of the following Java code?


class array_output
{
public static void main(String args[])
{
char array_variable [] = new char[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = 'i';
System.out.print(array_variable[i] + "");
}
}
}
(A) 1 2 3 4 5 6 7 8 9 10
(B) 0 1 2 3 4 5 6 7 8 9 10
(C) i j k l m n o p q r
(D) i i i i i i i i i i

D
MCQ No - 24

What will be the output of the following Java code?


class array_output
{
public static void main(String args[])
{
int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3 ; ++j)
sum = sum + array_variable[i][j];
System.out.print(sum / 5);
}
}

(A) 8
(B) 9
(C) 10
(D) 11

B
MCQ No - 25

What is the type of variable ‘b’ and ‘d’ in the following Java snippet?
int a[], b;
int []c, d;

(A) ‘b’ and ‘d’ are int


(B) ‘b’ and ‘d’ are arrays of type int
(C) ‘b’ is int variable; ‘d’ is int array
(D) ‘d’ is int variable; ‘b’ is int array

C
MCQ No - 26

What will be the output of the following Java code snippet?


Object[] names = new String[3];
names[0] = new Integer(0);

(A) ArrayIndexOutOfBoundsException
(B) ArrayStoreException
(C) Compilation Error
(D) Code runs successfully

B
MCQ No - 27

Generics does not work with?


(A) Set
(B) List
(C) Tree
(D) Array

D
MCQ No - 28

How to sort an array?


(A) Array.sort()
(B) Arrays.sort()
(C) Collection.sort()
(D) System.sort()

B
MCQ No - 29

How to copy contents of array?


(A) System.arrayCopy()
(B) Array.copy()
(C) Arrays.copy()
(D) Collection.copy()
A
MCQ No - 30

Where is an array stored in memory?


(A) heap space
(B) stack space
(C) heap space and stack space
(D) first generation memory

MCQ No - 31

An array elements are always stored in ________ memory locations.


(A) Sequential
(B) Random
(C) Sequential and Random
(D) Binary search

A
MCQ No - 32

Which of these is an incorrect Statement?


(A) It is necessary to use new operator to initialize an array.
(B) Array can be initialized using comma separated expressions surrounded by curly braces.
(C) Array can be initialized when they are declared.
(D) None of the mentioned

A
MCQ No - 33

In Java arrays are


(A) objects
(B) object references
(C) primitive data type
(D) None of the above

A
MCQ No - 34
Which one of the following is a valid statement?
(A) char[] c = new char();
(B) char[] c = new char[5];
(C) char[] c = new char(4);
(D) char[] c = new char[];

B
MCQ No - 35

What is the result of compiling and running the following code?


public class Test{
public static void main(String[] args){
int[] a = new int[0];
System.out.print(a.length);
}
}

(A) 0
(B) Compilation error, arrays cannot be initialized to zero size.
(C) Compilation error, it is a.length() not a.length
(D) None of the above

MCQ No - 36

What will be the output?


public class Test{
public static void main(String[] args){
int[] x = new int[3];
System.out.println("x[0] is " + x[0]);
}
}

(A) The program has a compile error because the size of the array wasn't specified when
declaring the array.
(B) The program has a runtime error because the array elements are not initialized.
(C) The program runs fine and displays x[0] is 0.
(D) The program has a runtime error because the array element x[0] is not defined.
C
MCQ No - 37

What is the output of the following code?


public class Test{
public static void main(String args[]){
double[] myList = {1, 5, 5, 5, 5, 1};
double max = myList[0];
int indexOfMax = 0;
for(int i = 1; i < myList.length; i++){
if(myList[i] > max){
max = myList[i];
indexOfMax = i;
}
}
System.out.println(indexOfMax);
}
}

(A) 0
(B) 1
(C) 2
(D) 3

B
MCQ No - 38

Determine output:
public class Test{
public static void main(String[] args){
int[] x = {1, 2, 3, 4};
int[] y = x;

x = new int[2];

for(int i = 0; i < x.length; i++)


System.out.print(y[i] + " ");
}
}
(A) 1 2 3 4
(B) 0 0 0 0
(C) 1 2
(D) 0 0

C
MCQ No - 39

Analyze the following code and choose the correct answer.


int[] arr = new int[5];
arr = new int[6];

(A) The code has compile errors because the variable arr cannot be changed once it is
assigned.
(B) The code has runtime errors because the variable arr cannot be changed once it is
assigned.
(C) The code can compile and run fine. The second line assigns a new array to arr.
(D) The code has compile errors because we cannot assign a different size array to arr.

C
MCQ No - 40

What will be the output?


public class Test{
public static void main(String[] args){
int[] a = new int[4];
a[1] = 1;
a = new int[2];
System.out.println("a[1] is " + a[1]);
}
}

(A) The program has a compile error because new int[2]


(B) The program has a runtime error because a[1]
(C) a[1] is 0
(D) a[1] is 1

MCQ No - 41
When you pass an array to a method, the method receives ________ .
(A) A copy of the array.
(B) A copy of the first element.
(C) The reference of the array.
(D) The length of the array.

C
MCQ No - 42

What will be the output of the program?


public class Test{
public static void main(String [] args){
String s1 = args[1];
String s2 = args[2];
String s3 = args[3];
String s4 = args[4];
System.out.print(" args[2] = " + s2);
}
}

and the command-line invocation is C:Java> java Test 1 2 3 4


(A) args[2] = 2
(B) args[2] = 3
(C) args[2] = null
(D) An exception is thrown at runtime.

D
MCQ No - 43

What is the value of a[1] after the following code is executed?


int[] a = {0, 2, 4, 1, 3};
for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];

(A) 0
(B) 1
(C) 2
(D) 3
B
MCQ No - 44

Choose all the lines which if inserted independently instead of "//insert code here" will allow
the following code to compile:
public class Test{
public static void main(String args[]){
add();
add(1);
add(1, 2);
}

// insert code here


}

(A) void add(Integer... args){}


(B) static void add(int... args, int y){}
(C) static void add(int args...){}
(D) static void add(int... args){}

MCQs of Objects and Classes

MCQ No - 1

What is the stored in the object obj in following lines of Java code?
box obj;

(A) Memory address of allocated memory of object


(B) NULL
(C) Any arbitrary pointer
(D) Garbage

B
MCQ No - 2

Which of these keywords is used to make a class?


(A) class
(B) struct
(C) int
(D) none of the mentioned

A
MCQ No - 3

Which of the following is a valid declaration of an object of class Box?


(A) Box obj = new Box();
(B) Box obj = new Box;
(C) obj = new Box();
(D) new Box obj;

A
MCQ No - 4

Which of these operators is used to allocate memory for an object?


(A) malloc
(B) alloc
(C) new
(D) give

C
MCQ No - 5

Which keyword is used by the method to refer to the object that invoked it?
(A) import
(B) catch
(C) abstract
(D) this

MCQ No - 6

What is not the use of “this” keyword in Java?


(A) Passing itself to another method
(B) Calling another constructor in constructor chaining
(C) Referring to the instance variable when local variable has the same name
(D) Passing itself to method of the same class

D
MCQ No - 7

Arrays in Java are implemented as?


(A) class
(B) object
(C) variable
(D) none of the mentioned

B
MCQ No - 8

Which of these keywords is used to prevent content of a variable from being modified?
(A) final
(B) last
(C) constant
(D) static

A
MCQ No - 9

Which of the following statements are incorrect?


(A) Variables declared as final occupy memory
(B) final variable must be initialized at the time of declaration
(C) Arrays in java are implemented as an object
(D) All arrays contain an attribute-length which contains the number of elements stored in
the array

A
MCQ No - 10

What is the return type of Constructors?


(A) int
(B) float
(C) void
(D) none of the mentioned
D

MCQ No - 11

Which of the following is a method having same name as that of its class?
(A) finalize
(B) delete
(C) class
(D) constructor

D
MCQ No - 12

Which operator is used by Java run time implementations to free the memory of an object
when it is no longer needed?
(A) delete
(B) free
(C) new
(D) none of the mentioned

D
MCQ No - 13

Which function is used to perform some action when the object is to be destroyed?
(A) finalize()
(B) delete()
(C) main()
(D) none of the mentioned

A
MCQ No - 14

Which of the following statements are incorrect?


(A) default constructor is called at the time of object declaration
(B) constructor can be parameterized
(C) finalize() method is called when a object goes out of scope and is no longer needed
(D) finalize() method must be declared protected

C
MCQ No - 15

What is true about private constructor?


(A) Private constructor ensures only one instance of a class exist at any point of time
(B) Private constructor ensures multiple instances of a class exist at any point of time
(C) Private constructor eases the instantiation of a class
(D) Private constructor allows creating objects in other classes

MCQ No - 16

What is true about constructor?


(A) It can contain return type
(B) It can take any number of parameters
(C) It can have any non access modifiers
(D) Constructor cannot throw an exception

B
MCQ No - 17

Abstract class cannot have a constructor.


(A) TRUE
(B) FALSE

B
MCQ No - 18

What is true about protected constructor?


(A) Protected constructor can be called directly
(B) Protected constructor can only be called using super()
(C) Protected constructor can be used outside package
(D) Protected constructor can be instantiated even if child is in a different package

B
MCQ No - 19

What would be the behaviour if one parameterized constructor is explicitly defined?


(A) Compilation error
(B) Compilation succeeds
(C) Runtime error
(D) Compilation succeeds but at the time of creating object using default constructor, it
throws compilation error

D
MCQ No - 20

What would be behaviour if the constructor has a return type?


(A) Compilation error
(B) Runtime error
(C) Compilation and runs successfully
(D) Only String return type is allowed

MCQ No - 21

What is true about Class.getInstance()?


(A) Class.getInstance calls the constructor
(B) Class.getInstance is same as new operator
(C) Class.getInstance needs to have matching constructor
(D) Class.getInstance creates object if class does not have any constructor

D
MCQ No - 22

Which of these cannot be declared static?


(A) class
(B) object
(C) variable
(D) method

B
MCQ No - 23

Which of the following statements are incorrect?


(A) static methods can call other static methods only
(B) static methods must only access static data
(C) static methods can not refer to this or super in any way
(D) when object of class is declared, each object contains its own copy of static variables

D
MCQ No - 24

Which of these methods must be made static?


(A) main()
(B) delete()
(C) run()
(D) finalize()

A
MCQ No - 25

Determine output:
public class Test{
int i = 34;
public static void main(String args[]){
Test t1 = new Test();
Test t2 = new Test();
t1.i = 65;
System.out.print(t1.i);
System.out.print(t2.i);
}
}

(A) 34 34
(B) 65 34
(C) 65 65
(D) 34 65

MCQ No - 26
What is the output for the below code ?
class A{
int k;
boolean istrue;
static int p;
public void printValue(){
System.out.print(k);
System.out.print(istrue);
System.out.print(p);
}
}

public class Test{


public static void main(String argv[]){
A a = new A();
a.printValue();
}
}

(A) 0 false 0
(B) 0 true 0
(C) 0 0 0
(D) Compile error - static variable must be initialized before use.

A
MCQ No - 27

Determine output:
public class Test{
int a = 10;

public void method(int a){


a += 1;
System.out.println(++a);
}
public static void main(String args[]){
Test t = new Test();
t.method(3);
}
}
(A) 4
(B) 5
(C) 12
(D) 11

B
MCQ No - 28

What is the result of compiling and running the following code?


public class Tester{
static int x = 4;
int y = 9;
public Tester(){
System.out.print(this.x); // line 1
printVariables();
}
public static void printVariables(){
System.out.print(x); // line 2
System.out.print(y); // line 3
}
public static void main(String... args) { // line 4
new Tester();
}
}

(A) Compile error at line 1 (static x must be only accessed inside static methods)
(B) Compile error at line 3 (static methods can't make reference to non-static variables)
(C) Compile error at line 4 (invalid argument type for method main)
(D) 49

B
MCQ No - 29

What will be the output?


public class Test{
static{
int a = 5;
}

public static void main(String args[]){


new Test().call();
}

void call(){
this.a++;
System.out.print(this.a);
}
}

(A) Compile with error


(B) Runtime Exception
(C) 5
(D) 6

A
MCQ No - 30

What will be the output of the following Java program?


class main_class
{
public static void main(String args[])
{
int x = 9;
if (x == 9)
{
int x = 8;
System.out.println(x);
}
}
}

(A) 9
(B) 8
(C) Compilation error
(D) Runtime error

MCQ No - 31
What will be the output of the following Java program?
class box
{
int width;
int height;
int length;
}
class mainclass
{
public static void main(String args[])
{
box obj1 = new box();
box obj2 = new box();
obj1.height = 1;
obj1.length = 2;
obj1.width = 1;
obj2 = obj1;
System.out.println(obj2.height);
}
}

(A) 1
(B) 2
(C) Runtime error
(D) Garbage value

A
MCQ No - 32

What will be the output of the following Java program?


class box
{
int width;
int height;
int length;
}
class mainclass
{
public static void main(String args[])
{
box obj = new box();
System.out.println(obj);
}
}

(A) 0
(B) 1
(C) Runtime error
(D) classname@hashcode in hexadecimal form

D
MCQ No - 33

What will be the output of the following Java code?


class area
{
int width;
int length;
int area;
void area(int width, int length)
{
this.width = width;
this.length = length;
}

}
class Output
{
public static void main(String args[])
{
area obj = new area();
obj.area(5 , 6);
System.out.println(obj.length + "" "" + obj.width);
}
}

(A) 0 0
(B) 5 6
(C) 6 5
(D) 5 5

C
MCQ No - 34

What will be the output of the following Java program?


class access
{
public int x;
static int y;
void cal(int a, int b)
{
x += a ;
y += b;
}
}
class static_specifier
{
public static void main(String args[])
{
access obj1 = new access();
access obj2 = new access();
obj1.x = 0;
obj1.y = 0;
obj1.cal(1, 2);
obj2.x = 0;
obj2.cal(2, 3);
System.out.println(obj1.x + "" "" + obj2.y);
}
}

(A) 1 2
(B) 2 3
(C) 3 2
(D) 1 5

D
MCQ No - 35
What will be the output of the following Java program?
class access
{
static int x;
void increment()
{
x++;
}
}
class static_use
{
public static void main(String args[])
{
access obj1 = new access();
access obj2 = new access();
obj1.x = 0;
obj1.increment();
obj2.increment();
System.out.println(obj1.x + "" "" + obj2.x);
}
}

(A) 1 2
(B) 1 1
(C) 2 2
(D) Compilation Error

MCQ No - 36

What will be the output of the following Java program?


class static_out
{
static int x;
static int y;
void add(int a , int b)
{
x = a + b;
y = x + b;
}
}
class static_use
{
public static void main(String args[])
{
static_out obj1 = new static_out();
static_out obj2 = new static_out();
int a = 2;
obj1.add(a, a + 1);
obj2.add(5, a);
System.out.println(obj1.x + "" "" + obj2.y);
}
}

(A) 7 7
(B) 6 6
(C) 7 9
(D) 9 7

MCQs of Object oriented thinking

MCQ No - 1

Method overriding is combination of inheritance and polymorphism?


(A) TRUE
(B) FALSE

A
MCQ No - 2

Which of this keyword can be used in a subclass to call the constructor of superclass?
(A) super
(B) this
(C) extent
(D) extends

A
MCQ No - 3

What is the process of defining a method in a subclass having same name & type signature
as a method in its superclass?
(A) Method overloading
(B) Method overriding
(C) Method hiding
(D) None of the mentioned

B
MCQ No - 4

Which of these keywords can be used to prevent Method overriding?


(A) static
(B) constant
(C) protected
(D) final

D
MCQ No - 5

Which of these is correct way of calling a constructor having no parameters, of superclass A


by subclass B?
(A) super(void);
(B) superclass.();
(C) super.A();
(D) super();

MCQ No - 6

Which of these is supported by method overriding in Java?


(A) Abstraction
(B) Encapsulation
(C) Polymorphism
(D) None of the mentioned

C
MCQ No - 7

Which of this keyword must be used to inherit a class?


(A) super
(B) this
(C) extent
(D) extends

D
MCQ No - 8

Which of these is correct way of inheriting class A by class B?


(A) class B + class A {}
(B) class B inherits class A {}
(C) class B extends A {}
(D) class B extends class A {}

C
MCQ No - 9

What is not type of inheritance?


(A) Single inheritance
(B) Double inheritance
(C) Hierarchical inheritance
(D) Multiple inheritance

B
MCQ No - 10

Using which of the following, multiple inheritance in Java can be implemented?


(A) Interfaces
(B) Multithreading
(C) Protected methods
(D) Private methods

MCQ No - 11

All classes in Java are inherited from which class?


(A) java.lang.class
(B) java.class.inherited
(C) java.class.object
(D) java.lang.Object

D
MCQ No - 12

Static members are not inherited to subclass.


(A) TRUE
(B) FALSE

B
MCQ No - 13

Which of the following is used for implementing inheritance through class?


(A) inherited
(B) using
(C) extends
(D) implements

C
MCQ No - 14

Does Java support multiple level inheritance?


(A) TRUE
(B) FALSE

A
MCQ No - 15
You want subclasses in any package to have access to members of a superclass. Which is the
most restrictive access that accomplishes this objective?
(A) public
(B) private
(C) protected
(D) transient

MCQ No - 16

What is the most restrictive access modifier that will allow members of one class to have
access to members of another class in the same package?
(A) public
(B) abstract
(C) protected
(D) default access

D
MCQ No - 17

Given a method in a protected class, what access modifier do you use to restrict access to
that method to only the other members of the same class?
(A) final
(B) static
(C) private
(D) protected

C
MCQ No - 18

Which of these keywords are used to define an abstract class?


(A) abst
(B) abstract
(C) Abstract
(D) abstract class

B
MCQ No - 19
Which of these is not abstract?
(A) Thread
(B) AbstractList
(C) List
(D) None of the Mentioned

A
MCQ No - 20

If a class inheriting an abstract class does not define all of its function then it will be known
as?
(A) Abstract
(B) A simple class
(C) Static class
(D) None of the mentioned

MCQ No - 21

Which of these is not a correct statement?


(A) Every class containing abstract method must be declared abstract
(B) Abstract class defines only the structure of the class not its implementation
(C) Abstract class can be initiated by new operator
(D) Abstract class can be inherited

C
MCQ No - 22

Which of these packages contains abstract keyword?


(A) java.lang
(B) java.util
(C) java.io
(D) java.system

A
MCQ No - 23

In order to restrict a variable of a class from inheriting to subclass, how variable should be
declared?
(A) Protected
(B) Private
(C) Public
(D) Static

B
MCQ No - 24

If super class and subclass have same variable name, which keyword should be used to use
super class?
(A) super
(B) this
(C) upper
(D) classname

A
MCQ No - 25

Which of the following is used for implementing inheritance through an interface?


(A) inherited
(B) using
(C) extends
(D) implements

MCQ No - 26

Which of these class is used to create an object whose character sequence is mutable?
(A) String()
(B) StringBuffer()
(C) String() & StringBuffer()
(D) None of the mentioned

B
MCQ No - 27

Which of this method of class StringBuffer is used to concatenate the string representation
to the end of invoking string?
(A) concat()
(B) append()
(C) join()
(D) concatenate()

B
MCQ No - 28

Which of these method of class StringBuffer is used to find the length of current character
sequence?
(A) length()
(B) Length()
(C) capacity()
(D) Capacity()

A
MCQ No - 29

What is the string contained in s after following lines of Java code?


StringBuffer s new StringBuffer("Hello");
s.deleteCharAt(0);

(A) Hell
(B) ello
(C) Hel
(D) llo

B
MCQ No - 30

Which of the following statement is correct?


(A) reverse() method reverses all characters
(B) reverseall() method reverses all characters
(C) replace() method replaces first occurrence of a character in invoking string with another
character
(D) replace() method replaces last occurrence of a character in invoking string with another
character
A

MCQ No - 31

Which of the following are incorrect form of StringBuffer class constructor?


(A) StringBuffer()
(B) StringBuffer(int size)
(C) StringBuffer(String str)
(D) StringBuffer(int size , String str)

D
MCQ No - 32

Which of these class is superclass of String and StringBuffer class?


(A) java.util
(B) java.lang
(C) ArrayList
(D) None of the mentioned

B
MCQ No - 33

Which of these operators can be used to concatenate two or more String objects?
(A) +
(B) +=
(C) &
(D) ||

A
MCQ No - 34

Which of these method of class String is used to extract a single character from a String
object?
(A) CHARAT()
(B) chatat()
(C) charAt()
(D) ChatAt()

C
MCQ No - 35
Which of these constructors is used to create an empty String object?
(A) String()
(B) String(void)
(C) String(0)
(D) None of the mentioned

MCQ No - 36

Which of these is an incorrect statement?


(A) String objects are immutable, they cannot be changed
(B) String object can point to some other reference of String variable
(C) StringBuffer class is used to store string in a buffer for later use
(D) None of the mentioned

C
MCQ No - 37

Which of these method of class String is used to compare two String objects for their
equality?
(A) equals()
(B) Equals()
(C) isequal()
(D) Isequal()

A
MCQ No - 38

Which of these methods is used to compare a specific region inside a string with another
specific region in another string?
(A) regionMatch()
(B) match()
(C) RegionMatches()
(D) regionMatches()

D
MCQ No - 39
Which of these methods of class String is used to check whether a given object starts with a
particular string literal?
(A) startsWith()
(B) endsWith()
(C) Starts()
(D) ends()

A
MCQ No - 40

What is the value returned by function compareTo() if the invoking string is less than the
string compared?
(A) zero
(B) value less than zero
(C) value greater than zero
(D) none of the mentioned

MCQ No - 41

Which of these data type value is returned by equals() method of String class?
(A) char
(B) int
(C) boolean
(D) all of the mentioned

C
MCQ No - 42

Which of these method of class String is used to remove leading and trailing whitespaces?
(A) startsWith()
(B) trim()
(C) Trim()
(D) doTrim()

B
MCQ No - 43

What is the value returned by function compareTo() if the invoking string is greater than the
string compared?
(A) zero
(B) value less than zero
(C) value greater than zero
(D) none of the mentioned

C
MCQ No - 44

At line number 2 in the following code, choose 3 valid data-type attributes/qualifiers among
“final, static, native, public, private, abstract, protected”
1. public interface Status
2. {
3. /* insert qualifier here */ int MY_VALUE = 10;
4. }

(A) final, native, private


(B) final, static, protected
(C) final, private, abstract
(D) final, static, public

D
MCQ No - 45

What will be the output of the following Java program?


final class A
{
int i;
}
class B extends A
{
int j;
System.out.println(j + " " + i);
}
class inheritance
{
public static void main(String args[])
{
B obj = new B();
obj.display();
}
}

(A) 2 2
(B) 3 3
(C) Runtime Error
(D) Compilation Error

MCQ No - 46

What will be the output of the following Java program?


class A
{
int i;
public void display()
{
System.out.println(i);
}
}
class B extends A
{
int j;
public void display()
{
System.out.println(j);
}
}
class Dynamic_dispatch
{
public static void main(String args[])
{
B obj2 = new B();
obj2.i = 1;
obj2.j = 2;
A r;
r = obj2;
r.display();
}
}

(A) 1
(B) 2
(C) 3
(D) 4

B
MCQ No - 47

What will be the output of the following Java program?


class A
{
int i;
void display()
{
System.out.println(i);
}
}
class B extends A
{
int j;
void display()
{
System.out.println(j);
}
}
class inheritance_demo
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
(A) 0
(B) 1
(C) 2
(D) Compilation Error

C
MCQ No - 48

What will be the output of the following Java program?


class A
{
int i;
}
class B extends A
{
int j;
void display()
{
super.i = j + 1;
System.out.println(j + " " + i);
}
}
class inheritance
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}

(A) 2 2
(B) 3 3
(C) 2 3
(D) 3 2

C
MCQ No - 49
What will be the output of the following Java program?
class A
{
public int i;
public int j;
A()
{
i = 1;
j = 2;
}
}
class B extends A
{
int a;
B()
{
super();
}
}
class super_use
{
public static void main(String args[])
{
B obj = new B();
System.out.println(obj.i + " " + obj.j)
}
}

(A) 1 2
(B) 2 1
(C) Runtime Error
(D) Compilation Error

A
MCQ No - 50

What will be the output of the following Java code?


class A
{
public int i;
private int j;
}
class B extends A
{
void display()
{
super.j = super.i + 1;
System.out.println(super.i + " " + super.j);
}
}
class inheritance
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}

(A) 2 2
(B) 3 3
(C) Runtime Error
(D) Compilation Error

MCQ No - 51

What will be the output of the following Java program?


class output
{
public static void main(String args[])
{
String a = "hello i love java";
System.out.println(a.indexOf('e')+" "+a.indexOf('a')+" "+a.lastIndexOf('l')+"
"+a.lastIndexOf('v'));
}
}
(A) 6 4 6 9
(B) 5 4 5 9
(C) 7 8 8 9
(D) 1 14 8 15

D
MCQ No - 52

What will be the output of the following Java program?


class output
{
public static void main(String args[])
{
StringBuffer c = new StringBuffer("Hello");
StringBuffer c1 = new StringBuffer(" World");
c.append(c1);
System.out.println(c);
}
}

(A) Hello
(B) World
(C) Helloworld
(D) Hello World

D
MCQ No - 53

What will be the output of the following Java code?


class output
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Hello");
sb.replace(1,3,"Java");
System.out.println(sb);
}
}

(A) Hello java


(B) Hellojava
(C) HJavalo
(D) Hjava

C
MCQ No - 54

What will be the output of the following Java code?


class output
{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello");
s1.setCharAt(1,'x');
System.out.println(s1);
}
}

(A) xello
(B) xxxxx
(C) Hxllo
(D) Hexlo

C
MCQ No - 55

What will be the output of the following Java code?


class output
{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello World");
s1.insert(6 , "Good ");
System.out.println(s1);
}
}

(A) HelloGoodWorld
(B) HellGoodoWorld
(C) HellGood oWorld
(D) Hello Good World

MCQ No - 56

What will be the output of the following Java program?


class String_demo
{
public static void main(String args[])
{
char chars[] = {'a', 'b', 'c'};
String s = new String(chars);
System.out.println(s);
}
}

(A) a
(B) b
(C) c
(D) abc

D
MCQ No - 57

What will be the output of the following Java code?


class output
{
public static void main(String args[])
{
String c = "Hello i love java";
boolean var;
var = c.startsWith("hello");
System.out.println(var);
}
}

(A) TRUE
(B) FALSE
(C) 0
(D) 1

B
MCQ No - 58

What will be the output of the following Java code?


class output
{
public static void main(String args[])
{
String s1 = "Hello";
String s2 = new String(s1);
String s3 = "HELLO";
System.out.println(s1.equals(s2) + " " + s2.equals(s3));
}
}

(A) true true


(B) false false
(C) true false
(D) false true

C
MCQ No - 59

In the following Java code, which code fragment should be inserted at line 3 so that the
output will be: “123abc 123abc”?
1. StringBuilder sb1 = new StringBuilder("123");
2. String s1 = "123";
3. // insert code here
4. System.out.println(sb1 + " " + s1);

(A) sb1.append(“abc”); s1.append(“abc”);


(B) sb1.append(“abc”); s1.concat(“abc”);
(C) sb1.concat(“abc”); s1.append(“abc”);
(D) sb1.append(“abc”); s1 = s1.concat(“abc”);

D
MCQ No - 60
What will be the output of the following Java code?
class output
{
public static void main(String args[])
{
String chars[] = {"a", "b", "c", "a", "c"};
for (int i = 0; i < chars.length; ++i)
for (int j = i + 1; j < chars.length; ++j)
if(chars[i].compareTo(chars[j]) == 0)
System.out.print(chars[j]);
}
}

(A) ab
(B) bc
(C) ca
(D) ac

MCQ No - 61

What will be the output of the following Java program?


class output
{
public static void main(String args[])
{
String c = " Hello World ";
String s = c.trim();
System.out.println("\""+s+"\"");
}
}

(A) “”Hello World””


(B) “”Hello World”
(C) “Hello World”
(D) Hello world

C
MCQ No - 62
What will be the output of the following Java program?
class output
{
public static void main(String args[])
{
String s1 = "Hello";
String s2 = s1.replace('l','w');
System.out.println(s2);
}
}

(A) hello
(B) helwo
(C) hewlo
(D) hewwo

D
MCQ No - 63

What will be the output of the following Java program?


class output
{
public static void main(String args[])
{
String s1 = "Hello World";
String s2 = s1.substring(0 , 4);
System.out.println(s2);
}
}

(A) Hell
(B) Hello
(C) Worl
(D) World

A
MCQ No - 64

What will be the output of the following Java program?


class output
{
public static void main(String args[])
{ String s = "Hello World";
int i = s.indexOf('o');
int j = s.lastIndexOf('l');
System.out.print(i + " " + j);

}
}

(A) 4 8
(B) 5 9
(C) 4 9
(D) 5 8

MCQs of Exception Handling, I/O, abstract classes and interfaces


MCQ No - 1

When does Exceptions in Java arises in code sequence?


(A) Run Time
(B) Compilation Time
(C) Can Occur Any Time
(D) None of the mentioned

A
MCQ No - 2

Which of these keywords is not a part of exception handling?


(A) try
(B) finally
(C) thrown
(D) catch

C
MCQ No - 3

Which of these keywords must be used to monitor for exceptions?


(A) try
(B) finally
(C) throw
(D) catch

A
MCQ No - 4

Which of these keywords must be used to handle the exception thrown by try block in some
rational manner?
(A) try
(B) finally
(C) throw
(D) catch

D
MCQ No - 5

Which of these keywords is used to manually throw an exception?


(A) try
(B) finally
(C) throw
(D) catch

MCQ No - 6

What will be the output of the following Java program?


class exception_handling
{
public static void main(String args[])
{
try
{
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
}
}

(A) A
(B) B
(C) AC
(D) BC

D
MCQ No - 7

Which of the following classes can catch all exceptions which cannot be caught?
(A) RuntimeException
(B) Error
(C) Exception
(D) ParentException

B
MCQ No - 8

Which of the following is a super class of all exception type classes?


(A) Catchable
(B) RuntimeExceptions
(C) String
(D) Throwable

D
MCQ No - 9

Which of the following keyword is used by calling function to handle exception thrown by
called function?
(A) throws
(B) throw
(C) try
(D) catch

A
MCQ No - 10

Which of the following handles the exception when a catch is not used?
(A) finally
(B) throw handler
(C) default handler
(D) java run time system

MCQ No - 11

Which part of code gets executed whether exception is caught or not?


(A) finally
(B) try
(C) catch
(D) throw

A
MCQ No - 12

Which of the following should be true of the object thrown by a thrown statement?
(A) Should be assignable to String type
(B) Should be assignable to Exception type
(C) Should be assignable to Throwable type
(D) Should be assignable to Error type

C
MCQ No - 13

At runtime, error is recoverable.


(A) TRUE
(B) FALSE
B
MCQ No - 14

Which of these class is related to all the exceptions that can be caught by using catch?
(A) Error
(B) Exception
(C) RuntimeExecption
(D) All of the mentioned

B
MCQ No - 15

What exception thrown by parseInt() method?


(A) ArithmeticException
(B) ClassNotFoundException
(C) NullPointerException
(D) NumberFormatException

MCQ No - 16

What will be the output of the following Java code?


class exception_handling
{
public static void main(String args[])
{
try
{
int a = args.length;
int b = 10 / a;
System.out.print(a);
try
{
if (a == 1)
a = a / a - a;
if (a == 2)
{
int []c = {1};
c[8] = 9;
}
}
catch (ArrayIndexOutOfBoundException e)
{
System.out.println("TypeA");
}
catch (ArithmeticException e)
{
System.out.println("TypeB");
}
}
}
}

(A) TypeA
(B) TypeB
(C) Compile Time Error
(D) 0TypeB

C
MCQ No - 17

What will be the output of the following Java code?


class exception_handling
{
public static void main(String args[])
{
try
{
System.out.print("A");
throw new NullPointerException ("Hello");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
}
}

(A) A
(B) B
(C) Hello
(D) Runtime Exception

D
MCQ No - 18

A single try block must be followed by which of these?


(A) finally
(B) catch
(C) finally & catch
(D) none of the mentioned

C
MCQ No - 19

Which of these exceptions will occur if we try to access the index of an array beyond its
length?
(A) ArithmeticException
(B) ArrayException
(C) ArrayIndexException
(D) ArrayIndexOutOfBoundsException

D
MCQ No - 20

What is the use of try & catch?


(A) It allows us to manually handle the exception
(B) It allows to fix errors
(C) It prevents automatic terminating of the program in cases when an exception occurs
(D) All of the mentioned

MCQ No - 21

Which of these statements is incorrect?


(A) try block need not to be followed by catch block
(B) try block can be followed by finally block instead of catch block
(C) try can be followed by both catch and finally block
(D) try need not to be followed by anything

D
MCQ No - 22

Which of these methods return description of an exception?


(A) getException()
(B) getMessage()
(C) obtainDescription()
(D) obtainException()

B
MCQ No - 23

Which of these methods is used to print stack trace?


(A) obtainStackTrace()
(B) printStackTrace()
(C) getStackTrace()
(D) displayStackTrace()

B
MCQ No - 24

Which of these methods return localized description of an exception?


(A) getLocalizedMessage()
(B) getMessage()
(C) obtainLocalizedMessage()
(D) printLocalizedMessage()

A
MCQ No - 25

Which of these keywords is used to define interfaces in Java?


(A) interface
(B) Interface
(C) intf
(D) Intf
A

MCQ No - 26

Which of these can be used to fully abstract a class from its implementation?
(A) Objects
(B) Packages
(C) Interfaces
(D) None of the Mentioned

C
MCQ No - 27

Which of these access specifiers can be used for an interface?


(A) Public
(B) Protected
(C) private
(D) All of the mentioned

A
MCQ No - 28

Which of these keywords is used by a class to use an interface defined previously?


(A) import
(B) Import
(C) implements
(D) Implements

C
MCQ No - 29

Which of the following is the correct way of implementing an interface salary by class
manager?
(A) class manager extends salary {}
(B) class manager implements salary {}
(C) class manager imports salary {}
(D) none of the mentioned

B
MCQ No - 30
Which of the following is an incorrect statement about packages?
(A) Interfaces specifies what class must do but not how it does
(B) Interfaces are specified public if they are to be accessed by any code in the program
(C) All variables in interface are implicitly final and static
(D) All variables are static and methods are public if interface is defined pubic

MCQ No - 31

What will be the output of the following Java program?


interface calculate
{
void cal(int item);
}
class display implements calculate
{
int x;
public void cal(int item)
{
x = item * item;
}
}
class interfaces
{
public static void main(String args[])
{
display arr = new display;
arr.x = 0;
arr.cal(2);
System.out.print(arr.x);
}
}

(A) 0
(B) 2
(C) 4
(D) None of the mentioned

C
MCQ No - 32

Which of the following is the correct way of implementing an interface A by class B?


(A) class B extends A{}
(B) class B implements A{}
(C) class B imports A{}
(D) None of the mentioned

B
MCQ No - 33

All methods must be implemented of an interface.


(A) TRUE
(B) FALSE

A
MCQ No - 34

What type of variable can be defined in an interface?


(A) public static
(B) private final
(C) public final
(D) static final

D
MCQ No - 35

What does an interface contain?


(A) Method definition
(B) Method declaration
(C) Method declaration and definition
(D) Method name

MCQ No - 36

What type of methods an interface contain by default?


(A) abstract
(B) static
(C) final
(D) private

A
MCQ No - 37

What will happen if we provide concrete implementation of method in interface?


(A) The concrete class implementing that method need not provide implementation of that
method
(B) Runtime exception is thrown
(C) Compilation failure
(D) Method not found exception is thrown

C
MCQ No - 38

What happens when a constructor is defined for an interface?


(A) Compilation failure
(B) Runtime Exception
(C) The interface compiles successfully
(D) The implementing class will throw exception

Event-driven programming
MCQ No - 01

Which method is used to make rounded rectangle?


(A) setArcWidth()
(B) setRoundedCorner()
(C) setHeight()
(D) setWidth()

A
MCQ No - 02

Which is the incorrect instantiation of Color class?


(A) Color c = Color.web("0x0000FF",1.0);
(B) Color c = Color.rgb(0,0,255);
(C) Color color = new Color(0.0,0.3,0.2);
(D) Color c = Color.hsb(270,1.0,1.0);

C
MCQ No - 03

Which package is required to load images in JavaFX?


(A) javafx.scene.image
(B) javafx.scene.imageView
(C) javafx.image
(D) javafx.scene.image.imageView

A
MCQ No - 04

The _____ layout arranges all the nodes in our application in a single horizontal row.
(A) VBox
(B) FlowPane
(C) HBox
(D) TilePane

C
MCQ No - 05

The _____ layout arranges all the nodes in our application in a single vertical column.
(A) VBox
(B) FlowPane
(C) HBox
(D) TilePane

MCQ No - 06

The _____ layout arranges the nodes in our application in top, left, right, bottom and center
positions.
(A) TilePane
(B) BorderPane
(C) GridPane
(D) FlowPane

B
MCQ No - 07

The _____ layout arranges the nodes in our application on top of another.
(A) TilePane
(B) BorderPane
(C) StackPane
(D) FlowPane

C
MCQ No - 08

The _____ layout arranges multiple text nodes in a single flow.


(A) FlowPane
(B) GridPane
(C) BorderPane
(D) TextFlow

D
MCQ No - 09

The _____ layout anchors the nodes in our application at a particular distance from the pane.
(A) GridPane
(B) AnchorPane
(C) BorderPane
(D) TextFlow

B
MCQ No - 10

The _____ layout adds all the nodes of application in the form of uniformly sized tiles.
(A) TilePane
(B) GridPane
(C) BorderPane
(D) AnchorPane
A

MCQ No - 11

The _____ layout arranges the nodes in our application as a grid of rows and columns.
(A) TilePane
(B) GridPane
(C) BorderPane
(D) AnchorPane

B
MCQ No - 12

The _____ layout wraps all the nodes in a flow.


(A) TilePane
(B) BorderPane
(C) StackPane
(D) FlowPane

D
MCQ No - 13

The following are the phase of event handling in JavaFX


(A) Target Selection
(B) Route Construction
(C) Event Capturing Phase
(D) All of them

D
MCQ No - 14

In event capturing phase, event travels to all nodes in top to bottom manner.
(A) Target Selection
(B) Route Construction
(C) Event Capturing Phase
(D) Event Bubbling Phase

C
MCQ No - 15
Event dispatch chain is created in
(A) Target Selection
(B) Route Construction
(C) Event Capturing Phase
(D) Event Bubbling Phase

MCQ No - 16

Inner class can access all the members of outer class including private data members and
methods.
(A) TRUE
(B) FALSE

Concurrency

MCQ No - 1

Which of this method can be used to make the main thread to be executed last among all the
threads?
(A) stop()
(B) sleep()
(C) join()
(D) call()

B
MCQ No - 2

Which of this method is used to find out that a thread is still running or not?
(A) run()
(B) Alive()
(C) isAlive()
(D) checkRun()
C
MCQ No - 3

What is the default value of priority variable MIN_PRIORITY AND MAX_PRIORITY?


(A) 0 & 256
(B) 0 & 1
(C) 1 & 10
(D) 1 & 256

C
MCQ No - 4

Which of these method waits for the thread to terminate?


(A) sleep()
(B) isAlive()
(C) join()
(D) stop()

C
MCQ No - 5

Which of these method is used to explicitly set the priority of a thread?


(A) set()
(B) make()
(C) setPriority()
(D) makePriority()

MCQ No - 6

What is synchronization in reference to a thread?


(A) It’s a process of handling situations when two or more threads need access to a shared
resource
(B) It’s a process by which many thread are able to access same shared resource
simultaneously
(C) It’s a process by which a method is able to access many different threads simultaneously
(D) It’s a method that allow too many threads to access any information require

A
MCQ No - 7

Which of these method is used to implement Runnable interface?


(A) stop()
(B) run()
(C) runThread()
(D) stopThread()

B
MCQ No - 8

Which of these method is used to begin the execution of a thread?


(A) run()
(B) start()
(C) runThread()
(D) startThread()

B
MCQ No - 9

Which of these statement is incorrect?


(A) A thread can be formed by implementing Runnable interface only
(B) A thread can be formed by a class that extends Thread class
(C) start() method is used to begin execution of the thread
(D) run() method is used to begin execution of a thread before start() method in special cases

D
MCQ No - 10

Which of these method of Thread class is used to find out the priority given to a thread?
(A) get()
(B) ThreadPriority()
(C) getPriority()
(D) getThreadPriority()

MCQ No - 11
Which of these method of Thread class is used to Suspend a thread for a period of time?
(A) sleep()
(B) terminate()
(C) suspend()
(D) stop()

A
MCQ No - 12

Which method of predefined class Thread is used to check whether current thread being
checked is still running?
(A) isAlive()
(B) Join()
(C) isRunning()
(D) Alive()

A
MCQ No - 13

Which of these keywords are used to implement synchronization?


(A) synchronize
(B) syn
(C) synch
(D) synchronized

D
MCQ No - 14

Which of the interface contains all the methods used for handling thread related operations
in Java?
(A) Runnable interface
(B) Math interface
(C) System interface
(D) ThreadHandling interface

A
MCQ No - 15

Which of these class is used to make a thread?


(A) String
(B) System
(C) Thread
(D) Runnable

MCQ No - 16

Which of this interface is implemented by Thread class?


(A) Runnable
(B) Connections
(C) Set
(D) MapConnections

A
MCQ No - 17

What is the name of the method used to start a thread execution?


(A) init();
(B) start();
(C) run();
(D) resume();

B
MCQ No - 18

Which two are valid constructors for Thread?


1. Thread(Runnable r, String name)
2. Thread()
3. Thread(int priority)
4. Thread(Runnable r, ThreadGroup g)
5. Thread(Runnable r, int priority)

(A) 1 and 3
(B) 2 and 4
(C) 1 and 2
(D) 2 and 5

C
MCQ No - 19

Which two of the following methods are defined in class Thread?


1. start()
2. wait()
3. notify()
4. run()
5. terminate()

(A) 1 and 4
(B) 2 and 3
(C) 3 and 4
(D) 2 and 4

A
MCQ No - 20

Which method must be defined by a class implementing the java.lang.Runnable interface?


(A) void run()
(B) public void run()
(C) public void start()
(D) void run(int priority)

MCQ No - 21

Which will contain the body of the thread?


(A) run();
(B) start();
(C) stop();
(D) main();

A
MCQ No - 22

What will be the output of the following Java code?


class newthread extends Thread
{
newthread()
{
super("My Thread");
start();
}
public void run()
{
System.out.println(this);
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}

(A) My Thread
(B) Thread[My Thread,5,main]
(C) Compilation Error
(D) Runtime Error

B
MCQ No - 23

What will be the output of the following Java code?


class newthread extends Thread
{
Thread t;
newthread()
{
t = new Thread(this,"My Thread");
t.start();
}
public void run()
{
try
{
t.join()
System.out.println(t.getName());
}
catch(Exception e)
{
System.out.print("Exception");
}
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}

(A) My Thread
(B) Thread[My Thread,5,main]
(C) Exception
(D) Runtime Error

MCQ No - 24

What will be the output of the following Java code?


class newthread extends Thread
{
Thread t;
newthread()
{
t = new Thread(this,"New Thread");
t.start();
}
public void run()
{
System.out.println(t.isAlive());
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}

(A) 0
(B) 1
(C) TRUE
(D) FALSE

MCQ No - 25

What will be the output of the following Java code?


class newthread extends Thread
{
Thread t1,t2;
newthread()
{
t1 = new Thread(this,"Thread_1");
t2 = new Thread(this,"Thread_2");
t1.start();
t2.start();
}
public void run()
{
t2.setPriority(Thread.MAX_PRIORITY);
System.out.print(t1.equals(t2));
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}

(A) TRUE
(B) FALSE
(C) truetrue
(D) falsefalse

MCQ No - 26

What will be the output of the following Java code?


class newthread implements Runnable
{
Thread t;
newthread()
{
t = new Thread(this,"My Thread");
t.start();
}
public void run()
{
System.out.println(t.getName());
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}

(A) My Thread
(B) Thread[My Thread,5,main]
(C) Compilation Error
(D) Runtime Error

A
MCQ No - 27

What will be the output of the following Java code?


class newthread implements Runnable
{
Thread t;
newthread()
{
t = new Thread(this,"My Thread");
t.start();
}
public void run()
{
System.out.println(t);
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}

(A) My Thread
(B) Thread[My Thread,5,main]
(C) Compilation Error
(D) Runtime Error

B
MCQ No - 28

What will be the output of the following Java code?


class newthread implements Runnable
{
Thread t;
newthread()
{
t = new Thread(this,"New Thread");
t.start();
}
public void run()
{
t.setPriority(Thread.MAX_PRIORITY);
System.out.println(t);
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}

(A) Thread[New Thread,0,main]


(B) Thread[New Thread,1,main]
(C) Thread[New Thread,5,main]
(D) Thread[New Thread,10,main]

D
MCQ No - 29

What will be the output of the following Java program?


class newthread extends Thread
{
Thread t;
String name;
newthread(String threadname)
{
name = threadname;
t = new Thread(this,name);
t.start();
}
public void run()
{
}

}
class multithreaded_programing
{
public static void main(String args[])
{
newthread obj1 = new newthread("one");
newthread obj2 = new newthread("two");
try
{
Thread.sleep(1000);
System.out.print(obj1.t.isAlive());
}
catch(InterruptedException e)
{
System.out.print("Main thread interrupted");
}
}
}

(A) TRUE
(B) FALSE
(C) Main thread interrupted
(D) None of the mentioned

You might also like