0% found this document useful (0 votes)
152 views53 pages

ET MCQ Practice Questions

The document contains 29 multiple choice questions related to Java programming. The questions cover topics like data types, operators, OOP concepts, strings, arrays, collections, loops etc. and test the understanding of Java syntax, semantics and standard classes/APIs.

Uploaded by

eclaudeeee
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)
152 views53 pages

ET MCQ Practice Questions

The document contains 29 multiple choice questions related to Java programming. The questions cover topics like data types, operators, OOP concepts, strings, arrays, collections, loops etc. and test the understanding of Java syntax, semantics and standard classes/APIs.

Uploaded by

eclaudeeee
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/ 53

Q1.

public class Main

public static void main(String[] args) {

String str = "123";

int a = Integer.parseInt(str);

System.out.println(a);

A)1

B)12

C)123

D)Error

Q2. Public class Test{


Public static void main(String argos[])
{
String str1 = “on”;
String str2 = “tw”;
System.out.println(str1.concat(str2));
}
}
A)ontw

B)twoone

C)one

D)two

Q3. When an String is passed to a method, what does the method receive?
a) Copy of an string
b) Reference of an string
c) Value of index
d) None of the above

Q4. What will be the output of the following program?


public class mcq
{
public static void main(String[] args) {
char ch = 70;
System.out.println("character = " + ch);
}
}
a) character =65
b) Compilation Error
c) character =F
d) character =”65”

Q5.
How many objects will be created in the following?

String a = new String(“Interviewbit”);


String b = new String(“Interviewbit”);
Strinc c = “Interviewbit”;
String d = “Interviewbit”;

A)2

B)3

C)4

D)5

Q6.

Iden fy the output of the following program.

String str = “abcde”;


System.out.println(str.substring(0, 3));

a) abc
b) ab
c) bc
d) b

Q7. What is the output of the program?


public class CppBuzz {
public static void main(String[] args){
int a = 5+7*2+2*2+(7*3);
System.out.println(a);
}
}
a) 110
b) 10
c) 44
ti
d) 13

Q8. Find the output of the following code.


Public class Solution{
Public static void main(String… argos){
Int x = 5;
x * = (5 + 7);
System.out.println(x);
e) 22
f) 60
g) 10
h) NONE

Q9. What will be the output of the following program?


public class mcq
{
public static void main(String[] args) {
char ch = 98;
System.out.println("character = " + ch);
}
}
e) character =65
f) Compilation Error
g) character =b
h) character =”65”

Q10.

compareTo() returns

a) An int value
b) False
c) True
d) None

Q11.
What is the output of the following JAVA program ?
Class Test {

public static void main(String[] args) {

Test obj = new Test();

obj.start();

void start() {

String stra = ”do”;

String strb = method(stra);

System.out.print(“: ”+stra + strb);

String method(String stra) {

stra = stra + ”good”;

System.out.print(stra);

return“ good”;

a) dogood : dogood
B) dogood : dogoodgood
C) dogood : gooddogood
D) dogood : dodogood

Q12. ____ is used to nd and x bugs in the Java programs.

i) JVM

j) JRE

k) JDK

l) JDB
Q13. Evaluate the following Java expression, if x=3, y=5, and z=10:
++z + y - y + z + x++

a) 24

b) 23

c) 20
fi
fi
d) 25

Q14. What will be the output of the following program?

1. Predict the output of the following program.

class Test

public void demo(String str)

String[] arr = str.split(";");

for (String s : arr)

System.out.println(s);

public static void main(String[] args)

char array[] = {'a', 'b', ' ', 'c', 'd', ';', 'e', 'f', ' ',

'g', 'h', ';', 'i', 'j', ' ', 'k', 'l'};

String str = new String(array);

Test obj = new Test();

obj.demo(str);

a) ab cd
ef gh
ij kl
b) ab
c) None

Q15. Which of the following for loop declaration is not valid?


a) for ( int i = 7; i <= 77; i += 7 )
b) for ( int i = 99; i >= 0; i / 9 )
c) for ( int i = 20; i >= 2; - -i )
d) for ( int i = 2; i <= 20; i = 2* i )

Q16. What is the output of the program?


interface calculate {
int VAR = 0;
void cal(int item);
}
class display implements calculate {
int x;
public void cal(int item) {
if (item < 2)
x = VAR;
else
x = item * item;
}
}
class mcq {
public static void main(String[] args) {
display[] arr = new display[3];
for (int i = 0; i < 3; i++) arr[i] = new display();
arr[0].cal(0);
arr[1].cal(1);
arr[2].cal(2);
System.out.print(arr[0].x + " " + arr[1].x + " " + arr[2].x);
}
}
a) 0 1 2
b) 0 2 4
c) 0 0 4
d) 0 1 4

Q17. What will be the output of the following program?


public class mcq {
public static void main(String[] args) {
Super s = new Sub();
s.foo();
}
}
class Super {
void foo() {
System.out.println("Class Super");
}
}
class Sub extends Super {
static void foo() {
System.out.println("Class Sub");
}
}
a) Class Super
b) Class Sub
c) foo() in Sub cannot override foo() in Super
d) The statement “Super s = new Sub();” is illegal.

