wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Fundamentals

Total questions: 47

Worksheet time: 3hrs 49mins

Name
Class
Date
1.

When programming, you have to use Data Types. Which of the following refers to Strings, classes, and arrays?

a)

Primitive

b)

Reference

c)

Arrays

d)

Boolean

2.
When we want to print in Java, we use the code ......
a)
system.out.print();
b)
System.out.println();
c)
System.Out.Printf();
d)
System.out.show();
3.

The main() method is not mandatory for a Java program

a)

True

b)

False

4.

Which one is a Java valid variable name?

a)

Sum Grades

b)

static

c)

public

d)

FindAverage

5.

which statement is correct?

a)

int Var

b)

boolean 15;

c)

double var123;

d)

int min=0

6.

The output of the following code is:

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

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

System.out.println(array[2]);

}

a)

2

4

6

8

10

b)

2,4,6,8,10

c)

6

6

6

6

6

d)

Nothing

7.

3 + 6 / 2 * 3 - 1 + 2 =

a)

14.5

b)

13

c)

5

d)

13.0

8.

num= 2;

x=num++;

++num;

After performing the above statements, the values of x and num are

a)

x= 2

num= 3

b)

x = 3

num = 4

c)

x= 2

num = 4

d)

x= 3

num= 3

9.

nextInt() is used to read user's numerical input

a)

True

b)

False

10.

Which of the following pairs of declarations will cause an error message?

I.  double x = 14.7;      int y = x;

II.  double x = 14.7;       int y = (int) x;

III.  int x = 14;        double y = x;

a)

I only

b)

II only

c)

III only

d)

I and III only

11.

What output will be produced by:


System.out.print("\\* This is not\n a comment *\\"):

a)

\* This is not a comment *\

b)

* This is not

a comment *

c)

\\* This is not

a comment *\\

d)

\* This is not

a comment *\

12.

Refer to the following code fragment:


double answer = 13 / 5;

System.out.println("13 / 5 = " + answer);


The output is:

13 / 5 = 2.0


The programmer intends the output to be:

13 / 5 = 2.6


Which of the following replacements for the first line of code will NOT fix the problem?

a)

double answer = (double) 13 / 5;

b)

double answer = 13 / (double) 5;

c)

double answer = 13.0 / 5;

d)

double answer = (double) (13 / 5);

13.

What value is stored in result if:


int result = 13 - 3 * 6 / 4 % 3;

a)

11

b)

13

c)

0

d)

12

14.

Which of the following will evaluate to true only if boolean expressions

A, B, and C are all false?

a)

!A && !(B && !C)

b)

!(A || B || C)

c)

!(A && B && C)

d)

!A || !(B || !C)

15.

Assume that a and b are integers. The boolean expression

!( a <= b) && (a * b > 0)

will always evaluate to true given that

a)

a > b

b)

a < b

c)

a > b and b > 0

d)

a > b and b < 0

16.

Name the data type: "true"

a)

boolean

b)

char

c)

String

d)

double

17.

Name the data type: 1.0

a)

double

b)

int

c)

char

d)

boolean

18.

Name the data type: '3'

a)

int

b)

char

c)

String

d)

None of the above

19.

What data type would you use for storing the number of students in a class?

a)

boolean

b)

String

c)

double

d)

int

20.

What data type would you use for storing the average test score in a class?

a)

double

b)

String

c)

int

d)

char

21.

How would you declare a variable storing a person's name?

a)

string name = "Jeff";

b)

name String = "Jeff";

c)

String name = Jeff;

d)

String name = "Jeff";

22.

How would you declare a variable storing the tax rate?

a)

int taxRate = 5.1;

b)

taxRate = "5.1";

c)

double taxRate = 5.1;

d)

double taxRate = "5.1";

23.

Which expression evaluates to true?

a)

false == false

b)

true != true

c)

4 < 4

d)

true == true != true

24.

Which expression evaluates to false?

a)

15 % 3 < 2

b)

3 * 3 % 2 <= 0

