wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Quiz

Total questions: 40

Worksheet time: 22mins

Name
Class
Date
1.

Array is

a)

immutable

b)

mutable

c)

always contant

2.

Which of the following declares an array of int named img?

a)

int img;

b)

int[] img;

c)

new int img[];

d)

int img = int[];

3.

What are the legal indexes for the array ar, given the following declaration:


int[] ar = {2, 4, 6, 8 };

a)

0, 1, 2, 3

b)

1, 2, 3, 4

c)

2, 4, 6, 8

d)

0, 2, 4. 6

4.

What is the output of the following code fragment:


int[] ar = {2, 4, 6, 8 };

System.out.println( ar[0] + " " + ar[1] );

a)

2 6

b)

8

c)

2 4

d)

6 8

5.

What is the output of the following code fragment:


int[] ar = {2, 4, 6, 8 };


ar[0] = 23;

ar[3] = ar[1];


System.out.println( ar[0] + " " + ar[3] );

a)

23 2

b)

2 8

c)

31

d)

23 4

6.

What is the output of the following code fragment:


int[] y = new int[5];


y[0] = 34;

y[1] = 88;


System.out.println( y[0] + " " + y[1] + " " + y[5] );

a)

34 88 0

b)

34 88 88

c)

The program is defective and will not compile

d)

0 34 88

7.

What is the output of the following code fragment:


int[] z = new int[9];

z[0] = 7;

z[1] = 3;

z[2] = 4;

System.out.println( z[0] + z[1] + " " + z[5] );

a)

10 0

b)

7 3 0

c)

The program is defective and will not compile.

d)

7 3 4

8.

What is the output of the following code fragment:


int[] zip = new int[5];

zip[0] = 7;

zip[1] = 3;

zip[2] = 4;

zip[3] = 1;

zip[4] = 9;

System.out.println( zip[ 2 + 1 ] );

a)

4 3

b)

3

c)

4

d)

1

9.

What is the output of the following code fragment:


int[] zip = new int[5];

zip[0] = 7;

zip[1] = 3;

zip[2] = 4;

zip[3] = 1;

zip[4] = 9;

int j = 3;

System.out.println( zip[ j-1 ] );

a)

7

b)

3

c)

4

d)

1

10.

How many objects are present after the following code fragment has executed?


double[] ann = new double[ 7 ];

double[] bob;


bob = ann;

a)

2

b)

7

c)

14

d)

1

11.

Which of the following is a comment in the Java language?

a)

/ comment /

b)

/* comment */

c)

/ comment */

d)

<-- comment -->

12.

Which of the following is the correct main() method signature in Java?

a)

public static void main (String [] args)

b)

public static void Main (String [] args)

c)

public static void main (string [] args)

d)

public static void main (String args)

13.

Which of the following is a valid variable declaration in Java?

a)

int 1myInteger = 0;

b)

int int = 0;

c)

Int my Integer = 0;

d)

int myInteger = 0;

14.

Which Java data type is used to store 64-bit floating-point numbers?

a)

int

b)

long

c)

float

d)

double

15.

Which of the following is a reserved word in Java?

a)

export

b)

import

c)

input

d)

output

16.

What is a class in object-oriented programming?

a)

A class is a template with data and methods

b)

A class is a package

c)

A class is the declared object in memory

d)

A class is a reference object in memory

17.

What is the output of this program?

a)

0

b)

1

c)

StackOverFlow

d)

NullPointerException

18.

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

a)

JVM

b)

JDK

c)

JIT

d)

JRE

19.

Which of the following is not a rule that must be followed when naming identifiers?

a)

After the first character, you may use the letters a-z, A-Z, an underscore, a dollar sign, or digits 0-9.

b)

Identifiers can contain spaces.

c)

Uppercase and lowercase characters are distinct.

d)

The first character must be one of the letters a-z, A-Z, an underscore or a dollar sign.

20.

Java was originally named:

a)

Coffee

b)

Oak

c)

New C++

d)

Duke

21.
Which type of data is used to enter the value (True)?
a)
Float
b)
Boolean
c)
Check box
d)
ٍString
22.
We are use Scanner Library in java for...
a)
Give permissions to the user
b)
Correcting software errors
c)
Allows user to read data
d)
Allows the user to enter data
23.

Which of these variable types is not numeric?

a)

boolean

b)

char

c)

double

d)

int

24.

How many times will this loop run?

int x = 0;

while(x < 4) {

x = x + 1;

}

a)

0

b)

3

c)

4

d)

5

25.

How many times will this loop run?

int x = 4;

while(x < 4) {

x = x + 1;

}

a)

0

b)

3

c)

4

d)

5

26.

Pick the correct code for taking String input from the keyboard.

Scanner input = new Scanner(System.in);

a)

String str = input.nextLine();

b)

String str = input.nextInt();

c)

int number = input.nextInt();

d)

int number = input.nextLine();

27.
What are the legal indexes for the array ar, given the following declaration:
int[] ar = {2, 4, 6, 8 }
a)
 0, 1, 2, 3
b)
 1, 2, 3, 4
c)
2, 4, 6, 8
d)
 0, 2, 4, 6
28.

For the array:

int stats[3];

What is the range of the index?

a)

0 to 3

b)

0 to 2

c)

1 to 3

d)

0 to 4

29.

int [] nums = {2, 3, 5, 8, 9, 11};

How would you access the fourth element in nums?

a)

nums[4]

b)

nums[3]

c)

nums(4)

d)

nums(3)

30.
Which of the following statements outputs the fifth value in the quizzes array?
a)
System.out.println(quizzes[5]);
b)
quizzes[4];
c)
quizzes[5];
d)
System.out.println(quizzes[4]);
31.
Index values of an array range from ______________.
a)
0 to length – 1
b)
1 to length
c)
0 to length
d)
0 to length + 1
32.
Create an array of 12 strings called months.
a)
String[] months = new String[12];
b)
String months = new String[12];
c)
String[] months = new String[];
d)
String[12] months = new String[];
33.
What is the correct way to create an array with these three numbers: 12  8  15
a)
int[] = {12, 8, 15};
b)
int[] nums = {12  8  15};
c)
int[] nums = {12, 8, 15}
d)
int[] nums = {12, 8, 15};
34.

Who invented Java?

a)

James Gosling

b)

Sergey Brin

c)

Steve Jobs

d)

Bill Gates

e)

Tim Berners-Lee

35.

Which of the following is not a Java features?

a)

Dynamic

b)

Architecture Neutral

c)

Use of pointers

d)

Object-oriented

36.

Which of the following is a valid declaration of a char?

a)

char ch = '\utea';

b)

char ca = 'tea';

c)

char cr = \u0223;

d)

char cc = '\itea';

37.

When does method overloading is determined?

a)

At run time

b)

At compile time

c)

At coding time

d)

At execution time

38.

Which concept of Java is a way of converting real world objects in terms of class?

a)

Polymorphism

b)

Encapsulation

c)

Abstraction

d)

Inheritance

39.

Which concept of Java is achieved by combining methods and attribute into a class?

a)

Encapsulation

b)

Inheritance

c)

Polymorphism

d)

Abstraction

40.

Syntax to compile java program from command prompt

a)

java Filename.java

b)

javac Filename

c)

javac Filename.java

d)

java Filename