Q18. What will be the output of the following program?


public class mcq {
public static void main(String[] args) {
StringBuffer s1 = new StringBuffer("Consider");
s1.setCharAt(1,'i');
s1.setCharAt(7,'d');
System.out.println(s1);
}
}
e) Consider
f) Cinsided
g) Cinsoded
h) Cinsooed

Q19. What will be the output of the following code?


import java.util.*;
public class Main
{
public static void main(String[] args)
{
List<String> names = new LinkedList<>();
names.add("Raman");
names.add("Shyam");
names.add("Rahul");
names.add("Rohit");
names.add("Ramit");
ListIterator<String> it = names.listIterator(3);
while(it.hasPrevious()){
System.out.print(it.previous()+" ");
}
}
}
a) Raman Shyam Rahul
b) Shyam Rahul Rohit
c) Rahul Rohit Ramit
d) Rahul Shyam Raman

Q20. What will be the output of the following java code?


public class Main
{
public static void main(String[ ] args)
{
StringBuffer[ ] stringBuffers = new StringBuffer[10];
for (int i = 0; i < stringBuffers.length; i ++)
stringBuffers [i].append("index " + i);
System.out.println(stringBuffers);
}
}
a) Compile Time Error
b) Null Pointer Exception
c) index0index
d) 0123456789

Q21. Find predicted output of given program which is used to add and delete elements using Stack.
class mcq
{
public static void main(string args[])
{
Stack obj = new Stack();
obj.push(new Integer(4);
obj.push(new Integer(2));
obj.pop();
obj.push(new Integer(5));
System.out.println(obj);
}
}
a) [4, 5]
b) [3, 2]
c) [3, 2, 5]
d) [3, 5, 2]

Q22. What is the output of the program?


interface calculate {
int VAR = 0;
void cal(int item);
}
class display implements calculate {
int x;
public void cal(int item) {
if (item < 2)
x = VAR;
else
x = item * item;
}
}
class mcq {
public static void main(String[] args) {
display[] arr = new display[3];
for (int i = 0; i < 3; i++) arr[i] = new display();
arr[0].cal(0);
arr[1].cal(1);
arr[2].cal(2);
System.out.print(arr[0].x + " " + arr[1].x + " " + arr[2].x);
}
}
a) 0 1 2
b) 0 2 4
c) 0 0 4
d) 0 1 4
a)
Q23. Which of the following is incorrect statement regarding the use of generics and parameterized
types in Java
A. Generics provide type safety by shifting more type checking responsibilities to the compile
B. Generics and parameterized types eliminate the need for down casts when using Java Collection
C. When designing your own collections class (say, a linked list), generics and parameterized types
allow you to achieve type safety with just a single class de nition as opposed to de ning multiple
classe
D. All of the mentione

Q24. What will be the output of the following program?


public class mcq
{
public static void main(String[] args) {
char ch = 70;
System.out.println("character = " + ch);
}
}
a) character =65
b) Compilation Error
c) character =F
d) character =”65”
s

fi
fi
r

Q25.
How many objects will be created in the following?

String a = new String(“Interviewbit”);


String b = new String(“Interviewbit”);
Strinc c = “Interviewbit”;
String d = “Interviewbit”;

A)2

B)3

C)4

D)5

Q26.

Which of the following allows us to call generic methods as a normal method


A. Interfac
B. Inner clas
C. Type Interfac
D. All of the mentione

Q27. What will be the output of the following program?


public class mcq {
public static void main(String[] args) {
Super s = new Sub();
s.foo();
}
}
class Super {
void foo() {
System.out.println("Class Super");
}
}
class Sub extends Super {
static void foo() {
System.out.println("Class Sub");
}
}
a) Class Super
b) Class Sub
e

c) foo() in Sub cannot override foo() in Super


d) The statement “Super s = new Sub();” is illegal.

Q28. Find the output of the following code.


Public class Solution{
Public static void main(String… argos){
Int x = 5;
x * = (5 + 7);
System.out.println(x);
a) 22
b) 60
c) 10
d) NONE

Q29. What will be the output of the following program?


public class mcq
{
public static void main(String[] args) {
char ch = 98;
System.out.println("character = " + ch);
}
}
e) character =65
f) Compilation Error
g) character =b
h) character =”65”

Q30.

compareTo() returns

a) An int value
b) False
c) True
d) None

Q31.
What is the output of the following JAVA program ?
Class Test {

public static void main(String[] args) {

Test obj = new Test();

obj.start();

void start() {

String stra = ”do”;

String strb = method(stra);

System.out.print(“: ”+stra + strb);

String method(String stra) {

stra = stra + ”good”;

System.out.print(stra);

return“ good”;

a) dogood : dogood
B) dogood : dogoodgood
C) dogood : gooddogood
D) dogood : dodogood

Q32. ____ is used to nd and x bugs in the Java programs.

e) JVM

f) JRE

g) JDK

h) JDB
Q33. Evaluate the following Java expression, if x=3, y=5, and z=10:
++z + y - y + z + x++

a) 24

b) 23

c) 20
fi
fi
d) 25

Q34. What will be the output of the following program?

