wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

JAVA TEST for BATCH J6_J8

Total questions: 100

Worksheet time: 2hrs 24mins

Name
Class
Date
1.

Predict the output:

a)

"Hello Abstract World"

b)

compile time error

c)

runtime exception

d)

None of the above

2.

Predict the output:

a)

[PJP, Java]

[Python, PJP, Java]

[PJP, Java]

b)

Compile time error

c)

Run time error

d)

None of the above

3.

Predict the output:

a)

Prints nothing

b)

Compile Time Error: variable x is already defined

c)

9

d)

8

4.

Predict the output:

Run command is issued as: Java Sample "Hello World"

a)

"Hello World"

b)

"Hello

c)

Hello

d)

Hello World

5.

Predict the output:

a)

Prints nothing

b)

0

c)

null

d)

OutOfBoundsException

6.

Predict the output:

a)

Child display..

b)

Child display..Parent display..

c)

Parent display..Child display..

d)

Compile Time Error

7.

Predict the output:

a)

true false

b)

true true

c)

false true

d)

false false

8.

Predict the output:

a)

There is no reverse() available for StringBuffer

b)

emocleW yajA

c)

Ajay Welcome

d)

yajA emocleW

9.

Predict the output:

a)

a = 0

Divide by zero error

b)

a = 0

inside the finally block

c)

a = 0

Divide by zero error

inside the finally block

d)

Divide by zero error

inside the finally block

10.

Which of the below is/are valid identifier with the main method?

a)

public

b)

private

c)

static

d)

this

11.

What is the extension of java code files?

a)

.class

b)

.txt

c)

.java

d)

.js

12.

class KIET_Test {

int x;

}

class Main {

public static void main(String args[]) {

KIET_Test t = new KIET_Test();

System.out.println(t.x);

}

}

a)

garbage value

b)

0

c)

compiler error

d)

runtime error

13.

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;

14.

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

a)

malloc

b)

alloc

c)

new

d)

this

15.

What is the output of the below Java code snippet?

char ch = 'A';//ASCII 65

int a = ch + 1;

ch = (char)a;

System.out.println(ch);

a)

A) 66

b)

B) A

c)

C) B

d)

D) 65

16.

What is the output of the following code snippet?

int i = 0; for(i = 0 ; i < 5; i++)

{ }

System.out.println(i);

a)

A. 5

b)

B. 0

c)

C. 4

d)

D. Compilation Error

17.

Predict the value of k,

int k =10;

k=k+k++;

a)

11

b)

20

c)

21

d)

22

18.

What is an exception

a)

Error occurred during the execution of a program

b)

Bug occurred during the compile time of a program

c)

Simply an Error

d)

It is related to input values

19.

Choose an exception if attempt to divide number by zero


int number = 89 / 0;

System.out.println("The answer is " + number);

a)

ArithmeticException

b)

NullPointerException

c)

NumberFormatException

d)

ArrayIndexOutOfBoundException

20.

Find the output for the following.

public class IncDec

{

public static void main(String s[])

{

int a = 1;

int b = 2;

int c;

int d;


c = ++b;

d = a++;


c++;


System.out.println("a = " + a);

System.out.print("b = " + b);

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

System.out.print("d = " + d);

}

}

a)

a = 2 b = 3 c = 4 d = 1

b)

a = 2 b = 3 c = 4 d = 1

c)

Program does not compile.

d)

a = 1 b = 2 c = 4 d = 2

21.

What will be the output?

a)

24

b)

32

c)

23

d)

31

22.

________ keyword is used to monitor an exception.

a)

try

b)

catch

c)

throw

d)

throws

23.

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.5

c)

1.5 1

d)

1 1.5

24.

class increment {

public static void main(String args[]) {

int g = 3;

System.out.print(++g * 8); } }

a)

24

b)

32

c)

25

d)

34

25.

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)

4 4 3

b)

3 3 4

c)

3 4 4

d)

4 3 3

26.

class bitwise_operator {

public static void main(String args[]) {

int var1 = 42;

int var2 = ~var1;

System.out.print(var1 + " " + var2); } }

a)

42 42

b)

42 -43

c)

43 42

d)

43 -42

27.

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 5

c)

5 7

d)

7 7

28.

class rightshift_operator {

public static void main(String args[]) {

int x; x = 10;

x = x >> 1;

System.out.println(x); } }

a)

2

b)

7

c)

5

d)

