wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

1510 Chapter 2

Total questions: 41

Worksheet time: 26mins

Name
Class
Date
1.

If x is a String, then x = new String("OH"); and x = "OH"; will accomplish the same thing.

a)

True

b)

False

2.

If x is the String "Hi There", then x.toUpperCase().toLowerCase(); will return the original value of x.

a)

True

b)

False

3.

If String name = "Bruce"; then the instruction name.length(); will return 5.

a)

True

b)

False

4.

If String a = "ABCD" and String b = "abcd" then a.equals(b); returns false but a.equalsIgnoreCase(b); returns true.

a)

True

b)

False

5.

Unlike the String class where you must pass a message to an object (instance) of the class, as in x.length(), in order to use either the Scanner or Math classes, you pass messages directly to the class name, as in Math.abs() or scan.nextInt().

a)

True

b)

False

6.

In order to generate a random number, you must use Math.random().

a)

True

b)

False

7.

A double is wider than a float and a float is wider than an int.

a)

True

b)

False

8.

A variable of type boolean will store either a 0 or a 1.

a)

True

b)

False

9.

In Java, 'a' and 'A' are considered to be different values.

a)

True

b)

False

10.

You cannot cast a String to be a char and you cannot cast a String which stores a number to be an int, float, or double.

a)

True

b)

False

11.

The values of (double) 5/2 and (double) (5/2) are identical.

a)

True

b)

False

12.

In Java, there are three main ways that data conversion may occur: by assignment, by promotion, and by casting.

a)

True

b)

False

13.

The following statement will output ______ lines of text. System.out.println("1 big bad wolf\t8 the 3 little pigs\n4 dinner\\2night");

a)

1

b)

2

c)

3

d)

4

e)

5

14.

If you want to output the text "hi there" including the quote marks, which of the following would you use?

4 lines
15.

println is a

a)

method

b)

reserved word

c)

variable

d)

class

e)

String

16.

A Java variable is the name of a

a)

numeric typed data value stored in memory

b)

immutable untyped data value stored in memory

c)

mutable typed data value stored in memory

d)

mutable untyped data value stored in memory

e)

mutable data value or class stored in memory

17.

Of the following types, which one cannot store a numeric value?

a)

int

b)

double

c)

float

d)

char

e)

All of these can store numeric values

18.

What is the value of z after the following assignment statement is executed? float z = 5/10;

a)

0.0

b)

0.5

c)

5.0

d)

0.05

e)

None of these; a run-time error will occur because z is a float and 5/10 is an int.

19.

Which of the following situations would require a cast?

a)

using charAt to take an element of a String and store it in a char

b)

storing an int in a float

c)

storing a float in a double

d)

storing a float in an int

e)

All of these require casts

20.

If i is an int and f is a float, which of the following statements is not a legal assignment statement?

a)

f = i;

b)

i = f;

c)

f = (float)i;

d)

i = (int)f;

e)

All of these are legal assignment statements

21.

Which of the following is true regarding the mod operator, %?

a)

It can only be performed on int values and its result is a double.

b)

It can only be performed on int values and its result is an int.

c)

It can only be performed on float or double values and its result is an int.

d)

It can only be performed on float or double values and its result is a double.

e)

It can be performed on any numeric values and its result is always numeric.

22.

int x = 20, int y = 10, and int z = 5. What is the result of: x / y / z?

a)

0

b)

12

c)

0.4

d)

this would cause a syntax error

e)

this would cause a run-time error because it is a division by zero

23.

Assume that x and y are ints equal to 10 and 5 respectively. What is the output of the following statement? System.out.println(x + y);

a)

15

b)

105

c)

10 5

d)

x + y

e)

this would cause an error since neither x nor y is a String

24.

Assume that x and y are ints equal to 10 and 5 respectively. What is the output of the following statement? System.out.println("" + x + y);

a)

15

b)

105

c)

10 5

d)

x + y

e)

this would cause an error since neither x nor y is a String

25.

If you want to store the value "Darth Vader" in the String variable name, which of the following statements could you use?

a)

String name = "Darth Vader";

b)

String name = new String("Darth Vader");

c)

String name = "Darth" + " " + "Vader";

d)

String name = new String("Darth" + " " + "Vader");

e)

Any of these would work

26.

Given three String variables, a, b, and c, which of the following statements could you use to achieve the same thing as: c = a + b;

a)

c = a.length() + b.length();

b)

c = (int)a + (int)b;

c)

c = a.concat(b);

d)

c = b.concat(a);

e)

c = a.plus(b);

27.

If the String major = "Computer Science", what is returned by major.charAt(1)?

a)

'C'

b)

'o'

c)

'm'

d)

"C"

e)

"Computer"

28.

Which of the following would return the last character of the String x?

a)

x.charAt(x.length())

b)

x.charAt(length(x));

c)

x.charAt(length(x) - 1);

d)

x.charAt(x.length() - 1)

29.

Given String name = "Arleen Crabtree". What will the following instruction return? name.toUpperCase().replace('R', 'Z');

a)

ARLEEN CRABTREE

b)

AZLEEN CZABTREE

c)

Azleen Czabtree

d)

Arleen Czabtree

e)

Ar2leen Cr2abtree

30.

Which library package would you import to use NumberFormat and DecimalFormat?

a)

java.beans

b)

java.io

c)

java.lang

d)

java.text

e)

java.util

31.

Which library package would you import to use the class Random?

a)

java.beans

b)

java.io

c)

java.lang

d)

java.text

e)

java.util

32.

The Random class has a method, nextFloat(), which returns a random float value between

a)

-1 and +1

b)

0 and 1

c)

0 and 99

d)

1 and 100

33.

If you want to output a double so that at least one digit appears to the left side of the decimal point and exactly one digit appears to the right side, which pattern would you give a DecimalFormat variable when you instantiate it?

a)

0.0

b)

#.0

c)

0.#

d)

#.#

e)

#.##

34.

Given the double likelihood = 0.013885 and given DecimalFormat df = new DecimalFormat("0.00##");, what would be the output if you execute the following: System.out.println(df.format(likelihood));

a)

0.013885

b)

0.0139

c)

0.0145

d)

0.0138

e)

0.014

35.

Using getCurrencyInstance() formats a variable, automatically inserting

a)

a decimal point for cents

b)

a dollar sign

c)

a percent sign

d)

all three of these

e)

a decimal point for cents and a dollar sign but not a percent sign

36.

What will be the value of z after the following statement is executed? int z = 50 / 10.00;

a)

5

b)

5.0

c)

50

d)

10

e)

None of these; a run-time error will occur because z is an int and 50/10.00 is not an int.

37.

Which of these will never return NaN?

a)

Math.sqrt((int)x)

b)

Math.sqrt(Math.abs(x))

c)

Math.abs(Math.sqrt(x))

d)

Math.sqrt(-x)

38.

Given double x = 0.362, which of the following prints it as 36% using
NumberFormat nf = NumberFormat.getPercentInstance();

a)

System.out.println(nf)

b)

System.out.println(nf.format(x))

c)

System.out.println(nf.x)

d)

System.out.println(format(x))

39.

In order to create a constant, which of the following Java reserved words is used?

a)

private

b)

static

c)

int

d)

final

e)

class

40.

Given int a = 5, b = 7, c = 12, what is the value of z?

int z = (a * b - c) / a;

a)

0

b)

4

c)

5

d)

-5

e)

23

41.

written answer question (not for marks)

What is a wrapper class? Why are wrapper classes useful?

4 lines