1. Predict the output of the following program.

class Test

public void demo(String str)

String[] arr = str.split(";");

for (String s : arr)

System.out.println(s);

public static void main(String[] args)

char array[] = {'a', 'b', ' ', 'c', 'd', ';', 'e', 'f', ' ',

'g', 'h', ';', 'i', 'j', ' ', 'k', 'l'};

String str = new String(array);

Test obj = new Test();

obj.demo(str);

a) ab cd
ef gh
ij kl
b) ab
c) None

Q35. What will be the output of the following Java program?


import java.util.*;
public class genericstack
{
Stack stk = new Stack ();
public void push(E obj)
{
stk.push(obj);
}
public E pop()
{
E obj = stk.pop();
return obj;
}
}
class Output
{
public static void main(String args[])
{
genericstack gs = new genericstack();
gs.push("Hello");
System.out.println(gs.pop());
}

A.
B. Hell
C. Runtime Erro
D. Compilation Erro

Q36. What is the output of the program?


interface calculate {
int VAR = 0;
void cal(int item);
}
class display implements calculate {
int x;
public void cal(int item) {
if (item < 2)
x = VAR;
else
x = item * item;
}
}
class mcq {
public static void main(String[] args) {
}

display[] arr = new display[3];


for (int i = 0; i < 3; i++) arr[i] = new display();
arr[0].cal(0);
arr[1].cal(1);
arr[2].cal(2);
System.out.print(arr[0].x + " " + arr[1].x + " " + arr[2].x);
}
}
a) 0 1 2
b) 0 2 4
c) 0 0 4
d) 0 1 4

Q37. What will be the output of the following program?


public class mcq {
public static void main(String[] args) {
Super s = new Sub();
s.foo();
}
}
class Super {
void foo() {
System.out.println("Class Super");
}
}
class Sub extends Super {
static void foo() {
System.out.println("Class Sub");
}
}
a) Class Super
b) Class Sub
c) foo() in Sub cannot override foo() in Super
d) The statement “Super s = new Sub();” is illegal.

Q38. What will be the output of the following java code?


class A {
public void aMethod() {
System.out.println("a Method from A");
}
}
class B extends A {
public void aMethod() {
System.out.println("a Method from B");
}
}
public class Main {
public static void main(String ar[]) {
A a = new B();
a.aMethod();
}
}
a) a Method from A
b) a Method from B
c) Compilation Error
d) Runtime Exception

Q39. What will be the output of the following code?


import java.util.*;
class genericstack <E>
{
Stack <E> stk = new Stack <E>();
public void push(E obj)
{
stk.push(obj);
}
public E pop()
{
E obj = stk.pop();
return obj;
} }
public class Main
{
public static void main(String args[])
{
genericstack <String> gs = new genericstack<String>();
gs.push("Hello");
gs.push(25);
System.out.println(gs.pop());
}
}
a) Compile Time Error: incompatible types
b) Compile Time Error: Stack class does not exist
c) Compile Time Error: E is not defined
d) No Compile Time Error

Q40. What will be the output of the following code?


import java.util.*;
public class Main
{
public static void main(String[] args)
{
List<String> names = new LinkedList<>();
names.add("Raman");
names.add("Shyam");
names.add("Rahul");
names.add("Rohit");
names.add("Ramit");
ListIterator<String> it = names.listIterator(3);
while(it.hasPrevious()){
System.out.print(it.previous()+" ");
}
}
}
a) Raman Shyam Rahul
b) Shyam Rahul Rohit
c) Rahul Rohit Ramit
d) Rahul Shyam Raman

Q41.Which IO stream in Java is used to read or write primitive data types?


a) BufferedDataInputStream and BufferedDataOutputStream
b) DataInputStream and DataOutputStream
c) BufferedInputStream and BufferedOutputStream
d) FilteredDataInputStream and FilteredDataOutputStream

Q42. Find predicted output of given program which is used to add and delete elements using Stack.
class mcq
{
public static void main(string args[])
{
Stack obj = new Stack();
obj.push(new Integer(4);
obj.push(new Integer(2));
obj.pop();
obj.push(new Integer(5));
System.out.println(obj);
}
}
a) [4, 5]
b) [3, 2]
c) [3, 2, 5]
d) [3, 5, 2]

Q43. What is the output of the program?


interface calculate {
int VAR = 0;
void cal(int item);
}
class display implements calculate {
int x;
public void cal(int item) {
if (item < 2)
x = VAR;
else
x = item * item;
}
}
class mcq {
public static void main(String[] args) {
display[] arr = new display[3];
for (int i = 0; i < 3; i++) arr[i] = new display();
arr[0].cal(0);
arr[1].cal(1);
arr[2].cal(2);
System.out.print(arr[0].x + " " + arr[1].x + " " + arr[2].x);
}
}
a) 0 1 2
b) 0 2 4
c) 0 0 4
d) 0 1 4

Q44. Which of the following is incorrect statement regarding the use of generics and parameterized
types in Java
A. Generics provide type safety by shifting more type checking responsibilities to the compile
B. Generics and parameterized types eliminate the need for down casts when using Java Collection
C. When designing your own collections class (say, a linked list), generics and parameterized types
allow you to achieve type safety with just a single class de nition as opposed to de ning multiple
classe
D. All of the mentione