10

29.
What are the primary goals of the collection framework in Java?
a)
To replace arrays completely
b)
To provide a way to organize files
c)
To manage and manipulate collections of objects
d)
To optimize CPU usage
30.
Which Java collection framework classes are commonly used?
a)
Vector and Stack
b)
Array and List
c)
ArrayList and LinkedList
d)
Set and Map
31.
What are the advantages of using the collection framework?
a)
Faster execution of programs
b)
Increased memory usage
c)
Code reusability and reliability
d)
Removal of null references
32.
What are the main differences between a collection framework and a data structure?
a)
They are the same
b)
Data structures store data, while collection framework doesn't
c)
Collection framework provides interfaces and classes to work with data structures
d)
Collection framework doesn't provide any classes
33.
What is the role and importance of the Collection framework?
a)
It doesn't have any role or importance
b)
To complicate the programming process
c)
To make Java slower
d)
To provide reusable data structures and algorithms
34.
Can you provide a real-life example of using the collection framework?
a)
Sorting algorithms
b)
Shopping cart in an e-commerce application
c)
Database management
d)
Video game graphics rendering
35.
Which interfaces are commonly used from the Collection framework?
a)
List and Array
b)
Vector and Map
c)
Set and List
d)
Set and Map
36.
What is the difference between HashSet and HashMap in Java?
a)
HashSet is used for sorting, while HashMap is for searching
b)
HashSet stores unique elements, while HashMap stores key-value pairs
c)
HashSet is a synchronized collection, while HashMap is not
d)
HashSet has better performance than HashMap
37.
What is the difference between a collection and a Map interface?
a)
There is no difference, they are the same
b)
Collection stores key-value pairs, while Map stores elements
c)
Collection stores elements, while Map stores key-value pairs
d)
Collection has better performance than Map
38.
Can a HashMap have duplicate keys?
a)
Yes
b)
No
39.
Why is a String immutable in Java?
a)
To make programming harder
b)
To improve memory usage
c)
To prevent modification after creation
d)
To make it thread-safe
40.
What are the two types of collection sets?
a)
SortedSet and HashSet
b)
SortedSet and LinkedSet
c)
TreeSet and HashSet
d)
TreeSet and LinkedHashSet
41.

What is Collection in Java?

a)

A group of objects

b)

A group of classes

c)

A group of interfaces

d)

None of the mentioned

42.

Which of these interface is not part of Java’s collection framework?

a)

List

b)

Sorted List

c)

Set

d)

Sorted Map

43.

Which of these return type of hasNext() method of an iterator?

a)

Integer

b)

Double

c)

Boolean

d)

Collections Object

44.

Which of these methods deletes all the elements from invoking collection?

a)

clear();

b)

reset();

c)

delete();

d)

remove();

45.

Which of these interface declares core method that all collections will have?

a)

Collection

b)

EventListner

c)

Comparator

d)

Set

e)

List

46.

What implementation of Iterator can traverse a collection in both directions?

a)

Iterator

b)

ListIterator

c)

SetIterator

d)

MapIterator

47.

Which is faster and uses less memory?

a)

ListEnumeration

b)

Iterator

c)

Enumeration

d)

ListIterator

48.

Which interface does java.util.Hashtable implement?

a)

Java.util.Map

b)

Java.util.List

c)

Java.util.HashTable

d)

Java.util.Collection

49.

The default capacity of a Vector is:

a)

10

b)

12

c)

8

d)

0

50.

The Comparable interface contains which called?

a)

toCompare

b)

compare

c)

compareTo

d)

compareWith

51.

Which of these is synchronized?

a)

TreeMap

b)

HashMap

c)

Hashtable

d)

All of the above

52.

How can you sort given HashMap on basis of values

a)

Implement Comparator interface and override its compare method

b)

It is not possible

c)

Implement Comparator interface and override its compareTo method

d)

Implement Comparator interface and override its comparable method

53.

-Entries are organized as key/value pairs.

-Duplicate entries replace old entries.

-Entries are sorted using a Comparator or the Comparable interface.

Which interface of the java.util package offers the specified behavior?

a)

List

b)

Map

c)

Set

d)

SortedSet

e)

SortedMap

54.

-Entries are not organized as key/value pairs.

-Duplicate entries are rejected.

Which interface of the java.util package offers the specified behavior?

a)

List

b)

Map

c)

Set

d)

None of the above

55.

