Intro to Java questions: Elementary Programming 2 – 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?
answer
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?
answer
Enter an integer, a space, a double value, and then the Enter key. Enter an integer, two spaces, a double value, and then the Enter key. 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
answer
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); } }
answer
2.0
question
What is the exact output of the following code? double area = 3.5; System.out.print("area"); System.out.print(area);
answer
area3.5
question
Every letter in a Java keyword is in lowercase?
answer
true
question
Which of the following is a valid identifier?
answer
$343 radius
question
Which of the following are correct names for variables according to Java naming conventions?
answer
radius findArea
question
is the Java assignment operator
answer
=
question
To assign a value 1 to variable x, you write
answer
x = 1;
question
Which of the following assignment statements is incorrect
answer
i == j == k == 1; i = 1 = j = 1 = k = 1;
question
To declare a constant MAX_LENGTH inside a method with value 99.98, you write
answer
final double MAX_LENGTH = 99.98;
question
Which of the following is a constant, according to Java naming conventions?
answer
MAX_VALUE COUNT
question
To improve readability and maintainability, you should declare _________ instead of using literal values such as 3.14159.
answer
constants
question
According to Java naming convention, which of the following names can be variables?
answer
totalLength findArea
question
Which of these data types requires the most amount of memory?
answer
long
question
If a number is too large to be stored in a variable of the float type, it _____________
answer
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); } }
answer
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?
answer
11
question
Which of the following expression results in a value 1?
answer
37 % 6
question
24 % 5 is _____
answer
4
question
-24 % 5 is _____
answer
-4
question
-24 % -5 is _____
answer
-4
question
Math.pow(2, 3) returns __________.
answer
8.0
question
Math.pow(4, 1.0 / 2) returns __________.
answer
2.0
question
The __________ method returns a raised to the power of b.
answer
Math.pow(a, b)
question
To declare an int variable number with initial value 2, you write
answer
int number = 2;
question
Analyze the following code. public class Test { public static void main(String[] args) { int month = 09; System.out.println("month is " + month); } }
answer
The program has a syntax error, because 09 is an incorrect literal value.
question
Which of the following are the same as 1545.534?
answer
A. 1.545534e+3 B. 0.1545534e+4 C. 1545534.0e-3 D. 154553.4e-2
question
The expression 4 + 20 / (3 - 1) * 2 is evaluated to
answer
24
question
To add a value 1 to variable x, you write
answer
x += 1; x = x + 1; x = 1 + x;
question
To add number to sum, you write (Note: Java is case-sensitive)
answer
sum = sum + number; sum += number;
question
Suppose x is 1. What is x after x += 2?
answer
3
question
What is x after the following statements? int x = 2; int y = 1; x *= y + 1;
answer
x is 4
question
What is x after the following statements? int x = 1; x *= x + 1;
answer
x is 2.
question
Which of the following statements are the same? (A) x -= x + 4 (B) x = x + 4 - x (C) x = x - (x + 4)
answer
(A) and (C) are the same
question
Are the following four statements equivalent? number += 1; number = number + 1; number++; ++number;
answer
Yes
question
What is the value of 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); } }
answer
6 Explanation: Operands are evaluated from left to right in Java. The left-hand operand of a binary operator is evaluated before any part of the right-hand operand is evaluated. This rule takes precedence over any other rules that govern expressions. Therefore, ++j is evaluated first, and returns 1. Then j*5 is evaluated, returns 5.
question
What is the value of 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); } }
answer
5 Explanation: Same as before, except that j++ evaluates to 0.
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); } }
answer
y is 3.
question
public class Test { public static void main(String[] args) { int x = 1; int y = x + x++; System.out.println("y is " + y); } }
answer
y is 2 Explanation: When evaluating x + x++, x is evaluated first, which is 1. X++ returns 1 since it is post-increment and 2. Therefore y is 1 + 1.
question
To assign a double variable d to a float variable x, you write
answer
x = (float)d;
question
Which of the following expressions will yield 0.5?
answer
(double) (1 / 2) (double) 1 / 2 1.0 / 2
question
What will be displayed by the following code? double x = 5.5; int y = (int)x; System.out.println("x is " + x + " and y is " + y);
answer
x is 5.5 and y is 5
question
Which of the following assignment statements is illegal?
answer
int t = (int)false; int t = 4.5;
question
What is the value of (double)5/2?
answer
2.0
question
Which of the following expression results in 45.37?
answer
(int)(45.378 * 100) / 100.0
question
The expression (int)(76.0252175 * 100) / 100 evaluates to _________.
answer
76
question
If you attempt to add an int, a byte, a long, and a double, the result will be a __________ value.
answer
double
question
_____________ is a formal process that seeks to understand the problem and document in detail what the software system needs to do
answer
Requirements specification
question
_____________ seeks to analyze the data flow and to identify the system's input and output.
answer
System analysis
question
Which of the following is the correct expression of character 4?
answer
'4'
question
A Java character is stored in __________.
answer
two bytes
question
Suppose x is a char variable with a value 'b'. What will be displayed by the statement System.out.println(++x)?
answer
c
question
Which of the following statement prints smithexam1test.txt?
answer
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?
answer
System.out.println((char)i);
question
The Unicode of 'a' is 97. What is the Unicode for 'c'?
answer
99
question
Will System.out.println((char)4) display 4?
answer
no Explanation: The Unicode is to be displayed, not number 4.
question
What will be displayed by System.out.println('z' - 'a')?
answer
25
question
An int variable can hold __________.
answer
'x' 120
question
Which of the following assignment statements is correct?
answer
'd'; char c = 100; Explanation: Choice (B) is also correct, because an int value can be implicitly cast into a char variable. The Unicode of the character is the int value. In this case, the character is d
question
'3' - '2' + 'm' / 'n' is ______.
answer
1
question
The expression "Java " + 1 + 2 + 3 evaluates to ________.
answer
Java 123
question
Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to ________.
answer
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
66
question
The System.currentTimeMillis() returns ________________ .
answer
The current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time)
question
The __________ method displays an input dialog for reading a string.
answer
String string = JOptionPane.showInputDialog(null, "Enter a string", "Input Demo", JOptionPane.QUESTION_MESSAGE); String string = JOptionPane.showInputDialog(null, "Enter a string"); . String string = JOptionPane.showInputDialog("Enter a string");
question
The __________ method parses a string s to an int value.
answer
Integer.parseInt(s);
question
The __________ method parses a string s to a double value.
answer
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); } }
answer
The program cannot compile because i does not have an initial value when it is used in i = i + 4;