Q45. What will be the output of the following program?


public class mcq
{
public static void main(String[] args) {
char ch = 70;
System.out.println("character = " + ch);
}
s

fi
fi
r

}
a) character =65
b) Compilation Error
c) character =F
d) character =”65”

Q46.
How many objects will be created in the following?

String a = new String(“Interviewbit”);


String b = new String(“Interviewbit”);
Strinc c = “Interviewbit”;
String d = “Interviewbit”;

A)2

B)3

C)4

D)5

Q47.

Which of the following allows us to call generic methods as a normal method


A. Interfac
B. Inner clas
C. Type Interfac
D. All of the mentione

Q48. What will be the output of the following program?


public class mcq {
public static void main(String[] args) {
Super s = new Sub();
s.foo();
}
}
class Super {
void foo() {
System.out.println("Class Super");
}
}
class Sub extends Super {
static void foo() {
e

System.out.println("Class Sub");
}
}
a) Class Super
b) Class Sub
c) foo() in Sub cannot override foo() in Super
d) The statement “Super s = new Sub();” is illegal.

Q49. Find the output of the following code.


Public class Solution{
Public static void main(String… argos){
Int x = 5;
x * = (5 + 7);
System.out.println(x);
a) 22
b) 60
c) 10
d) NONE

Q50. What will be the output of the following program?


public class mcq
{
public static void main(String[] args) {
char ch = 98;
System.out.println("character = " + ch);
}
}
e) character =65
f) Compilation Error
g) character =b
h) character =”65”

Q51.

compareTo() returns

a) An int value
b) False
c) True
d) None

Q52.
What is the output of the following JAVA program ?
Class Test {

public static void main(String[] args) {

Test obj = new Test();

obj.start();

void start() {

String stra = ”do”;

String strb = method(stra);

System.out.print(“: ”+stra + strb);

String method(String stra) {

stra = stra + ”good”;

System.out.print(stra);

return“ good”;

a) dogood : dogood
B) dogood : dogoodgood
C) dogood : gooddogood
D) dogood : dodogood

Q53. ____ is used to nd and x bugs in the Java programs.

e) JVM

f) JRE

g) JDK

h) JDB
Q54. Evaluate the following Java expression, if x=3, y=5, and z=10:
++z + y - y + z + x++

a) 24

b) 23

c) 20
fi
fi
d) 25

Q55. What will be the output of the following program?

1. Predict the output of the following program.

class Test

public void demo(String str)

String[] arr = str.split(";");

for (String s : arr)

System.out.println(s);

public static void main(String[] args)

char array[] = {'a', 'b', ' ', 'c', 'd', ';', 'e', 'f', ' ',

'g', 'h', ';', 'i', 'j', ' ', 'k', 'l'};

String str = new String(array);

Test obj = new Test();

obj.demo(str);

a) ab cd
ef gh
ij kl
b) ab
c) None

Q56. What will be the output of the following Java program?


import java.util.*;
public class genericstack
{
Stack stk = new Stack ();
public void push(E obj)
{
stk.push(obj);
}
public E pop()
{
E obj = stk.pop();
return obj;
}
}
class Output
{
public static void main(String args[])
{
genericstack gs = new genericstack();
gs.push("Hello");
System.out.println(gs.pop());
}

A.
B. Hell
C. Runtime Erro
D. Compilation Erro

Q57. What is the output of the program?


interface calculate {
int VAR = 0;
void cal(int item);
}
class display implements calculate {
int x;
public void cal(int item) {
if (item < 2)
x = VAR;
else
x = item * item;
}
}
class mcq {
public static void main(String[] args) {
}

display[] arr = new display[3];


for (int i = 0; i < 3; i++) arr[i] = new display();
arr[0].cal(0);
arr[1].cal(1);
arr[2].cal(2);
System.out.print(arr[0].x + " " + arr[1].x + " " + arr[2].x);
}
}
a) 0 1 2
b) 0 2 4
c) 0 0 4
d) 0 1 4

Q58. What will be the output of the following program?


public class mcq {
public static void main(String[] args) {
Super s = new Sub();
s.foo();
}
}
class Super {
void foo() {
System.out.println("Class Super");
}
}
class Sub extends Super {
static void foo() {
System.out.println("Class Sub");
}
}
a) Class Super
b) Class Sub
c) foo() in Sub cannot override foo() in Super
d) The statement “Super s = new Sub();” is illegal.

Q59. What will be the output of the following java code?


class A {
public void aMethod() {
System.out.println("a Method from A");
}
}
class B extends A {
public void aMethod() {
System.out.println("a Method from B");
}
}
public class Main {
public static void main(String ar[]) {
A a = new B();
a.aMethod();
}
}
a) a Method from A
b) a Method from B
c) Compilation Error
d) Runtime Exception

Q60. What will be the output of the following code?


