wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

BB_QUIZ

Total questions: 93

Worksheet time: 1hrs 6mins

Name
Class
Date
1.

Which of the following is FALSE about arrays on Java

a)

A java array is always an object

b)

Length of array can be changed after creation of array

c)

Arrays in Java are always allocated on heap

2.

Predict the output?

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]);

}

}

}

a)

10 20 30 40 50

b)

Compiler Error

c)

10 20 30 40

3.

class Test {

public static void main(String args[]) {

int arr[2];

System.out.println(arr[0]);

System.out.println(arr[1]);

}

}

a)

0

0

b)

garbage value

garbage value

c)

Compiler Error

d)

Exception

4.

public class Main {

public static void main(String args[]) {

int arr[][] = new int[4][];

arr[0] = new int[1];

arr[1] = new int[2];

arr[2] = new int[3];

arr[3] = new int[4];

int i, j, k = 0;

for (i = 0; i < 4; i++) {

for (j = 0; j < i + 1; j++) {

arr[i][j] = k;

k++;

}

}

for (i = 0; i < 4; i++) {

for (j = 0; j < i + 1; j++) {

System.out.print(" " + arr[i][j]);

k++;

}

System.out.println();

}

}

}

a)

Compiler Error

b)

0

1 2

3 4 5

6 7 8 9

c)

0

0 0

0 0 0

0 0 0 0

d)

9

7 8

4 5 6

0 1 2 3

5.

Which of the following is the correct way of importing an entire package ‘pkg’?

a)

import pkg.

b)

Import pkg.

c)

import pkg.*

d)

Import pkg.*

6.

Which of the following is/are advantages of packages?

a)

Packages avoid name clashes

b)

Classes, even though they are visible outside their package, can have fields visible to packages only

c)

We can have hidden classes that are used by the packages, but not visible outside.

d)

All of the above

7.

Which of these is a mechanism for naming and visibility control of a class and its content?

a)

Object

b)

Packages

c)

Interfaces

d)

None of the above

8.

Which of this access specifies can be used for a class so that its members can be accessed by a different class in the same package?

a)

Public

b)

Protected

c)

No Modifier

d)

All of the mentioned

9.

Arrays in Java are dynamically allocated using the . . . . operator.

a)

create

b)

dynamic

c)

arrayList

d)

new

10.

Which of the following statements correctly declares a two-dimensional integer array?

a)

int Matrix[ ] = new int[5,4];

b)

int Matrix[ ]; Matrix = new int[5,4];

c)

int Matrix[ ][ ] = new int[5][4];

d)

int Matrix[ ][ ] = int[5][4];

11.

What is the ArrayList nums if it is initially [5, 3, 1] and the following code is executed?

nums.add(6);

nums.add(0, 4);

nums.remove(1);

a)

[4, 3, 1, 6]

b)

[5, 3, 1, 6]

c)

[4, 3, 6]

d)

[4, 5, 3, 6]

12.

What index value is used to locate the last element in the nums ArrayList?

a)

nums.size()-1

b)

nums.length()-1

c)

nums.size()

d)

nums.length

13.

Which statement below is the correct way to retrieve the first element in the nums ArrayList?

a)

nums.get(0)

b)

nums[0]

c)

nums(0)

d)

nums[1]

14.

Given the ArrayList nums with the values [3, 7, 6, 0], what code below is the proper way to change the 7 to be a 5?

a)

nums.set(1, 5)

b)

nums.set(7, 5)

c)

nums.set(5, 2)

d)

nums[2] = 5

15.

Given the nums ArrayList with the values [5, 4, 3, 2], what statement below removes the value 3 from the list?

a)

nums.remove(2)

b)

nums.remove(3)

c)

nums.get(2) = null

d)

nums.remove(1)

16.

Given the nums ArrayList with the values [1, 2, 3, 4, 6], what code below can be used to add a 5 between the 4 and 6 in the list?

a)

nums.add(4, 5)

b)

nums.add(5, 4)

c)

nums.add(5, 5)

d)

nums.add(5)

17.

What will print when the following code executes?