-Entries are not organized as key/value pairs.

-Duplicate entries are rejected.

Which interface of the java.util package offers the specified behavior?

a)

List

b)

Map

c)

Set

d)

None of the above

56.
a)

[Computer, Network, Networks, Programming, Security]

b)

[Computer, Programming, Network, Networks, Programming, Security]

c)

[Computer, Programming, Computer, Networks, Network, Security]

d)

[Computer, Networks, Network, Programming, Security]

57.

Which of the following is/are correct statements?

a)

HashSet class implements the Set interface

b)

HashSet does not allow duplicate elements that means you can not store duplicate values in HashSet.

c)

HashSet permits to have a single null value.

d)

HashMap does not allow duplicate keys however it allows to have duplicate values.

e)

HashMap and HashSet are threadsafe

58.

Which of the following statement(s) is/are TRUE?

a)

Both HashMap and HashSet are not synchronized.

b)

HashMap does not allow duplicate keys however it allows to have duplicate values.

c)

HashMap permits single null key and any number of null values.

d)

Both HashMap and HashSet are synchronized.

59.
a)

{"Computer", "Programming","Networks","null"}

b)

{"Computer", "Programming","Networks",null,"null"}

c)

Line 11 : Genetates NullPointer Exception

d)

Line12: Generates NullPointerException

60.

List listNames = new ArrayList();

listNames.add("Tom");

listNames.add("Mary");

listNames.add("Peter");


String name2 = (String) listNames.get(1);

System.out.println(name2);

What is the output of the above code

a)

Tom

b)

Mary

c)

Peter

d)

raises Exception

61.

int x = 5;

int y = 6

int z = 50;

System.out.println(x + y + z);


What will be the output?

a)

61

b)

5650

c)

66

d)

x + y + z

62.

Which of the following is the right way of defining char variable?

a)

char myGrade = 'B';

b)

char myGrade = "B";

c)

char myGrade 'B';

d)

char myGrade = "B"

63.

x /= 3


Which of the below statements matches with above statement?

a)

x = 3 / x

b)

x = x / 3

c)

x / 3

d)

x /x = 3

64.

int x = 4;

System.out.println(x < 5 && x < 10);


What will be the output?

a)

false

b)

true

c)

4

d)

x < 5 && x < 10

65.

What is the output of below code?


int x = 5;

System.out.println(x++);

System.out.println(x);

a)

5

5

b)

6

6

c)

5

6

d)

6

5

66.

Command to run a Java program

a)

javaa

b)

java

c)

jaava

d)

jaavaa

67.

java compiler converts the java file into _________ file.

a)

.jvm

b)

.class

c)

.obj

d)

.java

68.

Which of these are selection statements in Java?

a)

if()

b)

for

c)

continue

d)

break

69.

Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?

a)

for

b)

while

c)

do..while

d)

None of the above

70.

Which of these is the correct format for an "if/else" statement?

a)
b)
c)
d)
71.

Which of these is the proper format of an "else/if" chain statement?

a)
b)
c)
72.

What will the output of the following code segment be?


int a = 1, b = 2;

System.out.println(++a+”,”+b++);

a)

1,2

b)

2,2

c)

2,3

d)

3,3

73.

What is each repetition of a loop known as?

a)

cycle

b)

revlolution

c)

orbit

d)

iteration

74.

This statement causes a loop to terminate early.

a)

a. continue

b)

b. break

c)

c. switch

d)

d. none of the above

75.

Base on the code block below what will be the output to the console?


int number =11;


if(number >=10) {

System.out.print("New York");

}

else if(number<=5){

System.out.print("California");

}

else System.out.print("Texas");

a)

New York

b)

California

c)

Texas

d)

Nothing

76.

Base on the code block below what will print to the console?


var n =5;


for(int i=1;i<=n;i++){

System.out.println(i);

}

a)

1

2

3

4

5

b)

5

4

3

2

1

c)

1

2

3

4

d)

4

3

2

1

77.

Which one of these control flow statements does not loop

a)

switch

b)

while

c)

do-while

d)

for

78.

Base on the code block below what will be the output to the console?


int number =2;


if(number >=10) {

System.out.print("New York");

}

else if(number<=5){

System.out.print("California");

}

else System.out.print("Texas");

a)

New York

b)

California

c)

Texas

d)

Nothing

79.

Base on the code block below what will be the output to the console?


int number =8;


if(number >=10) {

System.out.print("New York");

}