import java.util.*;
class genericstack <E>
{
Stack <E> stk = new Stack <E>();
public void push(E obj)
{
stk.push(obj);
}
public E pop()
{
E obj = stk.pop();
return obj;
} }
public class Main
{
public static void main(String args[])
{
genericstack <String> gs = new genericstack<String>();
gs.push("Hello");
gs.push(25);
System.out.println(gs.pop());
}
}
a) Compile Time Error: incompatible types
b) Compile Time Error: Stack class does not exist
c) Compile Time Error: E is not defined
d) No Compile Time Error

Q61. What will be the output of the following code?


import java.util.*;
public class Main
{
public static void main(String[] args)
{
List<String> names = new LinkedList<>();
names.add("Raman");
names.add("Shyam");
names.add("Rahul");
names.add("Rohit");
names.add("Ramit");
ListIterator<String> it = names.listIterator(3);
while(it.hasPrevious()){
System.out.print(it.previous()+" ");
}
}
}
a) Raman Shyam Rahul
b) Shyam Rahul Rohit
c) Rahul Rohit Ramit
d) Rahul Shyam Raman

Q62.Which IO stream in Java is used to read or write primitive data types?


a) BufferedDataInputStream and BufferedDataOutputStream
b) DataInputStream and DataOutputStream
c) BufferedInputStream and BufferedOutputStream
d) FilteredDataInputStream and FilteredDataOutputStream

Q63. 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;

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

a) Public

b) Protected

c) private

d) All of the mentioned

Q65 Which of the following is thread safe ?


A. StringBuffer
B. StringBuilder
C. All
D. None of these

Q66. What is the numerical range of a char?


a) -128 to 127
b) -(215) to (215) - 1
c) 0 to 32767
d) 0 to 65535

Q67. Which of the following for loop declaration is not valid?


a) for ( int i = 99; i >= 0; i / 9 )
b) for ( int i = 20; i >= 2; i++ )
c) for ( int i = 2; i <= 20; i = 2*i )
d) for ( int i = 7; i <= 77; i += 7 )

Q68. What are the types of memory allocated in memory in java?


a) Heap memory
b) Stack memory
c) Both A and B
d) None of these

Q69. Which of the following Sets maintains the insertion order?


a) HashSet
b) TreeSet
c) LinkedHashSet
d) All the answers are true

Q70. The break statement in Java is used to ___.


A. Terminates from the loop immediately
B. Terminates from the program immediately
C. Skips the current iteration
D. All of these

Q71. Wrapper class in java is ___.


a) Used to encapsulate primitive data types
b) Declare new classes called wrapper
c) Create a new instance of the class
d) None of these

Q72 . Which of these keywords must be used to handle the exception thrown by try block in a java
code ?

a) try

b) finally

c) throw

d) catch

Q73.
public class Main {
public static void main(String arg[]) {
int i;
for (i = 1; i <= 12; i += 2) {
if (i == 8) {
System.out.println(i);
break;
}
}
}
}
a) 1
b) No output
c) 8
d) 1357911

Q74.
class Hello
{
static int a;

static
{
a = 14;
System.out.println ("inside static block");
System.out.println ("a = " + a);
}

Test()
{
System.out.println ("inside constructor");
a = 10;
}

public static void func()


{
a = a + 1;
System.out.println ("a = " + a);
}

public static void main(String[] args)


{

Hello obj = new Hello();


obj.func();

}
}

a) inside static block


a = 1
inside constructo
a = 1
b) Null Pointer Exception
c) Runtime Error
d) inside static bloc
a = 1
inside constructo
a = 1

Q75. What will be the output of the following java code?


class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
4

System.out.println("Dogs can walk and run");


}
public void bark() {
System.out.println("Dogs can bark");
}
}
public class TestDog {
public static void main(String args[]) {
Animal b = new Dog
b.move();
}
}

a) Dogs can bark


b) Dogs can walk and run
c) Runtime Exception
d) Animal can walk

Q76. What will be the output of the following code?

public class Test {


public static void main(String[] args) {
String s = new String("5");
System.out.println(1 + 10 + s + 1 + 10);
}
}

a) 11511
b) 1105110
c) 115110
d) 27

Q77.
public class Hello {
public static void main(String[] args) {
TreeSet<String> treeSet = new TreeSet<>();
treeSet.add("Sky");
treeSet.add("Is");
treeSet.add("Blue");
treeSet.add("SkyIsBlue");
for (String temp : treeSet)
System.out.print(temp + " ");
System.out.println("\n");
}
}
A. Blue Is Sky SkyIsBlue
B. Is Sky Blue SkyISblue
C. Blue Sky Is
D. SkyIsBlue

Q78 What is the output of this program?

class Main
{
public static void main(String args[])
{
try
{
System.out.print("Hello" + " " + 1 / 0);
}
catch(ArithmeticException e)
{
System.out.print("World");
}
}
}

A. Hello
B. World
C. HelloWorld
D. Hello World

Q79
import java.util.ArrayList;
class Demo {
public void show()
{
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(4);
list.add(7);
list.add(1);
for (int number : list) {
System.out.print(number + " ");
}
}
} public class Main {
public static void main(String[] args)
{
Demo demo = new Demo();
demo.show();
}
}

A. Compilation Error
B. 4 7 1
C. 1 4 7
D. None