c)

false == false

d)

false == false == false == false

25.

Which declaration will throw an error?

a)

double num = 8;

b)

int averageGrade = 89.7;

c)

boolean done = false;

d)

String done = "true";

26.

What prints out "I love Java" successfully?

a)

System.out.println(I love Java);

b)

Systemoutprintln("I love Java);

c)

System.out.println("I love" + " Java");

d)

System.out.println("I love Java")

27.

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};

28.

What will be the output of the program?

a)

true

b)

false

c)

Compilation fails

d)

An exception is thrown at runtime

29.

What will be the output of the program?

a)

5 3

b)

6 3

c)

6 4

d)

5 2

30.

What will be the output of the program?

a)

5 3

b)

8 3

c)

8 2

d)

8 5

31.

What will be the output of the block of code in the main() method?

a)

j = -1

b)

j = 0

c)

j = 1

d)

Compilation fails.

32.

What will be the output of the the block of code in the main() method?

a)

i = 5 and j = 5

b)

i = 6 and j = 5

c)

i = 5 and j = 6

d)

i = 6 and j = 4

33.

What will be the output of the the block of code in the main() method?

a)

0 2 4

b)

0 2 4 5

c)

0 1 2 3 4

d)

Compilation fails.

34.

What will be the output of the the block of code in the main() method? (NOTE: This is a "trick" question!)

a)

a

b)

b

c)

c

d)

d

35.

Consider the following code segment:

Line 1: int a = 10;

Line 2: double b = 10.7;

Line 3: int d = a + b;


Line 3 will not compile in the code segment above. With which of the following statements could we replace this line so that is compiles?


I. int d = (int) a + b;

II. int d = (int) (a + b);

III. int d = a + (int) b;

a)

II

b)

III

c)

I or III

d)

II or III

36.

Consider the following code segment:


int var = 12;

var = var % 7;

var = var - 1;

System.out.println(var);


What is printed as a result of executing the code segment?

a)

1

b)

2

c)

4

d)

5

37.

Consider the following code segment:


int count = 5;

double multiplier = 2.5;

int answer = (int)(count * multiplier);

answer = (answer * count) % 10;

System.out.println(answer);


What is printed as a result of executing the code segment?

a)

0

b)

2.5

c)

6

d)

12.5

38.

Assume that x, y, and z have been declared as follows:


boolean x = true;

boolean y = false;

boolean z = true;


Which of the following expressions evaluates to true?

a)

x && y || z && y

b)

!(x && y) || z

c)

!(x || y) && z

d)

x && y || !z

39.

Function of the Scanner class to accept a float literal from the user is

a)

nextInt( )

b)

hasNext( )

c)

next( )

d)

nextFloat( )

40.

Absence of (a) ________ in switch..case leads to fallthrough

a)

switch

b)

case

c)

default

d)

break

41.

Find the output of,

int x =4;

while(x>5)

{

System.out.println(x);

x++;

}

a)

6

7

8... infinity

b)

1

2

3

4

c)

No Output

d)

4

5

42.

How do you insert COMMENTS in Java code?

a)

/* This is a comment

b)

# This is a comment

c)

// This is a comment  

d)

/* This is a comment */

43.

The value of a string variable can be surrounded by single quotes.

a)

True

b)

False

44.

Which method can be used to return a string in upper case letters?

a)

touppercase()

b)

toUpperCase()  

c)

upperCase()

d)

toUppercase()

45.

Which operator can be used to compare equality between two primitive values?

a)

><

b)

=

c)

==

d)

<>

46.

How do you start writing an if statement in Java?

a)

if (x > y)  

b)

if x > y then:

c)

if x > y:

d)

if (x>y):

47.

Which statement about choosing variable names is FALSE?

a)

Variable names can contain letters, digits, underscores, and dollar signs.

b)

Variable names are NOT case sensitive

c)

Reserved words (like Java keywords, such as int or String) cannot be used as variable names

d)

Variable names can also begin with $ and _