ArrayList<Integer> list = new ArrayList<Integer>();

list.add(7);

list.add(8);

list.add(9);

System.out.println(list.remove(0));

a)

7

b)

8

c)

9

d)

0

18.

What is printed?

ArrayList<Integer> alist;

alist = new ArrayList<Integer>();

alist.add(1);

alist.add(2);

alist.remove(1);

alist.add(1, 3);

alist.set(1, 4);

alist.add(5);

System.out.println(alist);

a)

[1, 4, 5]

b)

[1, 4, 3, 5]

c)

[2, 4, 5]

d)

[2, 4, 3, 5]

19.

What is printed?

ArrayList<Double> vals;

vals = new ArrayList<Double>();

vals.add(3.5);

vals.add(4.5);

vals.add(5.5);

vals.set(0, 2.5) ;

System.out.println(vals);

a)

[2.5, 4.5, 5.5, 3.5]

b)

[2.5, 3.5, 4.5, 5.5]

c)

[3.5, 4.5, 5.5, 2.5]

d)

[2.5, 4.5, 5.5]

20.

Which statement is the correct declaration and initialization of an ArrayList of String values?

a)

ArrayList<String> name;

name = new ArrayList<String>();

b)

ArrayList name;

name = new ArrayList<String>();

c)

ArrayList<String> name;

name = ArrayList<String>();

d)

String<ArrayList> name;

name = new String<ArrayList>();

21.

What is printed after executing code on the nums ArrayList with the values [3, 2, 4, 5, 7]?

a)

7

b)

5

c)

6

d)

memory full because too many values added

22.

What is the output of the following snippet?

a)

Hello Java

b)

Java Hello

c)

Hello

d)

String cannot be modified.

23.

Which of these is not a method of String class?

a)

charAt()

b)

getChars()

c)

getString()

d)

toCharArray()

24.

Strings in Java can be created using:

a)

String

b)

StringBuffer

c)

StringBuilder

d)

All of these

25.

To which package does the string class belong to?

a)

java.io

b)

java.util

c)

java.lang

d)

javax.conn

26.

Given this snippet , char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' };

String s = new String(chars, 2, 3);

What is the value of s?

a)

abc

b)

cde

c)

bc

d)

def

27.

byte ascii[] = {65, 66, 67};

String s1 = new String(ascii);

What is the value of s1?

a)

656657

b)

67

c)

ABC

d)

abc

28.

toString() belongs to ------- class?

a)

String

b)

Scanner

c)

Object

d)

Main

29.

char ch;

ch = "Hello".charAt(1);

What is stored in ch?

a)

Null

b)

He

c)

H

d)

e

30.

What is the output of the code?

a)

b)

c)

d)

None

31.

What is the result of the expression: "Hello".substring(1,4)

a)

Hel

b)

H

c)

ell

d)

llo

32.

Is String a primitive data type?

a)

Yes

b)

No

c)

Both

33.

Which method to use to find length of a string?

a)

length()

b)

size()

c)

long()

d)

count()

34.

Which data type gets returned from length() method in String class?

a)

double

b)

String

c)

int

d)

number

35.

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


What is the correct way to find the length of "txt" string?

a)

int len = length(txt);

b)

float len = txt.length();

c)

int len = txt.length();

d)

double len = length(txt);

36.

Which is correct method to convert String into uppercase

a)

changeUpperCase()

b)

convertUpperCase()

c)

toUpper()

d)

toUpperCase()

37.

String txt = "Hello World";

System.out.println(txt.toUpperCase());


Choose the correct output.

a)

"HELLO WORLD"

b)

HELLO WORLD

c)

Hello world

d)

Hello World

38.

Choose correct purpose of indexOf() in String class.

a)

returns the index (the position) of the last occurrence of a specified text in a string

b)

returns the character of the first occurrence of a specified character in a string

c)

returns the index (the position) of the first occurrence of a specified text in a string (excluding whitespace)

d)

returns the index (the position) of the first occurrence of a specified text in a string (including whitespace)

39.

String x = "10";

String y = "20";