Q80
int values[ ] = {1,2,3,4,5,6,7,8,9,10};
for(int i=0;i< Y; ++i)
System.out.println(values[i]);
Find the value of value[i]?
a. 10
b. 11
c. 15
d. None of the above

Q 81 What is the output of this program?

package pkg;
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

Q 82 . What will be the output of the following code?


public class Main {
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) 1234
b) 0000
c) 12
d) 00

Q83. int nums [ ] = {1,2,3,4,5,6,7,8,9,10};


for(int i=0;i<Y; ++i)
System.out.println(values[i]);
a) 9
b) 10
c) 11
d) None of above

Q84 ArrayList list = new ArrayList();

What is the initial capacity of ArrayList ?


a) 5
b) 10
c) 15
d) 0

Q85 The OutputStreams includes methods that are designed to perform the following tasks.
i) closing streams ii) flushing streams
iii) reading bytes iv) writing bytes

A) ii, iii and iv only


B) i, ii and iii only
C) i, ii and iv only
D) All i, ii, iii and iv

Q86. What will be the output of the following program?


public class Hello
{
public static void main(String[] args) {
char ch = 48;
System.out.println(ch);
}
}
a) 48
b) Compilation Error
c) 0
d) Runtime Error

Q87 . Which of these classes is not part of the Collection framework in Java?
a) Queue
b) Stack
c) ArrayList
d) Map
Q88. Threads ar
a) light weight process
b) heavyweight process
c) both
d) none

Q89.

int a = 2, b = 3, c = 4, d = 5;
float k = 4.3f;

System.out.println (c=c++);

1. 2

2. 4

3. 5

4. 8

Q90. public class Main {


public static void main(String args[]) {
int arr[] = {10, 20, 30, 40, 50};
for(int i=0; i < arr.length; i++)
{
System.out.print(" " + arr[i]);
}
}
e

}
a) 10 20 30 40 50
b) Compile Time Error
c) Runtime Error
d) 10 20 30 40

Q91. What will be the output of the following program?

class Test {
public static void main(String[] args) {
for(int i = 0; 0; i++)
{
System.out.println("Hello");
break;
}
}
}

a) Hello
b) Empty Output
c) Compiler error
d) Runtime error

Q92.
class Main {
public static void main(String args[]) {
int t;
System.out.println(t);
}
}
Class Super
a) 0
b) Error
c) 1
d) Garbage value

Q93.
class Test
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1 == arr2)
System.out.println("Same");
else
System.out.println("Not Same");
}
}

a) Compile Time Error


b) Runtime Error
c) Same
d) Not Same

Q94.
What is the output of this program?

import java.util.LinkedList;

class Demo {

public void show()

LinkedList<String> list = new LinkedList<String>();

list.add("Element1"); // line 6

list.add("Element2");

System.out.print(list.getFirst()); // line 8

} public class Main {

public static void main(String[] args)

Demo demo = new Demo();

demo.show();

A. Element1

B. Compilation Error at line 8

C. Runtime Error

D. Element2
Q95. What will be the output of the following java code?
class A {
public void aMethod() {
System.out.println("a Method from A");
}
}
class B extends A {
public void aMethod() {
System.out.println("a Method from B");
}
}
public class Main {
public static void main(String ar[]) {
A a = new B();
a.aMethod();
}
}
a) a Method from A
b) a Method from B
c) Compilation Error
d) Runtime Exception

Q96. What will be the output of the following code?

public class Main {


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) 1234
b) 0000
c) 12
d) 00

Q97.
import java.util.*;
class Demo {
public void show()
{
List<Integer> list = new LinkedList<Integer>();
list.add(1);
list.add(4);
list.add(7);
list.add(5);
Collections.sort(list); // line 8
System.out.println(list);
}
} public class Main {
public static void main(String[] args)
{
Demo demo = new Demo();
demo.show();
}
}
A. Compilation Error at line 9
B. [1, 4, 5, 7]
C. [1, 4, 7, 5]
D. Runtime Error

Q 98 public class Test {

public static void main(String[] args) {


String s1 = "abc";
StringBuffer s2 = new StringBuffer(s1);
System.out.println(s1.equals(s2));
}
}

a) false
b) true
c) ClassCastException at runtime
d) Compile-time error

Q 99 import java.util.*;

public class Hello {


public static void main(String[] args) {
LinkedHashSet<Integer> set = new LinkedHashSet<>();
set.add(1);
set.add(2);
set.add(2);
set.add(4);

for (int temp : set)


System.out.print(temp + " ");

System.out.println("\n");

}
}

a) 1 2 4
b) 1 2 2 4
c) 4 2 2 1
d) Compile-time error

Q100. What will be the output of the following code?

public class Test {


public static void main(String[] args) {
String s = new String("5");
System.out.println(s+1 + 10 + s + 1 + 10);
}
}

a) 11511
b) s1105110
c) 51105110
d) 27

Q 101.What is the output of this program?

class Main
{
public static void main(String args[])
{
try
{
System.out.print("Hello" + " " + 1 / 0);
}
catch(ArithmeticException e)
{
System.out.print("World");
}
}
}

