Ch4 Verified Test Bank Fundamental Data Types - Big Java Early Objects 5e Complete Test Bank by Cay S. Horstmann. DOCX document preview.
Package Title: Test Bank
Course Title: Big Java
Chapter Number: 4 Fundamental Data Types
Question type: Multiple Choice
1) Which of the following options declares a float variable?
a) Float age;
b) flt age;
c) float age;
d) age: float;
Title: Which declares a float variable?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
Section Reference 2: Special Topic 4.1
2) What is the result of the following code snippet?
public static void main(String[] args)
{
double circleRadius;
double circleVolume = 22 / 7 * circleRadius * circleRadius;
System.out.println(circleVolume);
}
a) 0
b) 3.14
c) 6.28
d) compile-time error
Title: What is result of snippet (with assignment)?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
3) What is wrong with the following code snippet?
public class Area
{
public static void main(String[] args)
{
int width = 10;
height = 20.00;
System.out.println("area = " + (width * height));
}
}
a) The code snippet uses an uninitialized variable.
b) The code snippet uses an undeclared variable.
c) The code snippet attempts to assign a decimal value to an integer variable.
d) The code snippet attempts to add a number to a string variable.
Title: What is wrong with snippet (with variable error)?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
4) What is wrong with the following code snippet?
int average;
average = 78A;
a) The average variable is never initialized.
b) The data type for the average variable is not specified.
c) The average variable is never assigned a value.
d) The average variable is assigned a non-numeric value.
Title: What is wrong with snippet (with value assigned to variable)?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
5) Which of the following guidelines will make code more explanatory for others?
a) Use more statements in source code.
b) Add comments to source code.
c) Avoid usage of complex calculations in source code.
d) Always enclose the statements in curly braces in source code.
Title: Which of the following guidelines will make code more explanatory for others?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
6) What will be the value stored in the variable x after the execution of the following code snippet?
int a = 10;
int b = 20;
int c = 2;
int x = b / a /*c*/;
a) 1
b) 2
c) 4
d) The code has a syntax error
Title: What is output of snippet (with arithmetic expression)?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
7) Which of the following statements with comments is(are) valid?
I. int cnt = 0; /* Set count to 0
II. int cnt = 0; /* Set count to 0 */
III. int cnt = 0; // Set count to 0
a) Only I is valid
b) I and II are valid
c) II and III are valid
d) Only III is valid
Title: Which statement has good comments?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
8) What is wrong with the following code?
int count = 2000 * 3000 * 4000;
a) Wrong data type
b) Variable is undefined
c) Integer overflow
d) Illegal expression
Title: What’s wrong with integer expression?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
Section Reference 2: Common Error 4.2
9) Which one of the following variables is assigned with valid literals?
a)
int salary = 0;
salary = 5000.50;
b)
int salary1 = 0;
salary1 = 1.2E6;
c)
double salary2 = 0;
salary2 = 2.96E-2;
d)
long salary3 = 0;
salary3 = 1E-6;
Title: Which variable is assigned with valid literals?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
10) What will be the value inside the variables a and b after the given set of assignments?
int a = 20;
int b = 10;
a = (a + b) / 2;
b = a;
a++;
a) a = 15, b = 16
b) a = 16, b = 16
c) a = 16, b = 15
d) a = 15, b = 15
Title: What is the value of a and b after these assignments?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
11) What is the value inside the value variable at the end of the given code snippet?
public static void main(String[] args)
{
int value = 3;
value = value – 2 * value;
value++;
}
a) –2
b) 0
c) 2
d) 4
Title: What is value after this snippet (with assignment and increment)?
Difficulty: Easy
Section Reference 1: 4.2 Arithmetic
12.) What are the values of num1 and num2 after this snippet executes?
double num1 = 4.20;
double num2 = num1 * 10 + 5.0;
a) num1 = 4.20 and num2 = 42.0
b) num1 = 4.20 and num2 = 47.0
c) num1 = 42.0 and num2 = 42.0
d) num1 = 42.0 and num2 = 47.0
Title: What are num1 and num2 after snippet (with assignment)?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
13) What is the result of the following expression?
double d = 2.5 + 4 * -1.5 - (2.5 + 4) * -1.5;
a) 24.375
b) 6.25
c) 12.375
d) 6
Title: What is the result of (arithmetic) expression?
Difficulty: Hard
Section Reference 1: 4.3 Input and Output
14) What is the output of the following code snippet?
public static void main(String[] args)
{
int value = 3;
value++;
System.out.println(value);
}
a) 2
b) 3
c) 4
d) No output due to syntax error
Title: What is output of snippet (with increment)?
Difficulty: Easy
Section Reference 1: 4.2 Arithmetic
15) What is the output of the following code snippet?
public static void main(String[] args)
{
int value = 25;
value = value * 2;
value--;
System.out.println(value);
}
a) 25
b) 50
c) 49
d) 26
Title: What is output of snippet (with decrement)?
Difficulty: Easy
Section Reference 1: 4.2 Arithmetic
16) Assuming that the user inputs a value of 25 for the price and 10 for the discount rate in the following code snippet, what is the output?
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the price: ");
double price = in.nextDouble();
System.out.print("Enter the discount rate: ");
double discount = in.nextDouble();
System.out.println("The new price is " +
price - price * (discount / 100.0));
}
a) The new price is 25
b) The new price is 15
c) The new price is 22.5
d) The new price is 20.0
Title: What is output of snippet (that calculates value based on user input)?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
17) Which of the following statements is correct about constants?
a) Constants are written using capital letters because the compiler ignores constants declared in small letters.
b) The data stored inside a constant can be changed using an assignment statement.
c) You can make a variable constant by using the final reserved word when declaring it.
d) Constant variables can only be changed through the Math library.
Title: Which statement is correct about constants?
Difficulty: Medium
Section Reference 1: 4.1 Variables
18) Which one of the following operators computes the remainder of an integer division?
a) /
b) %
c) \
d) !
Title: Which operator computes the remainder of an integer division?
Difficulty: Easy
Section Reference 1: 4.2 Arithmetic
19) What is the value of Math.pow(3, 2)?
a) 6.0
b) 9.0
c) 8.0
d) 5.0
Title: What is the value of Math.pow(3, 2)?
Difficulty: Easy
Section Reference 1: 4.2 Arithmetic
20) What is the output of the following code snippet?
public static void main(String[] args)
{
double a;
a = Math.sqrt(9.0) + Math.sqrt(16.0);
System.out.println(a);
}
a) 25.0
b) 337.0
c) 7.0
d) 19.0
Title: What is output of snippet (with Math.sqrt())?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
21) Which is the Java equivalent of the following mathematical expression?
c = √(a2 + b2)
a) c = Math.sqrt(a * 2 + b * 2);
b) c = Math.sqrt(a * 2) + Math.sqrt(b * 2);
c) c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
d) c = Math.sqrt(Math.pow(a, 2)) + Math.sqrt(Math.pow(b, 2));
Title: TB 2.10 Which is the Java equivalent of this mathematical expression?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
22) Which one of the following is a correct representation of the given mathematical expression in Java?
a + b
2
a) a + b % 2
b) a + b / 2
c) a + (b / 2)
d) (a + b) / 2
Title: Which is the Java equivalent of this mathematical expression?
Difficulty: Easy
Section Reference 1: 4.2 Arithmetic
23) Which of the following is the Java equivalent of the following mathematical expression?
c = 2π ⋅ radius
a) c = 2 * Math.PI * radius * 2;
b) c = 2 * Math.PI * Math.pow(2, radius);
c) c = 2 * Math.PI * Math.pow(radius, 2);
d) c = 2 * Math.PI * radius;
Title: Which is the Java equivalent of this mathematical expression?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
24) What is the result of the following statement?
String s = "You" + "had" + "me" + "at" + "hello";
a) The string s has the following value: "You had me at "hello"
b) The statement results in an error because the + operator can be used only with numbers
c) The statement results in an error because the + operation cannot be performed on string literals
d) The string s has the following value: "Youhadmeathello"
Title: What is result of statement (that uses + with strings)?
Difficulty: Medium
Section Reference 1: 4.5 Strings
25) Which operator is used to concatenate two or more strings?
a) +
b) %
c) &
d) ^
Title: Which operator is used to concatenate two or more strings?
Difficulty: Easy
Section Reference 1: 4.5 Strings
26) What output is produced by these statements?
String name = "Joanne Hunt";
System.out.println(name.length());
a) 8
b) 10
c) 9
d) 11
Title: What output is produced by (sending string length to output)?
Difficulty: Medium
Section Reference 1: 4.5 Strings
27) How do you compute the length of the string str?
a) length(str)
b) length.str
c) str.length
d) str.length()
Title: How do you compute the length of the string str?
Difficulty: Medium
Section Reference 1: 4.5 Strings
28) What is the output of the following code snippet?
public static void main(String[] args){
{
String str1;
str1 = "I LOVE MY COUNTRY";
String str2 = str1.substring(4, 11);
System.out.println(str2);
}
a) OVE MY
b) OVE MY C
c) VE MY CO
d) VE MY C
Title: What is output of snippet (with substring)?
Difficulty: Medium
Section Reference 1: 4.5 Strings
29) What is the output of the following code snippet?
public static void main(String[] args)
{
int s;
double f = 365.25;
s = f / 10;
System.out.println(s);
}
a) 36
b) 36.525
c) 37
d) No output because the code snippet generates compilation errors
Title: What is output of snippet (with division)?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
30) Assuming that the user inputs “Joe” at the prompt, what is the output of the following code snippet?
public static void main(String[] args)
{
System.out.print("Enter your name ");
String name;
Scanner in = new Scanner(System.in);
name = in.next();
name += ", Good morning";
System.out.print(name);
}
a) The code snippet does not compile because the += operator cannot be used in this context.
b) Joe, Good morning
c) , Good morning
d) Joe
Title: What is output of snippet (using += with string variable)?
Difficulty: Medium
Section Reference 1: 4.5 Strings
31) Which one of the following refers to a number constant that appears in code without explanation?
a) Constant
b) Variable
c) Magic number
d) String literal
Title: Which refers to a number constant that appears without explanation?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
32) What happens to the fractional part when a division is performed on two integer variables?
a) The fractional part is rounded off to the nearest integer value.
b) The fractional part is discarded.
c) Two integers cannot be used in division; at least one of the operands should be a floating-point number.
d) Instead of using an integer division, you should use the modulus operator to perform floating-point division.
Title: What happens to the fractional part (in integer division)?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
33) Consider the following division statements:
I. 22 / 7
II. 22.0 / 7
III. 22 / 7.0
Which of the following is correct?
a) All three statements will return an integer value.
b) Only I will return an integer value.
c) Only I, II will return an integer value.
d) Only I and III will return an integer value.
Title: Which is correct (about division statements)?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
34) Which of the following options is valid with reference to the code snippet?
public static void main(String[] args)
{
double d = 45.326;
double r = d % 9.0;
System.out.println(r);
}
a) The value inside the variable r will be 0.326
b) The value inside the variable r will be 5.036
c) Variable r has to be defined as an integer because the % operator always returns an integer
d) The initialization of variable r is wrong, because the % operator expects integer values as operands
Title: Which is valid for this snippet (using modulus)?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
35) What is the output of the following code snippet?
public static void main(String[] args)
{
int var1 = 10;
int var2 = 2;
int var3 = 20;
var3 = var3 / (var1 % var2);
System.out.println(var3);
}
a) 0
b) 4
c) 20
d) There will be no output due to a run-time error.
Title: What is output of snippet (using modulus)?
Difficulty: Hard
Section Reference 1: 4.2 Arithmetic
36) Which one of the following statements gives the absolute value of the floating-point number x = -25.50?
a) abs(x);
b) Math.abs(x);
c) x.abs();
d) x.absolute();
Title: Which statement gives the absolute value of this floating-point number?
Difficulty: Easy
Section Reference 1: 4.2 Arithmetic
37) Assuming that the user enters 45 and 62 as inputs for n1 and n2, respectively, what is the output of the following code snippet?
public static void main(String[] args)
{
System.out.print("Enter a number: ");
Scanner in = new Scanner(System.in);
String n1 = in.next();
System.out.print("Enter another number: ");
String n2 = in.next();
String result = n1 + n2;
System.out.print(result);
}
a) 46
b) 4662
c) 107
d) 4562
Title: What is output of snippet (that adds two numbers stored as strings)?
Difficulty: Medium
Section Reference: 4.5 Strings
38) Which of the methods below are static methods?
I. length
II. substring
III. pow
IV. sqrt
a) All the methods are static
b) Only I, II and III
c) Only II and IV
d) Only III and IV
Title: Which of these methods are static?
Difficulty: hard
Section Reference 1: 4.5 Strings
39) Which one of the following statements can be used to extract the last five characters from any string variable str?
a) str.substring(str.length() - 5, str.length())
b) str.substring(5, 5)
c) str.substring(str.length() - 4, 5)
d) str.substring(str.length() - 5, 5)
Title: Which statement extracts the last five characters from any string variable?
Difficulty: Hard
Section Reference 1: 4.5 Strings
40) Assuming that the user inputs a value of 25000 for the pay and 10 for the bonus rate in the following code snippet, what is the output?
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the pay: ");
double pay = in.nextDouble();
System.out.print("Enter the bonus rate: ");
double bonus = in.nextDouble();
System.out.println("The new pay is " +
(pay + pay * (bonus / 100.0)));
}
a) The new pay is 25000
b) The new pay is 25100
c) The new pay is 27500
d) The new pay is 30000
Title: What is output of snippet (that calculates value based on user input)?
Difficulty: Medium
Section Reference 1: 4.3 Input and Output
41) What is the value of Math.abs(-2)?
a) -2
b) 0
c) 2
d) 4
Title: What is the value of Math.abs(-2)?
Difficulty: Easy
Section Reference 1: 4.2 Arithmetic
42) What is the output of the following code snippet?
public static void main(String[] args)
{
double x;
x = Math.pow(3.0, 2.0) + Math.pow(4.0, 2.0);
System.out.println(x);
}
a) 25.0
b) 34
c) 7.0
d) 14
Title: What is output of snippet (with pow function)?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
43) Which is the Java equivalent of the following mathematical expression?
c = (√a + √b)2
a) c = Math.sqrt(a * 2 + b * 2);
b) c = Math.sqrt(a * 2) + Math.sqrt(b * 2);
c) c = Math.sqrt(pow(a, 2) + Math.pow(b, 2));
d) c = Math.pow((Math.sqrt(a) + Math.sqrt(b)), 2);
Title: Which is the Java equivalent of this mathematical expression?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
44) Which of the following is the Java equivalent of the following mathematical expression?
p = 2 ⋅π ⋅ (radius)3
a) p = 2 * Math.PI * (radius * 3);
b) p = Math.PI * Math.pow(3, radius);
c) p = 2 * Math.PI * Math.pow(radius, 3);
d) p = 2 * Math.pow(Math.PI * radius, 3);
Title: Which is the Java equivalent of this mathematical expression?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
45) How do you extract the first 5 characters from the string str?
a) substring(str, 5)
b) substring.str(0, 5)
c) str.substring(5)
d) str.substring(0, 5)
Title: How do you extract the first 5 characters from the string str?
Difficulty: Medium
Section Reference 1: 4.5 Strings
46) Which of the given System.out.print statements generates the following output?
ABCDE"\
a) System.out.println("ABCDE\"\\");
b) System.out.println("ABCDE"\");
c) System.out.println("ABCDE"\);
d) System.out.println("ABCDE\"\");
Title: Which System.out.print statement generates output with quote and backslash?
Difficulty: Medium
Section Reference 1: 4.5 Strings
47) Which of the given statements generates the following output?
\\\"///
a) System.out.println("\\\"///");
b) System.out.println("\\\\\\\"///");
c) System.out.println("\\\\\\""//////");
d) System.out.println("\\\"///");
Title: Which statement generates this output?
Difficulty: Hard
Section Reference 1: 4.5 Strings
48) What will be the value inside the variables x and y after the given set of assignments?
int x = 20;
int y = 10;
x = (x - y) * 2;
y = x / 2;
a) x = 40, y = 20
b) x = 20, y = 10
c) x = 10, y = 20
d) x = 20, y = 20
Title: What is the value of x and y after these assignments?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
49) What is the value inside the var variable at the end of the given code snippet?
public static void main(String[] args)
{
int var = 30;
var = var + 2 / var;
var++;
}
a) 0
b) 1
c) 30
d) 31
Title: What is var after this snippet (with assignment and increment)?
Difficulty: Easy
Section Reference 1: 4.2 Arithmetic
50) What is the output of the following code snippet?
public static void main(String[] args)
{
int num1 = 10;
int num2 = 5;
int num3 = 200;
num3 = num3 % (num1 * num2);
System.out.println(num3);
}
a) 0
b) 4
c) 10
d) 250
Title: What is output of snippet (using modulus)?
Difficulty: Hard
Section Reference 1: 4.2 Arithmetic
51) Assuming that the user enters 23 and 45 as inputs for num1 and num2, respectively, what is the output of the following code snippet?
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
String num1 = in.next();
System.out.print("Enter another number: ");
String num2 = in.next();
System.out.println(num1 + num2);
}
a) 23
b) 4523
c) 68
d) 2345
Title: What is output of snippet (that adds two numbers stored as strings)?
Difficulty: Medium
Section Reference 1: 4.5 Strings
52) Which one of the following statements can be used to extract the last 10 characters from the string variable str?
a) str.substring(str.length() - 10, str.length())
b) str.substring(10, str.length())
c) str.substring(str.length() - 9, 10)
d) str.substring(0, 10)
Title: Which statement extracts the last 10 characters from the string variable str?
Difficulty: Hard
Section Reference 1: 4.5 Strings
53) Which one of the following statements can be used to convert a string str to a double?
a) double n = str.parseDouble();
b) double n = Integer.parseDouble(str);
c) double n = Double.parseDouble(str);
d) double n = double.parseDouble(str);
Title: Which statement converts string to double?
Difficulty: Medium
Section Reference 1: 4.5 Strings
54) Which one of the following statements can be used to get the fifth character from a string str?
a) char c = str.charAt(5);
b) char c = str.charAt(4);
c) char c = str[5];
d) char c = str[4];
Title: Which statement gets a character from a string ?
Difficulty: Medium
Section Reference 1: 4.5 Strings
55) Which one of the following statements displays the output as 54321.00?
a) System.out.printf("%8.2f", 54321.0);
b) System.out.printf("%8,2f", 54321.0);
c) System.out.printf(",8.2f", 54321.0);
d) System.out.printf("%8.00f", 54321.0);
Title: Which statement displays specified formatted output?
Difficulty: Medium
Section Reference 1: 4.3 Input and Output
56) Which one of the following statements displays the output as (1.23e+02)?
a) System.out.printf("%(5.2e", -123.0);
b) System.out.printf("%5.2e", -123.0);
c) System.out.printf("^5.2e", -123.0);
d) System.out.printf("%5.2E", -123.0);
Title: Which statement displays specified formatted output ?
Difficulty: Medium
Section Reference 1: 4.3 Input and Output
57) Which one of the following statements defines a constant with the value 123?
a) final int MY_CONST = 123;
b) const int MY_CONST = 123;
c) final int MY_CONST;
MY_CONST = 123;
d) static int MY_CONST = 123;
Title: Which statement defines a constant?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
58) Which one of the following statements displays the output as +000321.00?
a) System.out.printf("+%09.2f", 321.0);
b) System.out.printf("%009,2f", 321.0);
c) System.out.printf("+9.2f", 321.0);
d) System.out.printf("%09.00f", 321.0);
Title: Which statement displays specified formatted output ?
Difficulty: Medium
Section Reference 1: 4.3 Input and Output
59) One way to avoid round-off errors is to use:
a) Math.sqrt()
b) Math.pow()
c) Math.round()
d) Math.truncate()
Title: What to use for rounding number?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
60) What (if any) type of error occurs with the following code if the user input is ABC?
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
String str = in.next();
int count = Integer.parseInt(str);
System.out.println("Input is " + count);
}
a) Compile-time error
b) Run-time error
c) Overflow error
d) Illegal expression
Title: What type of error with code containing Integer.parseInt()?
Difficulty: Medium
Section Reference 1: 4.5 Strings
61) What does the following statement sequence print?
String str = "Harry";
int n = str.length();
String mystery = str.substring(0, 1) + str.substring(n - 2, n);
System.out.println(mystery);
a) Ha
b) Har
c) Hy
d) Hry
Title: Output of code snippet with substring
Difficulty: Medium
Section Reference 1: 4.5 Strings
62) What does the following statement sequence print?
String str = "Hello";
int n = str.length();
String mystery = str.substring(0, 1)
+ str.substring(n - 2, n + 1);
System.out.println(mystery);
a) Run-time error
b) He
c) Ho
d) Hry
Title: Output of code snippet with substring
Difficulty: Medium
Section Reference 1: 4.5 Strings
63) What does the following statement sequence print?
String str = "Java Is Good";
int n = str.length();
String mystery = str.substring(n - 4, n) +
str.charAt(4) + str.substring(0, 4);
System.out.println(mystery);
a) Java
b) Good Java
c) Good
d) Is Good
Title: Output of code snippet with substring
Difficulty: Medium
Section Reference 1: 4.5 Strings
64) What does the following statement sequence print?
final String str = "Java";
str += " is powerful";
System.out.println(str);
a) Java is powerful
b) Java + is powerful
c) is powerful
d) Nothing; compile-time error
Title: Output of code snippet with string expression
Difficulty: Medium
Section Reference 1: 4.5 Strings
65) What does the following statement sequence print?
String str = "Java";
str += " is powerful";
System.out.println(str);
a) Java is powerful
b) Java + is powerful
c) is powerful
d) Compile-time error
Title: Output of code snippet with string expression
Difficulty: Medium
Section Reference 1: 4.5 Strings
66) What does the following statement sequence print if the user input is 123?
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number ");
int myInt = in.nextInt();
myInt += 456;
System.out.println(myInt);
}
a) 579
b) Compile-time error
c) Run-time error
d) 123456
Title: Output of code snippet with Scanner input
Difficulty: Medium
Section Reference 1: 4.3 Input and Output
67) What does the following statement sequence print if the user input is 123?
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
String str = in.next();
str += 456;
System.out.println(str);
}
a) 579
b) Compile-time error
c) Run-time error
d) 123456
Title: Output of code snippet with Scanner input
Difficulty: Medium
Section Reference 1: 4.3 Input and Output
68) What is the output of the following statement sequence?
public static void main(String[] args)
{
int x = 100.0 % 6.0;
System.out.println(x);
}
a) 4
b) Compile-time error
c) Run-time error
d) 16
Title: Output of code snippet with modulus operator
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
69) Which statement is true?
a) Variables cannot be assigned and declared in the same statement
b) Variable names must contain at least one dollar sign
c) Variable names can be no more than 8 characters long
d) It is incorrect to initialize a string variable with a number
Title: Which statement about variable names is true?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
70) Which statement about number literals in Java is false?
a) Numbers in exponential notation always have type double
b) Zero is an integer
c) Integers must be positive
d) An integer with fractional part of .0 has type double.
Title: Which statement about number literals in Java is false?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
71. Which option represents the best choice for a variable name to represent the average grade of students on an exam?
a) averageGrade
b) $averageGrade
c) avg
d) AveGd
Title: Which option is the best variable name?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
72) The assignment operator
a) denotes mathematical equality
b) places a new value into a variable
c) means the same as the equals sign used in algebra
d) makes it illegal to write a statement like sum = sum + 4;
Title: What is true about the assignment operator?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
73) Which of the following statements about constants in Java are true?
I. Although not required, constants are commonly named using uppercase letters
II. Only integer values can appear as constants
III. A variable can be defined with an initial value, but the reserved word final prevents it from being changed
IV. A named constant makes computations that use it clearer
a) I, II, III
b) II, III, IV
c) I, III, IV
d) I, II, IV
Title: Which of the following statements about constants are true?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
74) What is the output of this code snippet?
int sum = 22;
sum = sum + 2;
System.out.print(sum); // sum = sum + 4;
System.out.print(sum);
a) 2424
b) 2425
c) 2428
d) 2528
Title: What is the output of the code snippet?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
75) What is the output of this code snippet?
double average;
int grade1 = 87;
int grade2 = 94;
// System.out.print("The average is " + (grade1 + grade2) / 2.0);
System.out.print("The average is " + average);
a) Compile-time error
b) The average is 91.5
c) The average is 91.5
The average is 91.5
d) The average is 91.5
The average is 0.0
Title: What is the output of the code snippet?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
Section Reference 2: Common Error 4.1
76) What is the output of the following code snippet?
int counter = 0;
counter++;
System.out.print("The initial value of the counter is ");
System.out.println(count);
a) The initial value of the counter is 0
b) The initial value of the counter is 1
c) The code will not compile
d) The initial value of the counter is
Title: What is the output of the code snippet (using the ++ operator and initialization of variables)?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
Section Reference 2: Common Error 4.1
77. Which statements about numeric types in Java are true?
I. There is more than one integer type
II. The data type float uses twice the storage of double
III. The numeric range of the Java integer type is related to powers of two
a) I, II
b) I, III
c) II, III
d) I, II, III
Title: Which statements about numeric types in Java are true?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
Section Reference 2: Special Topic 4.1
78. The typical ranges for integers may seem strange but are derived from
a) Base 10 floating-point precision
b) Field requirements for typical usage and limits
c) Overflows
d) Powers of two because of base 2 representation within the computer
Title: What is the reason for the ranges for integer values in Java?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
79. What is result of evaluating the following expression?
(45 / 6) % 5
a) 2
b) 7
c) 2.5
d) 3
Title: What is the result of evaluating this arithmetic expression using the mod operator?
Difficulty: Easy
Section Reference 1: 4.2 Arithmetic
80. What is the difference between the result of the following two Java statements?
I. int cents = (int)(100 * price + 0.5);
II. int cents = (100 * price + 0.5);
a) Statement I causes truncation, but II does not
b) Statement II causes truncation, but I does not
c) Statement I compiles, but II does not
d) Statement II compiles, but I does not
Title: What is the difference in execution and result between these two Java statements?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
81) The first step in problem solving is
a) To write the expression that calculates the answer
b) To understand the problem and its inputs and outputs
c) To do examples by hand that confirm the solution will work
d) To write Java code that can be executed and tested
Title: What is the first step in problem solving?
Difficulty: Easy
Section Reference 1: 4.4 Problem Solving: First Do it By Hand
82) At what point in the problem-solving process should one write pseudocode?
a) After writing Java code, as a way to summarize the code’s algorithm
b) Before writing Java code, as a guide for a general solution
c) After defining Java variables so that the pseudocode and data types make sense
d) Before working out examples by hand in order to guide those examples
Title: When should pseudocode be written (in the steps for problem solving)?
Difficulty: Easy
Section Reference 1: 4.4 Problem Solving: First Do it By Hand
83) The problem solving process emphasizes a “first, do-it-by-hand” approach because
a) Pseudocode is not able to capture the subtleties of complex problems.
b) it is faster to do computations by hand than to do them by computer.
c) this guarantees that programs will be correct.
d) if programmers cannot compute a solution by hand, it is unlikely they will be able to write a program that can do it.
Title: Why should one “do examples by hand” in problem solving?
Difficulty: Easy
Section Reference 1: 4.4 Problem Solving: First Do it By Hand
84) What is the output of the following code snippet?
String firstname = "William";
String lastname;
System.out.println("First: " + first);
System.out.println("Last: " + lastname);
a)
First: William
Last:
b)
First: William
Last: lastname
c)
Code will not compile
d)
Unpredictable output
Title: What is the output of the code snippet (strings)
Difficulty: Medium
Section Reference 1: 4.5 Strings
85) What is the correct way to invoke methods on variables in Java that are strings?
a) Methods can only be invoked on string constants, not on variables.
b) For each method there is a special operator that must be used.
c) There are no methods available in Java for string variables.
d) Invoke them using the variable name and the dot (.) notation.
Title: How should methods be invoked on string variables?
Difficulty: Easy
Section Reference 1: 4.5 Strings
86) Suppose a phone number, stored as a ten-character string (of digits only) called phoneNumber must be converted into a string that has parentheses around the area code. Which statement below will do that?
a) String newNumber = "(" + phoneNumber.substring(3, 0) + ")";
b) String newNumber = "(" + ")" + phoneNumber;
c) String newNumber = "(" + phoneNumber.substring(1, 3) + ")" + phoneNumber.substring(3, 7);
d) String newNumber = "(" + phoneNumber.substring(0, 3) + ")" + phoneNumber.substring(3, 10);
Title: How do you manipulate a phone number as a string?
Difficulty: Hard
Section Reference 1: 4.5 Strings
87) Which of the following options defines an integer variable?
a) char age;
b) integer age;
c) int age;
d) age: int;
Title: Which defines an integer variable?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
88) Which statement is true about variable names in Java?
a) They can contain the percent sign (%)
b) They can contain an underscore symbol (“_”)
c) They can contain spaces
d) They must make sense as a word
Title:
Difficulty: Easy
Section Reference 1: 4.1 Numbers
89) Consider the following Java variable names:
I. 1stInstance
II. basicInt%
III. empName_
IV. addressLine1
V. DISCOUNT
Which of the following options is correct?
a) Only IV is a valid Java variable name.
b) Only I and IV are valid Java variable names.
c) Only I, IV, and V are valid Java variable names.
d) Only III, IV, and V are valid Java variable names.
Title: Which is correct (about variable names)?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
90) What is the value of the following expression?
3 * 5 / 2 * 5
a) 1.5
b) 30
c) 35
d) 37.5
Title: What is the value of the following expression?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
91) What is the result of the following code snippet?
double bottles;
double bottleVolume = bottles * 2;
System.out.println(bottleVolume);
a) 0
b) 1
c) 2
d) Does not compile
Title: What is the result of snippet (with assignment)?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
Section Reference 2: Common Error 4.1
92) Which one of the following is a correct method for defining and initializing an integer variable with name value?
a) int value = 30;
b) Int value = 30;
c) int value = .30;
d) Int value = .30;
Title: Which correctly defines and initializes an integer variable value?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
93) What is wrong with the following code snippet?
int size = 42;
cost = 9.99;
System.out.println("size = " + size);
System.out.println(" cost = " + cost);
a) The code snippet uses a variable that has not yet been initialized.
b) The code snippet uses a variable that has not been declared.
c) The code snippet attempts to assign a decimal value to an integer variable.
d) The code snippet attempts to assign an integer value to a decimal variable.
Title: What is wrong with snippet?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
Section Reference 2: Common Error 4.1
94) Which one of the following reserved words is used in Java to represent a value without a fractional part?
a) integer
b) int
c) Int
d) Float
Title: Which reserved word represents a value without a fractional part?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
95) In an airline reservation system, the number of available seats in an airplane is required. Which data type should be used to store this value?
a) double
b) float
c) int
d) long
Title: Which data type should store the number of available seats in an airplane?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
96) When you purchase donuts, they come in boxes of 12, so 24 donuts take 2 boxes. All donuts need to be in a box, so if you buy 13 donuts, you'll get 2 boxes with 12 in the first box and 1 in the second. If the integer variable numberOfDonuts contains the positive number of donuts purchased, which of the following will correctly give the number of boxes needed?
- int numberOfBoxes = (numberOfDonuts + 11)/12;
- int numberOfBoxes = numberOfDonuts /12;
- int numberOfBoxes = 1 + numberOfDonuts/12;
- int numberOfBoxes = numberOfDonuts/12 + numberOfDonuts % 12;
Title: How do you correctly express an integer variable?
Difficulty: Medium
Section Reference 1: 4.3 Inputs and Outputs
97) What is wrong with the following code snippet?
int price;
price = 9.42;
a) The price variable is never initialized.
b) The data type for the price variable is not specified.
c) The price variable is never assigned a value.
d) The price variable is assigned a decimal value.
Title: What is wrong with snippet (with value assigned to variable)?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
98.) Which one of the following is an assignment statement?
a) int a = 20;
b) a = 20;
c) assign a = 20;
d) assign 20 to a;
Title: Which is an assignment statement?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
99) Which one of the following types of statements is an instruction to replace the existing value of a variable with another value?
a) Update
b) Declaration
c) Assignment
d) Initialization
Title: Which statement replaces the existing value of a variable with another value?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
100) What is the meaning of x = 0; in Java?
a) It checks whether x equals 0.
b) It sets the variable x to zero.
c) It defines a variable named x and initializes it with 0.
d) It is a syntax error because x is not always 0.
Title: What is the meaning of x = 0; in Java?
Difficulty: Easy
Section Reference 1: 4.1 Numbers
101) What are the values of num1 and num2 after this snippet executes?
double num1 = 4.20;
double num2 = num1 * 10 + 5.0;
a) num1 = 4.20 and num2 = 63.0
b) num1 = 4.20 and num2 = 47.0
c) num1 = 42.0 and num2 = 42.0
d) num1 = 42.0 and num2 = 47.0
Title: What are num1 and num2 after snippet (with assignment)?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
102) Which of the following statements places input into the variable value given this line of code?
Scanner in = new Scanner(System.in);
a) int value = in();
b) int value = in.nextInt();
c) int value = in.next();
d) int value = in.nextFloat();
Title: Which statement places input into the variable value?
Difficulty: Easy
Section Reference 1: 4.3 Input and Output
103) Assuming that the user inputs a value of 30 for the price and 10 for the discount rate in the following code snippet, what is the output?
Scanner in = new Scanner(System.in);
System.out.print("Enter the price: ");
double price = in.nextDouble();
System.out.print("Enter the discount rate: ");
double discount = in.nextDouble();
System.out.print("The new price is ");
System.out.println(price - price * (discount / 100.0));
a) The new price is 30.0
b) The new price is 20.0
c) The new price is 27.0
d) The new price is 33.0
Title: What is output of snippet (that calculates value based on user input)?
Difficulty: Medium
Section Reference 1: 4.3 Input and Output
104) Which of the following statements is correct about constants?
a) Constants are written using uppercase letters because the compiler ignores constants declared in lowercase letters.
b) The data stored inside a final variable can be changed using an assignment statement.
c) You can make a variable constant by using the constant reserved word while declaring the variable.
d) Variables defined using final make a code snippet more readable and easier to maintain.
Title: Which statement is correct about constants?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
105) What is the value of Math.pow(2, 3)?
a) 5.0
b) 6.0
c) 8.0
d) 9.0
Title: What is the value of Math.pow(2, 3)?
Difficulty: Easy
Section Reference 1: 4.2 Arithmetic
106) Which one of the following is a correct representation of the given mathematical expression in Java?
a) a – b / 2 % 2
b) a - b / 2
c) a - (b / 2) / 2
d) (a – b / 2) / 2
Title: Which is the Java equivalent of this mathematical expression?
Difficulty: Easy
Section Reference 1: 4.2 Arithmetic
107) Given the definition final double PI = 3.14159; which of the following is the Java equivalent of the mathematical expression c = π ⋅ radius2
a) c = PI * (radius * 2);
b) c = PI * Math.pow(2, radius);
c) c = PI * Math.pow(radius, 2);
d) c = Math.pow(PI * radius, 2);
Title: Which is the Java equivalent of this mathematical expression?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
108) Which of the following is the mathematical equivalent of the following Java expression?
h = (4.0 * a * b – Math.pow(b, 2)) / c;
a) h = 4ab – 2b / c
b) h = (4ab – 2b) / c
c) h = 4ab – b2 / c
d) h = (4ab – b2) / c
Title: Which is the mathematical equivalent of Java expression?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
109) Assuming price contains the value 20.0, which of the following statements displays
price = 20.00
a)
System.out.print("price = ");
System.out.printf(price);
b)
System.out.print("price = ");
System.out.printf("%f", price);
c)
System.out.print("price = ");
System.out.printf("%10.2f", price);
d)
System.out.print("price = ");
System.out.printf("%2.10f", price);
Title: Which statement displays (this formatted output)?
Difficulty: Easy
Section Reference 1: 4.3 Input and Output
110) What is the output of the following code snippet?
System.out.printf("%5.3f", 20.0);
a) 20
b) 20.0
c) 20.00
d) 20.000
Title: What is output of code using format specifier?
Difficulty: Easy
Section Reference 1: 4.3 Input and Output
111) Which of the following statements will assign the largest value of three integer variables a, b, and c to the integer variable maximum?
a) maximum = Math.max(a, b, c);
b) maximum = Math.max(a, b);
maximum = Math.max(b, c);
maximum = Math.max(a, c);
c) maximum = Math.max(a, b);
maximum = Math.max(maximum, c);
d) maximum = Math.max(a, b) + Math.max(b, c) + Math.max(a, c);
Title: Which of the following statements will assign the largest value of three integer variables a, b, and c to the integer variable maximum?
Difficulty: Easy
Section Reference 1: 4.3 Input and Output
112) A student's class average can be computed by dividing the number of points they have earned by the number of points possible in the class. Assume the following variables have been declared as shown and given values elsewhere:
int pointsEarned, pointsPossible;
double classPercentage;
Which of the following statements will assign the correct percentage average to classPercentage? For example, if pointsEarned is 725 and pointsPossible is 1000, classPercentage should be assigned the value 72.5.
a)classPercentage = pointsEarned/pointsPossible * 100.0;
b)classPercentage = 100 * pointsEarned/pointsPossible;
c)classPercentage = (100.0 * pointsEarned)/pointsPossible;
d)classPercentage = (100.0 + pointsEarned)/pointsPossible;
Title: Which of the following statements will assign the correct percentage average to classPercentage?
Difficulty: Medium
Section Reference 1: 4.3 Input and Output
113) Assume the following variables have been declared and given values as shown:
int i = 2345;
double m = 67.8;
What will be printed by the statement below?
System.out.printf ("Values are %10d and %7.2f", i, j);
a)Values are 2345 and 67.8
b)Values are 2345 and 67.80
c)Values are 2345 and 67.80
d)Values are %10d and %7.2f 2345 67.8
Title: What will be printed by the statement below?
Difficulty: Medium
Section Reference 1: 4.3 Input and Output
114) Assume the following variables have been declared and given values as shown:
int i = 2345;
double m = 67.8;
Which statement will give the output below?
Values are 2345 and 67.80
a)System.out.printf ("Values are %10d and %7.2f", i, m);
b)System.out.printf ("Values are %6d and %2.2f", i, m);
c)System.out.printf ("Values are %i and %m");
d)System.out.println ("Values are " + i + " and " + m);
Title: Which statement will give the output below?
Difficulty: Medium
Section Reference 1: 4.3 Input and Output
115) What will be printed by the statement below?
System.out.print ("O\"my\t\\\"no!");
a)O"myt\"no!
b)O\"my\t\\\"no!
c)O\ my\t\\\ no!
d)O"my \"no!
Title: What will be printed by the statement below?
Difficulty: Medium
Section Reference 1: 4.3 Input and Output
116) Assume the following variable has been declared and given values as shown:
String name = "Mamey, Jean";
Which statement will print the name as "Jean Mamey"?
- System.out.print (name.substring(7) + " "
+ name.substring(0, 5)); - System.out.print (name.substring(8, 4) + " "
+ name.substring(1, 5)); - System.out.print (name.substring(8) + " "
+ name.substring(1, 4)); - System.out.print (name.substring(2) + " "
+ name.substring(1));
- System.out.print (name.substring(7) + " "
Title: Which statement will print the name as “Jean Mamey”?
Difficulty: Medium
Section Reference 1: 4.3 Input and Output
117) What will be printed by the statements below?
final int BONUS = 20;
int salary = 100;
int netSalary = salary + BONUS;
System.out.print (netSalary);
a)salaryBONUS
b)120
c)100BONUS
d) Nothing will be printed because constants cannot be used in expressions.
Title: What will be printed by the statement below?
Difficulty: Medium
Section Reference 1: 4.3 Input and Output
118) What will be printed by the statements below?
int x = 20;
int y = x;
x++;
y--;
System.out.print ("x = " + x + ", y = " + y);
x = 21, y = 19
a)x = 20, y = 20
b)x = 21, y = 20
c)x = 20, y = 19
d) x = 21, y = 19
Title: What will be printed by the statement below?
Difficulty: Medium
Section Reference 1: 4.3 Input and Output
119) What is the value of the following expression?
-18 % 5
a)-2
b)-3
c)2
d)3
Title: What is the value of the following expression?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
120) What is the value of the following expression?
1 % 12
a)1
b)0
c)-11
d) This is an error because 12 is greater than 1
Title: What is the value of the following expression?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
121) What is the value of the following expression?
2 + 3 + 5 / 2
a)5
b)6
c)7
d)7.5
Title: What is the value of the following expression?
Difficulty: Medium
Section Reference 1: 4.1 Numbers
122) Assume the following variables have been declared as shown and given values elsewhere:
double a, b, c;
The discriminant of a quadratic equation is defined to be:
Which of the following statements will assign the correct value to discriminant?
a)double discriminant = Math.sqrt (b * b – 4 * a * c);
b)double discriminant = Math.sqrt (b * b) – 4 * a * c;
c)double discriminant = Math.sqrt (b * b) - Math.sqrt (4 * a * c);
d)double discriminant = ;
Title: Which of the following statements will assign the correct value to discriminant?
Difficulty: Medium
Section Reference 1: 4.5 Strings
123) What will be printed by the statements below?
int x = 20;
int y = x;
x += 3;
y *= 2;
System.out.print ("x = " + x + ", y = " + y);
a)x = 23, y = 46
b)x = 23, y = 40
c)x = 46, y = 46
d)x = 3, y = 2
Title: What is the value of the following expression?
Difficulty: Medium
Section Reference 1: 4.3 Input and Output
124) Assume the following variables have been declared and given values as shown:
String str = "0123456789";
String sub = str.substring(3, 4);
Which is the value of sub?
a)3
b)34
c)345
d)3456
Title: What is the value of sub?
Difficulty: Hard
Section Reference 1: 4.5 Strings
125) Assume the following variable has been declared and given a value as shown:
String str = "0123456789";
Which is the value of str.length()?
a)9
b)10
c)11
d)12
Title: What is the value of str.length()?
Difficulty: Hard
Section Reference 1: 4.5 Strings
126) Assume the variable str has been declared to be a String that has at least one character. Which is the following represents the last character in str?
a)str.charAt (str.length())
b)str.lastChar()
c)str.charAt (str.length() – 1)
d)str.substring(str.length())
Title: Which is the following represents the last character in str?
Difficulty: Medium
Section Reference 1: 4.5 Strings
127) What will be printed by the statements below?
int x = 100;
int y = -12;
System.out.print ("Sum is " + x + y);
a)Sum is 100-12
b)Sum is 88
c)Sum is xy
d)Sum is 100 + -12
Title: What will be printed by the statements below?
Difficulty: Medium
Section Reference 1: 4.5 Strings