String z = x + y;

System.out.println(z);

Guess the output.

a)

1020

b)

10 20

c)

30

d)

"10 20"

40.

What will be the output of below code?


String statement = "Outfit of the day";

System.out.println(statement.Substring(3,6));

a)

it of

b)

tfit

c)

fit

d)

FIT

41.

Choose most appropriate purpose of trim() method in String?

a)

to remove white spaces

b)

to get a substring of the string

c)

to cut String at desired index

d)

to remove extra white spaces from start and end of String

42.

What is the return type of equalsIgnorecase() method?

a)

int

b)

String

c)

char

d)

boolean

43.

What is the most appropriate way to check if two Strings are identical?

a)

string1 == string2

b)

string1.equals(string2)

c)

string1.same(string2)

d)

string1.equalsIgnorecase(string2)

44.

(Multiple answer question)

Pick all NON-primitive datatypes from below :

a)

String

b)

int

c)

primitive

d)

Array

45.

What is the correct relation between length and last-index of array?

a)

last-index is always 1 more than the length

b)

last-index is 1 less than the length (but not always)

c)

last-index is always 1 less than the length

d)

last-index is always equal to the length

46.
String word = "A few good men";
What is word.length()?
a)
14
b)
15
c)
13
47.
String word = "A few good men";
What is word.charAt(7) ?
a)
o
b)
g
c)
7
d)
good men
48.
String word = "A few good men";
What is word.startsWith("A few") ?
a)
true
b)
false
49.
String word = "A few good men";
What is word.endsWith("man") ?
a)
true
b)
false
50.

String word = "A few good men";

What is word.indexOf(“f”)?

a)

2

b)

0

c)

1

d)

3

51.
String word = "A few good men";
What is word.substring(3, 7) ?
a)
ew g
b)
ew go
c)
few
d)
few[space]
52.

A programming mechanism that binds together code and the data it manipulates, and that keeps both safe from outside interference and misuse.

a)

Polymorphism

b)

Inheritance

c)

Encapsulation

d)

Object-Oriented Programming

53.

Allows us to consider complex ideas while ignoring irrelevant detail that would confuse us.

a)

Abstraction

b)

Encapsulation

c)

Generalisation

d)

Polymorphism

e)

Object

54.

As a Vehicle user, we know how to use it but its internal working are not known. In OOPs Concept, this principle is known as

a)

Encapsulation

b)

Inheritance

c)

Abstraction

d)

Polymorphism

55.

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

a)

JRE

b)

JIT

c)

JDK

d)

JVM

56.

Which of the following language was developed as the first purely object programming language?

a)
  1. SmallTalk

b)
  1. C++

c)
  1. Kotlin

d)
  1. Java

57.

Which two features of object-oriented programming are the same?

a)

Abstraction and Polymorphism

b)

Inheritance and Encapsulation

c)

Encapsulation and Polymorphism

d)
  1. Encapsulation and Abstraction

58.

Output?

a)

The vehicle moves

b)

The car moves

c)

The code does not compile 

d)

None of the above 

59.

Output?

a)

parent from parent

b)

child from child

c)

parent from child

d)

child from parent

60.

Java uses both compiler and interpreter

a)

true

b)

false

61.

Which of the following concepts of OOPs means exposing only necessary information and hiding the background details?

a)

Encapsulation

b)

Abstraction

c)

Data hiding

d)

Data binding

62.

What is a HashMap in Java used for?

a)

To store data in key-value pairs.

b)

To perform mathematical calculations.

c)

To manage threads.

d)

To handle exceptions.

63.

Which is the superclass of Error class in Java?

a)

Throwable

b)

Exception

c)

Runtine Exception

d)

None of the above

64.
a)

23

b)

Hi this is test 23

c)

22

d)

compiler Error

65.
a)

9.797

b)

5.830

c)

7.211

d)

Compilation Error

66.

Choose the correct output...

a)

12.4 12.4

b)

12 12

c)

14 12.4

d)

12.4 12

67.

Find the output of the given code

a)

Hi this is test

6

b)

2

c)