A. Hello
B. World
C. HelloWorld
D. Hello World

Q 102 .What will be the output of the following code?

public class hashSet {


public static void main(String[] args)
{
HashSet<String> hashSet = new HashSet<>();
hashSet.add("Hello");
hashSet.add("For");
hashSet.add("Hello");
hashSet.add("Good");

System.out.println(hashSet);
}
}

a) Hello For Good


b) Hello For Hello Good
c) Good Hello For Hello
d) Hello Good Hello For

Q103.What are generics in Java?


a) A type of data structure
b) A type of programming language
c) A type of algorithm
d) A way to create classes that work with di erent data types

Q104.Which of these is not a bitwise operator?


a) &
b) &=
c) |=
d) %
 

ff
Q105.What is the Java Collections Framework?
a) A set of classes and interfaces implementing complex collection data structures
b) A set of classes and interfaces implementing simple collection data structures
c) A set of classes and interfaces implementing complex algorithms
d) A set of classes and interfaces implementing simple algorithms

Q106. What is a class in Java?


a) A method in Java
b) A variable in Java
c) A blueprint or template for creating objects that de ne its properties and
methods
d) An object in Java

Q107. Wrapper class in java is ___.


a) Used to encapsulate primitive data types
b) Declare new classes called wrapper
c) Create a new instance of the class
d) None of these

Q108. What is inheritance in Java?


a) A mechanism where an element acquires all houses and behaviors of a
discerning element
b) A mechanism where an element acquires some houses and behaviors of a discerning
element
c) A mechanism where an element acquires none of the houses and behaviors of a
discerning element
d) none of the above
fi
Q109.What is an object in Java?
a) A blueprint or template that de nes the variables and methods common to all objects
of a certain kind
b) A logical entity that can't be physical
c) An instance of a class that has its own state and behavior
d) A variable which is created inside the class but outside the method

Q110 What is the use of "this" keyword in Java?


a) Refers to the current instance of a class
b) Refers to a variable or method that belongs to the class
c) Refers to an instance of an object
d) Refers to a logical entity that can't be physical

Q111. What is the purpose of try-catch block in Java?


a) To declare an exception in its signature
b) To throw an exception
c) To catch an exception and handle it properly
d) To declare a variable or method that belongs to the class

Q112. What is the purpose of JDBC in Java?


a) To provide a standard abstraction for Java applications to communicate with
various databases
b) To provide a standard abstraction for Java applications to communicate with various
internet protocols
c) To provide a standard abstraction for Java applications to communicate with various
le systems
d) To provide a standard abstraction for Java applications to communicate with various
operating systems
fi
fi
Q113. What will be the output of the following program?
List<Integer> list = new ArrayList<>();
list.add(100);
list.add(200);
list.add(300);
list.add(400);
System.out.println(list.get(0));

a) 100
b) 200
c) 300
d) 400

Q114 What will be the Output of the following code?

public class Main{


public static void main(String[] args) {
String s = new String("5");
System.out.println(1 + 10 + s + 1 + 10);
}
}

a) 11511
b) 1105110
c) 115110
d) 27

Q115.What is the output of the following Java program?


class Main {
public static void main(String[] args) {
try {
int x = 0;
int y = 5 / x;
} catch(Exception e) {
System.out.print(“Exception ");
} nally {
System.out.print("Finally");
}
}
}

a) Exception Finally
b) Exception
c) Finally Exception
d) none of the above

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

class GenericsExample {
public static void main(String[] args) {
GenericClass<Integer> gc1 = new GenericClass<>(10);
GenericClass<String> gc2 = new GenericClass<>("Hello");

System.out.println(gc1.getData());
System.out.println(gc2.getData());
}
}

class GenericClass<T> {
private T data;

public GenericClass(T data) {


this.data = data;
}

public T getData() {
return data;
}
}
a) 10 Hello
b) Hello 10
c) Error
d) None of the above
fi
Q117.What are the two ways to create a thread in Java?
a) By extending the Thread class
b) By implementing the Runnable interface
c) By using Collection framework
d) Both a and b

Q118 What is the output of the following Java program?


python
class Main {
public static void main(String[] args) {
Thread myThread= new Thread();
System.out.println(myThread.isAlive());
}
}

a) true
b) can be either true or false
c) Compilation error
d) false

Q119 What is synchronisation in Java?


a) The capability to control the access of multiple threads to any shared resource
b) The process of creating multiple threads
c) The process of stopping a running thread
d) None of the above

Q120 What is the di erence between Queue and Stack in Java?


a) Queue follows FIFO (First In First Out) while Stack follows LIFO (Last In First Out)
b) Queue follows LIFO while Stack follows FIFO
c) Both follow FIFO
d) None of the above
ff
Q121 What will be the output of the following Java program?
class Base {
public void Print() {
System.out.print(“Base ");
}
}
class Derived extends Base {
public void Print() {
System.out.print(“Derived ");
}
}
class Main{
public static void DoPrint(Base o) {
o.Print();
}
public static void main(String[] args) {
Base x = new Base();
Base y = new Derived();
Base z = new Base();
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}
a) Base Derived Base
b) Derived Derived Base
c) Base Base Base
d) Derived Base Base

