Java Test 1, Ch2 – Flashcards
Unlock all answers in this set
Unlock answersquestion
Suppose a Scanner object is created as follows: Scanner input = new Scanner(System.in); What method do you use to read an int value? A. input.nextInt(); B. input.nextInteger(); C. input.int(); D. input.integer();
answer
A. input.nextInt();
question
The following code fragment reads in two numbers: Scanner input = new Scanner(System.in); int i = input.nextInt(); double d = input.nextDouble(); What are the correct ways to enter these two numbers? A. Enter an integer, a space, a double value, and then the Enter key. B. Enter an integer, two spaces, a double value, and then the Enter key. C. Enter an integer, an Enter key, a double value, and then the Enter key. D. Enter a numeric value with a decimal point, a space, an integer, and then the Enter key.
answer
A. Enter an integer, a space, a double value, and then the Enter key. B. Enter an integer, two spaces, a double value, and then the Enter key. C. Enter an integer, an Enter key, a double value, and then the Enter key.
question
_______ is the code with natural language mixed with Java code. A. Java program B. A Java statement C. Pseudocode D. A flowchart diagram
answer
C. Pseudocode
question
If you enter 1 2 3, when you run this program, what will be the output? import java.util.Scanner; public class Test1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter three numbers: "); double number1 = input.nextDouble(); double number2 = input.nextDouble(); double number3 = input.nextDouble(); // Compute average double average = (number1 + number2 + number3) / 3; // Display result System.out.println(average); } } A. 1.0 B. 2.0 C. 3.0 D. 4.0
answer
B. 2.0
question
What is the exact output of the following code? double area = 3.5; System.out.print("area"); System.out.print(area); A. 3.53.5 B. 3.5 3.5 C. area3.5 D. area 3.5
answer
C. area3.5
question
Every letter in a Java keyword is in lowercase? A. true B. false
answer
A. true
question
Which of the following is a valid identifier? A. $343 B. class C. 9X D. 8+9 E. radius
answer
A. $343 E. radius
question
Which of the following are correct names for variables according to Java naming conventions? A. radius B. Radius C. RADIUS D. findArea E. FindArea
answer
A. radius D. findArea
question
Which of the following are correct ways to declare variables? A. int length; int width; B. int length, width; C. int length; width; D. int length, int width;
answer
A. int length; int width; B. int length, width;
question
____________ is the Java assignment operator. A. == B. := C. = D. =:
answer
C. =
question
To assign a value 1 to variable x, you write A. 1 = x; B. x = 1; C. x := 1; D. 1 := x; E. x == 1;
answer
B. x = 1;
question
Which of the following assignment statements is incorrect? A. i = j = k = 1; B. i = 1; j = 1; k = 1; C. i = 1 = j = 1 = k = 1; D. i == j == k == 1;
answer
C. i = 1 = j = 1 = k = 1; D. i == j == k == 1;
question
To declare a constant MAX_LENGTH inside a method with value 99.98, you write A. final MAX_LENGTH = 99.98; B. final float MAX_LENGTH = 99.98; C. double MAX_LENGTH = 99.98; D. final double MAX_LENGTH = 99.98;
answer
D. final double MAX_LENGTH = 99.98;
question
Which of the following is a constant, according to Java naming conventions? A. MAX_VALUE B. Test C. read D. ReadInt E. COUNT
answer
A. MAX_VALUE E. COUNT
question
To improve readability and maintainability, you should declare _________ instead of using literal values such as 3.14159. A. variables B. methods C. constants D. classes
answer
C. constants
question
Which of these data types requires the most amount of memory? A. long B. int C. short D. byte
answer
A. long
question
To declare an int variable number with initial value 2, you write A. int number = 2L; B. int number = 2l; C. int number = 2; D. int number = 2.0;
answer
C. int number = 2;
question
If a number is too large to be stored in a variable of the float type, it _____________. A. causes overflow B. causes underflow C. causes no error D. cannot happen in Java
answer
A. causes overflow
question
Analyze the following code: public class Test { public static void main(String[] args) { int n = 10000 * 10000 * 10000; System.out.println("n is " + n); } } A. The program displays n is 1000000000000 B. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program is aborted. C. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program continues to execute because Java does not report errors on overflow. D. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an underflow and the program is aborted. E. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an underflow and the program continues to execute because Java does not report errors on underflow.
answer
C. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program continues to execute because Java does not report errors on overflow.
question
What is the result of 45 / 4? A. 10 B. 11 C. 11.25 D. 12
answer
B. 11
question
Which of the following expressions will yield 0.5? A. 1 / 2 B. 1.0 / 2 C. (double) (1 / 2) D. (double) 1 / 2 E. 1 / 2.0
answer
B. 1.0 / 2 D. (double) 1 / 2 E. 1 / 2.0
question
Which of the following expression results in a value 1? A. 2 % 1 B. 15 % 4 C. 25 % 5 D. 37 % 6
answer
D. 37 % 6
question
25 % 1 is _____ A. 1 B. 2 C. 3 D. 4 E. 0
answer
E. 0
question
-25 % 5 is _____ A. 1 B. 2 C. 3 D. 4 E. 0
answer
E. 0
question
24 % 5 is _____ A. 1 B. 2 C. 3 D. 4 E. 0
answer
E. 0
question
-24 % 5 is _____ A. -1 B. -2 C. -3 D. -4 E. 0
answer
D. -4
question
-24 % -5 is _____ A. 3 B. -3 C. 4 D. -4 E. 0
answer
D. -4
question
To add a value 1 to variable x, you write A. 1 + x = x; B. x += 1; C. x := 1; D. x = x + 1; E. x = 1 + x;
answer
B. x += 1; D. x = x + 1; E. x = 1 + x;
question
To add number to sum, you write (Note: Java is case-sensitive) A. number += sum; B. number = sum + number; C. sum = Number + sum; D. sum += number; E. sum = sum + number;
answer
D. sum += number; E. sum = sum + number;
question
Suppose x is 1. What is x after x += 2? A. 0 B. 1 C. 2 D. 3 E. 4
answer
D. 3
question
Suppose x is 1. What is x after x -= 1? A. 0 B. 1 C. 2 D. -1 E. -2
answer
A. 0
question
What is x after the following statements? int x = 1; int y = 2; x *= y + 1; A. x is 1. B. x is 2. C. x is 3. D. x is 4.
answer
C. x is 3.
question
What is x after the following statements? int x = 1; x *= x + 1; A. x is 1. B. x is 2. C. x is 3. D. x is 4.
answer
B. x is 2.
question
Math.pow(2, 3) returns __________. A. 9 B. 8 C. 9.0 D. 8.0
answer
D. 8.0
question
Math.pow(4, 1 / 2) returns __________. A. 2 B. 2.0 C. 0 D. 1.0 E. 1
answer
D. 1.0
question
Math.pow(4, 1.0 / 2) returns __________. A. 2 B. 2.0 C. 0 D. 1.0 E. 1
answer
B. 2.0
question
The __________ method returns a raised to the power of b. A. Math.power(a, b) B. Math.exponent(a, b) C. Math.pow(a, b) D. Math.pow(b, a)
answer
C. Math.pow(a, b)
question
Analyze the following code. public class Test { public static void main(String[] args) { int month = 09; System.out.println("month is " + month); } } A. The program displays month is 09 B. The program displays month is 9 C. The program displays month is 9.0 D. The program has a syntax error, because 09 is an incorrect literal value.
answer
D. The program has a syntax error, because 09 is an incorrect literal value.
question
What is y displayed in the following code? public class Test1 { public static void main(String[] args) { int x = 1; int y = x = x + 1; System.out.println("y is " + y); } } A. y is 0. B. y is 1 because x is assigned to y first. C. y is 2 because x + 1 is assigned to x and then x is assigned to y. D. The program has a compile error since x is redeclared in the statement int y = x = x + 1.
answer
C. y is 2 because x + 1 is assigned to x and then x is assigned to y.
question
Are the following four statements equivalent? number += 1; number = number + 1; number++; ++number; A. Yes B. No
answer
A. Yes
question
What is i printed? public class Test { public static void main(String[] args) { int j = 0; int i = ++j + j * 5; System.out.println("What is i? " + i); } } A. 0 B. 1 C. 5 D. 6
answer
D. 6
question
What is i printed in the following code? public class Test { public static void main(String[] args) { int j = 0; int i = j++ + j * 5; System.out.println("What is i? " + i); } } A. 0 B. 1 C. 5 D. 6
answer
C. 5
question
What is y displayed in the following code? public class Test { public static void main(String[] args) { int x = 1; int y = x++ + x; System.out.println("y is " + y); } } A. y is 1. B. y is 2. C. y is 3. D. y is 4.
answer
C. y is 3.
question
What is y displayed? public class Test { public static void main(String[] args) { int x = 1; int y = x + x++; System.out.println("y is " + y); } } A. y is 1. B. y is 2. C. y is 3. D. y is 4.
answer
B. y is 2.
question
To assign a double variable d to a float variable x, you write A. x = (long)d B. x = (int)d; C. x = d; D. x = (float)d;
answer
D. x = (float)d;
question
What is the printout of the following code: double x = 5.5; int y = (int)x; System.out.println("x is " + x + " and y is " + y); A. x is 5 and y is 6 B. x is 6.0 and y is 6.0 C. x is 6 and y is 6 D. x is 5.5 and y is 5 E. x is 5.5 and y is 5.0
answer
D. x is 5.5 and y is 5
question
Which of the following assignment statements is illegal? A. float f = -34; B. int t = 23; C. short s = 10; D. int t = (int)false; E. int t = 4.5;
answer
D. int t = (int)false; E. int t = 4.5;
question
What is the value of (double)5/2? A. 2 B. 2.5 C. 3 D. 2.0 E. 3.0
answer
B. 2.5
question
What is the value of (double)(5/2)? A. 2 B. 2.5 C. 3 D. 2.0 E. 3.0
answer
D. 2.0
question
Which of the following expression results in 45.37? A. (int)(45.378 * 100) / 100 B. (int)(45.378 * 100) / 100.0 C. (int)(45.378 * 100 / 100) D. (int)(45.378) * 100 / 100.0
answer
B. (int)(45.378 * 100) / 100.0
question
The expression (int)(76.0252175 * 100) / 100 evaluates to _________. A. 76.02 B. 76 C. 76.0252175 D. 76.03
answer
B. 76
question
If you attempt to add an int, a byte, a long, and a double, the result will be a __________ value. A. byte B. int C. long D. double
answer
D. double
question
Which of the following is the correct expression of character 4? A. 4 B. "4" C. '004' D. '4'
answer
D. '4'
question
A Java character is stored in __________. A. one byte B. two bytes C. three bytes D. four bytes
answer
B. two bytes
question
Suppose x is a char variable with a value 'b'. What is the printout of the statement System.out.println(++x)? A. a B. b C. c D. d
answer
C. c
question
Which of the following statement prints smithexam1test.txt? A. System.out.println("smithexam1test.txt"); B. System.out.println("smithexam1test.txt"); C. System.out.println("smith"exam1"test.txt"); D. System.out.println("smith"exam1"test.txt");
answer
B. System.out.println("smithexam1test.txt");
question
Suppose i is an int type variable. Which of the following statements display the character whose Unicode is stored in variable i? A. System.out.println(i); B. System.out.println((char)i); C. System.out.println((int)i); D. System.out.println(i + " ");
answer
B. System.out.println((char)i);
question
The Unicode of 'a' is 97. What is the Unicode for 'c'? A. 96 B. 97 C. 98 D. 99
answer
D. 99
question
Will System.out.println((char)4) display 4? A. Yes B. No
answer
B. No
question
What is the printout of System.out.println('z' - 'a')? A. 25 B. 26 C. a D. z
answer
A. 25
question
An int variable can hold __________. A. 'x' B. 120 C. 120.0 D. "x" E. "120"
answer
A. 'x' B. 120
question
Which of the following assignment statements is correct? A. char c = 'd'; B. char c = 100; C. char c = "d"; D. char c = "100";
answer
A. char c = 'd'; B. char c = 100;
question
'3' - '2' + 'm' / 'n' is ______. A. 0 B. 1 C. 2 D. 3
answer
B. 1
question
The expression "Java " + 1 + 2 + 3 evaluates to ________. A. Java123 B. Java6 C. Java 123 D. java 123 E. Illegal expression
answer
C. Java 123
question
Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to ________. A. 66 B. B C. A1 D. Illegal expression
answer
C. A1
question
Note that the Unicode for character A is 65. The expression 'A' + 1 evaluates to ________. A. 66 B. B C. A1 D. Illegal expression
answer
A. 66
question
The System.currentTimeMillis() returns ________________ . A. the current time. B. the current time in milliseconds. C. the current time in milliseconds since midnight. D. the current time in milliseconds since midnight, January 1, 1970. E. the current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time).
answer
E. the current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time).
question
Programming style is important, because ______________. A. a program may not compile if it has a bad style B. good programming style can make a program run faster C. good programming style makes a program more readable D. good programming style helps reduce programming errors
answer
C. good programming style makes a program more readable D. good programming style helps reduce programming errors
question
According to Java naming convention, which of the following names can be variables? A. FindArea B. findArea C. totalLength D. TOTAL_LENGTH E. class
answer
B. findArea C. totalLength
question
If a program compiles fine, but it produces incorrect result, then the program suffers __________. A. a compilation error B. a runtime error C. a logic error
answer
C. a logic error
question
The __________ method displays an input dialog for reading a string. A. String string = JOptionPane.showMessageDialog(null, "Enter a string", "Input Demo", JOptionPane.QUESTION_MESSAGE); B. String string = JOptionPane.showInputDialog(null, "Enter a string", "Input Demo", JOptionPane.QUESTION_MESSAGE); C. String string = JOptionPane.showInputDialog("Enter a string", "Input Demo", JOptionPane.QUESTION_MESSAGE); D. String string = JOptionPane.showInputDialog(null, "Enter a string"); E. String string = JOptionPane.showInputDialog("Enter a string");
answer
B. String string = JOptionPane.showInputDialog(null, "Enter a string", "Input Demo", JOptionPane.QUESTION_MESSAGE); D. String string = JOptionPane.showInputDialog(null, "Enter a string"); E. String string = JOptionPane.showInputDialog("Enter a string");
question
The __________ method parses a string s to an int value. A. integer.parseInt(s); B. Integer.parseInt(s); C. integer.parseInteger(s); D. Integer.parseInteger(s);
answer
B. Integer.parseInt(s);
question
The __________ method parses a string s to a double value. A. double.parseDouble(s); B. Double.parsedouble(s); C. double.parse(s); D. Double.parseDouble(s);
answer
D. Double.parseDouble(s);
question
Analyze the following code. import javax.swing.*; public class ShowErrors { public static void main(String[] args) { int i; int j; String s = JOptionPane.showInputDialog(null, "Enter an integer", "Input", JOptionPane.QUESTION_MESSAGE); j = Integer.parseInt(s); i = (i + 4); } } A. The program cannot compile because j is not initialized. B. The program cannot compile because i does not have an initial value when it is used in i = i + 4; C. The program compiles but has a runtime error because i does not have an initial value when it is used in i = i + 4; D. The program compiles and runs fine.
answer
B. The program cannot compile because i does not have an initial value when it is used in i = i + 4;