compiler error

d)

Hi this is test

2

68.

Find the output of the given code

a)

65621

b)

390689

c)

Compiler error

d)

190332

69.

What is the output of the given code..?

a)

Garbage value

b)

100

c)

200

d)

Compiler error

70.

What is the output of the below code..?

a)

true true

b)

false false

c)

true false

d)

false true

71.

Find the output of the given code....

a)

0

b)

1

c)

2

d)

0 1 2

72.

Find the correct Java statement(s)...

a)

1 and 2

b)

2 and 3

c)

3 and 4

d)

All statements are correct

73.
a)

a = 3 s = Blue

b)

a = 5 s = Yellow

c)

a = 3 s = Yellow

d)

a = 5 s = Blue

74.

What is the output of the code given...?

a)

0 1 2

b)

0 2 4

c)

0 0 4

d)

0 1 4

75.
a)

No divide by Zero Error….

b)

Its Working

c)

Compilation Error

d)

Its Working No divide by Zero Error…

76.
a)

1 2

b)

2 1

c)

Runtime Error

d)

Compilation Error

77.
a)

6 6

b)

6.4 6.4

c)

6.4 6

d)

4 6.4

78.

Find the correct output of the given code....

a)

===============

b)

#################

&&&&&&&&&

c)

&&&&&&&&&&&

d)

===============

#################

&&&&&&&&&&

79.

What is the advantage of Exception Handling

a)

To avoid abnormal termination of a program

b)

To find out errors

c)

To Debug program

d)

None of these

80.

What is the parent class of All Exceptions in JAVA

a)

Throw

b)

Exception

c)

Error

d)

Bug

81.

Which of the following key word is optional in Exception handling program

a)

try

b)

catch

c)

finally

d)

throw

82.

What is the purpose of 'throw' keyword

a)

To Raise an exception explicityly

b)

To Raise an exception implicityly

c)

To create user defined exceptions

d)

To create custom exceptions

83.

Exception classes belongs to following package

a)

import java.io.*

b)

import java.lang.*

c)

import java.util.*

d)

import java.lang.Exception.*

84.

How to create our own exception

a)

using 'throw' keyword

b)

using 'throws' keyword

c)

using 'finally' keyword

d)

using 'throwable' keyword

85.

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

86.

What will it print?

a)

1

2

3

30

b)

1

2

30

c)

1

2

3

11

d)

1

2

11

87.

What will this program print?

a)

Nothing, there is an error

b)

There was a ValueError, please try again

c)

num1num2num1^{num2}

d)

num2num1num2^{num1}

88.

public class Foo

{

public static void main(String[] args)

{

try

{

return;

}

finally

{

System.out.println( "Finally" );

}

}

}

a)

Finally

b)

Compilation fails.

c)

The code runs with no output

d)

An exception is thrown at runtime.

89.

class Main {

public static void main(String args[]) {

try {

throw 10;

}

catch(int e) {

System.out.println("Got the Exception " + e);

}

}

}

a)

Got the Exception 10

b)

Got the Exception 0

c)

Compiler Error

d)

none

90.

Choose the CORRECT mechanism of exception handling.

a)

try block --> catch block --> finally block

b)

try block --> catch block --> throws exception

c)

try block --> catch block --> finally --> throws exception

d)

try block --> throws exception--> catch block --> finally block

91.

What is the output?

a)

The code will lead to a compilation error as declaration of the display method has been provided in two interface.

b)

The code will lead to a compilation error due to public modifier while declaring the display method.

c)

The code will compile and execute successfully showing the output Welcome to Examveda.

d)

The code will lead to a compilation error as the display method is not declared as abstract.

e)

None of these

92.

Exception is a class/interface/abstract class/other?

a)

Class

b)

Interface

c)

Abstract class

d)

Other

93.

1. class main_class

2. {

3. public static void main(String args[])

4. {

5. int y = 10;

6. if (y == 10)

7. {

8. int y = 11;

9. System.out.println(y);

10. }

11. }

12. }

a)

Compilation error

b)

10

c)

11

d)

Run time error