Q122. What will be the output of the following code snippet?


public class Main {

public static void main(String[] args) {


String s1 = "abc";
StringBu er s2 = new StringBu er(s1);
System.out.println(s1.equals(s2.toString()));
}
}
a) Error
b) false
c) true
d) ClassCastException at runtime
ff
ff
Q123.What are generics in Java?
a) A type of data structure
b) A type of programming language
c) A type of algorithm
d) A way to create classes that work with di erent data types

Q124.Which of these is not a bitwise operator?


a) &
b) &=
c) |=
d) %

Q125.What is the Java Collections Framework?


a) A set of classes and interfaces implementing complex collection data structures
b) A set of classes and interfaces implementing simple collection data structures
c) A set of classes and interfaces implementing complex algorithms
d) A set of classes and interfaces implementing simple algorithms

Q126. What is a class in Java?


a) A method in Java
b) A variable in Java
c) A blueprint or template for creating objects that de ne its properties and
methods
d) An object in Java

Q127.What is access control in Java?


a) The ability to restrict access to certain parts of code, such as variables and
methods, to prevent unauthorised modi cation or access.
b) The ability to access any part of code without restrictions
c) The ability to modify any part of code without restrictions
fi
ff
fi
d) None of the above

Q128. What is inheritance in Java?


a) A mechanism where an element acquires all houses and behaviors of a
discerning element
b) A mechanism where an element acquires some houses and behaviors of a discerning
element
c) A mechanism where an element acquires none of the houses and behaviors of a
discerning element
d) none of the above

Q129. What is Comparable in Java?


a) An interface that allows sorting of objects based on their natural order
b) An interface that allows sorting of objects based on a custom order
c) A class that allows sorting of objects based on their natural order
d) A class that allows sorting of objects based on a custom order

Q130 Which keyword is used to de ne packages in Java?


a) pkg
b) Pkg
c) package
d) Package

Q131. What is the toString() method in Java?


• a) A method used to convert an object to a string representation
• b) A method used to convert a string to an object representation
• c) A method used to compare two strings for equality
• d) None of the above
fi
Q132. What is the purpose of JDBC in Java?
a) To provide a standard abstraction for Java applications to communicate with
various databases
b) To provide a standard abstraction for Java applications to communicate with various
internet protocols
c) To provide a standard abstraction for Java applications to communicate with various
le systems
d) To provide a standard abstraction for Java applications to communicate with various
operating systems

Q133. What will be the output of the following program?


List<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
list.add(40);
System.out.println(list.get(3));

a) 10
b) 20
c) 30
d) 40

Q134.What is the superclass of all errors and exceptions in the Java language?
a) RunTimeExceptions
b) Throwable
c) Catchable
d) None of the above

Q135.What is the output of the following Java program?


fi
class Main {
public static void main(String[] args) {
try {
int x = 0;
int y = 5 / x;
} catch(Exception e) {
System.out.print(“Exception ");
} nally {
System.out.print("Finally");
}
}
}

a) Exception
b) Exception Finally
c) Finally
d) none of the above

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

class GenericsExample {
public static void main(String[] args) {
GenericClass<Integer> gc1 = new GenericClass<>(10);
GenericClass<String> gc2 = new GenericClass<>("Hello");

System.out.println(gc1.getData());
System.out.println(gc2.getData());
}
}

class GenericClass<T> {
private T data;

public GenericClass(T data) {


this.data = data;
}

public T getData() {
return data;
}
fi
}
a) 10 Hello
b) Hello 10
c) Error
d) None of the above

Q137.What are the two ways to create a thread in Java?


a) By extending the Thread class
b) By implementing the Runnable interface
c) By using Collection framework
d) Both a and b

Q138 What is the output of the following Java program?


python
class Main {
public static void main(String[] args) {
Thread t = new Thread();
System.out.println(t.isAlive());
}
}

a) true
b) false
c) Compilation error
d) None of the above

Q139 What is synchronisation in Java?


a) The capability to control the access of multiple threads to any shared resource
b) The process of creating multiple threads
c) The process of stopping a running thread
d) None of the above
Q140 What is the di erence between Queue and Stack in Java?
a) Queue follows FIFO (First In First Out) while Stack follows LIFO (Last In First Out)
b) Queue follows LIFO while Stack follows FIFO
c) Both follow FIFO
d) None of the above

Q141 What will be the output of the following Java program?


class Base {
public void Print() {
System.out.print(“Base ");
}
}
class Derived extends Base {
public void Print() {
System.out.print(“Derived ");
}
}
class Main{
public static void DoPrint(Base o) {
o.Print();
}
public static void main(String[] args) {
Base x = new Base();
Base y = new Derived();
Base z = new Base();
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}
a) Base Derived Base
b) Derived Derived Base
c) Base Base Base
d) Derived Base Base

Q142. What will be the output of the following code snippet?


public class Main {

public static void main(String[] args) {


String s1 = "abc";
StringBu er s2 = new StringBu er(s1);
System.out.println(s1.equals(s2.toString()));
ff
ff
ff
}
}
a) Error
b) false
c) true
d) ClassCastException at runtime

You might also like