else if(number<=5){

System.out.print("California");

}

else System.out.print("Texas");

a)

New York

b)

California

c)

Texas

d)

Nothing

80.

Base on the code block below what will print to the console?


var n =5;


for(int i=1;i<n;i++){

System.out.println(i);

}

a)

1

2

3

4

5

b)

5

4

3

2

1

c)

1

2

3

4

d)

4

3

2

1

81.

Base on the code block below what will be the output to the console?


int n=5;

int c=0;

while(n>=0&&c<=5) {

System.out.println(c+":"+n);

c++;

n--;

}

a)

1:1

2:2

3:3

4:4

5:5

b)

1:4

2:3

3:2

4:1

c)

0:5

1:4

2:3

3:2

4:1

5:0

d)

0:0

1:1

2:2

3:3

4:4

5:5

82.

How do you write a Greater than or equal to Relational operator

a)

<=

b)

=<

c)

>=

d)

=>

83.

How do you write a Not equal to Relational operator

a)

!=

b)

=!

c)

!==

d)

!!=

84.

How do you write am Equal to Relational operator

a)

===

b)

EQUAL

c)

=

d)

==

85.

What is the output of the below Java program?


int hours = 10;

String message=null;


switch(hours)

{

case 10: message ="TEN"; break;

case 11: message ="TEN AGAIN"; break;

default: message ="TEN AS USUAL";

}

System.out.println(message);

a)

TEN

b)

TEN AGAIN

c)

TEN AS USUAL

86.

What is the output of the below Java program?


int hours = 10;

String message=null;


switch(hours)

{

case 10: message ="TEN";

case 11: message ="TEN AGAIN"; break;

default: message ="TEN AS USUAL";

}

System.out.println(message);

a)

TEN

b)

TEN AGAIN

c)

TEN AS USUAL

87.

What is the output of the below Java program?


int hours = 10;

String message=null;


switch(hours)

{

case 10: message ="TEN";

case 11: message ="TEN AGAIN";

default: message ="TEN AS USUAL";

}

System.out.println(message);

a)

TEN

b)

TEN AGAIN

c)

TEN AS USUAL

88.

What will be outputted from this code?

a)

50

b)

60

c)

40

d)

85

89.

Which of the following use the correct syntax for a traditional for loop?

a)

for(x=0; x<10; x++) {System.out.println(x);}

b)

for(int x=0; x<10; x++) {System.out.println(x);}

c)

for(x=0, x<10, x++){System.out.println(x);}

d)

for(int x=0: x<10: x++){System.out.println(x);}

90.

Which of the following use the correct syntax for an enhanced for loop?

a)

for(int x=0; x<10; x++) {System.out.println(x);}

b)

for(int x : numbers) {System.out.println(x);}

c)

for(x=0, x<10, x++){System.out.println(x);}

d)

for(int x=0: int numbers=10){System.out.println(x);}

91.

Base on the code block below what will print to the console?


String Days[]= {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};


for(int i=6;i>=0;i--) {

System.out.println(Days[i] );

}

a)

Sunday Monday Tuesday Wednesday Thursday Friday Saturday

b)

Sunday

Monday

Tuesday

Wednesday

Thursday

Friday

Saturday

c)

"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"

d)

Saturday

Friday

Thursday

Wednesday

Tuesday

Monday

Sunday

92.

What would be outputted if you inputted "a"?

a)

1

b)

2

c)

3

93.

What would be outputted if you inputted "B"?

a)

1

b)

2

c)

3

94.

What would be outputted if you inputted "3"?

a)

1

b)

2

c)

3

d)

nothing, this will trigger an error.

95.

What would print if the following code was inserted on line 9.


System.out.print(num1);

a)

5

b)

10

c)

15

d)

nothing, this will trigger a error.

96.

What would print if the following code was inserted on line 13.


System.out.print(num3);

a)

5

b)

10

c)

15

d)

nothing, this will trigger an error.

97.

Which three statements are jump statements?

a)

case

b)

default

c)

continue

d)

break

e)

return

98.

Which two statements are not jump statements?

a)

case

b)

default

c)

continue

d)

break

e)

return

99.

What will be outputted from this code?

a)

5

b)

6

c)

nothing, this will trigger an error.

d)

10

100.

What is the output of this program?

a)

0

2

6

8

10

b)

0

2

4

c)

2

4

d)

Nothing, this program will not compile.