1/84
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Consider the following code segment.
System.out.print("Oh, ");
System.out.println("happy");
System.out.print("happy");
System.out.print("day!");
What is printed as a result of executing the code segment? Choose the best answer.
Oh, happy
happyday!
Oh, happyhappyday!
Oh, happy happy day!
Oh,
happyhappyday!
Oh, happy
happyday!
println()
prints the output and then moves the cursor to the next line but
print()
prints and keeps the cursor on the same line. Also: note any spaces added to the literals.
Consider the following code segment.
System.out.print(‘count down’);
System.out.print(“4”);
System.out.print(“3”);
System.out.print(“2”);
System.out.print(“1”);
System.out.print(“ liftoff!”);
What is printed as a result of executing the code segment? Choose the best answer.
Nothing - compiler error
54321liftoff!
5
4
3
2
1 liftoff!
54321
liftoff!
Nothing - compiler error
Also note the values of Strings are surrounded by quotation marks and the values of char data types are surrounded by single quotes.
‘count down’
would cause a compiler error because it’s a String literal and not a single char.
Consider the following code segment.
System.out.println(happy); // Line 1
System.out.print(day); // Line 2
The code segment is intended to produce the following output but does not work as intended.
happy
day
Which of the following changes can be made so that the code segment produces the intended output? Choose the best answer.
Enclosing happy
in line 1 and day
in line 2 with quotation marks
Inserting System.out.print();
between lines 1 and 2
Changing print
in line 2 to println
Inserting System.out.println();
between lines 1 and 2
Enclosing happy
in line 1 and day
in line 2 with quotation marks
Consider the following code segment.
What is printed as a result of executing the code segment? Choose the best answer.
“Observe good faith and justice toward all nations.”
George Washington
Observe good faith and justice toward all nations.
George Washington
“Observe good faith and justice toward all nations.” George Washington
“Observe good faith and justice
toward all nations.”
George Washington
“Observe good faith and justice
toward all nations.”
George Washington
Question 5
Consider the following code segment.
System.out.print(“\”If you tell “);
System.out.print(“the truth you “);
System.out.print(“don’t have to “);
System.out.println(“remember anything.”);
System.out.print(“Mark Twain”);
What is printed as a result of executing the code segment? Choose the best answer.
“If you tell the truth you don’t have to remember anything.
Mark Twain
“If you tell
the truth you
don’t have to
remember anything.”
Mark Twain
“If you tell the truth you don’t have to remember anything.”
Mark Twain
Nothing - compiler error
“If you tell the truth you don’t have to remember anything.
Mark Twain
println()
prints the output and then moves the cursor to the next line but
print()
prints and keeps the cursor on the same line.
Also, note any spaces in the String literals and any escape sequences.
Question 6
2 / 2 pts
Consider the following code segment.
System.out.print(5)
System.out.print(4)
System.out.print(3)
System.out.print(2)
System.out.print(1)
System.out.print(“ liftoff!”);
What is printed as a result of executing the code segment? Choose the best answer.
Nothing - compiler error
54321
liftoff!
54321liftoff!
5
4
3
2
1 liftoff!
Nothing - compiler error
One of the most common compiler errors is a missing semicolon. Note the missing semicolon in the first five lines.
Each of the following code segments is intended to print the word
Happy
Which of the following code segments works as intended? Choose the best answer.
System.out.print(Happy);
System.out.print(Ha);
System.out.print(ppy);
System.out.print("Happy");
System.out.print("Happy");
Consider the following code segment.
System.out.print(“It’s”);
System.out.print(“a”);
System.out.print(“beautiful”);
System.out.println(“ day!”);
What is printed as a result of executing the code segment? Choose the best answer.
It’s
a
beautiful
day!
It’sabeautiful day!
It’sabeautifulday!
Nothing - compiler error
It’sabeautiful day!
Consider the following code segment.
System.out.print(“Oh, what fun”);
System.out.print(“ it is”)
System.out.print(“ to learn Java!”);
What is printed as a result of executing the code segment? Choose the best answer.
Oh, what fun
it is
to learn Java!”
Oh, what fun it is to learn Java!
“Oh, what fun it is to learn Java!”
Nothing - compiler error
Nothing - compiler error
Each of the following code segments is intended to print the quote
”Whatever you do, do it well.” - Walt Disney
Which of the following code segments works as intended? Choose the best answer.
If an int variable age holds the value 7, what is its value after the following statement is executed?
age = age * 3;
If it shows an error, just type error.
21
Which of the following expressions evaluate to 4 ?
I. 7 + 10 % 13
II. (7 + 10) % 13
III. 6 – 2 % 13
I only
I, II, & III
II & III only
I & II only
II & III only
What is the result of 13 % 5 when evaluated in a Java expression?
If it shows an error, just type error.
3
What is the result of 17 / 4 when evaluated in a Java expression?
If it shows an error, just type error.
4
What would be the output of the following code?
int x = 110;
int y = 3;
x %= y;
System.out.println(x);
2
3
107
5
2
x and y are declared as integer variables.
You are given the following expression:
x >= y
Which of these expressions is the opposite of the above expression and has valid syntax?
x =< y
x < y
There’s not enough information to determine this.
x == y
x <= y
x < y
If an int variable weight currently holds the value 150, what is its value after the following statement is executed?
weight -= 27;
If it shows an error, just type error.
123
What will be printed after the following statements are executed?
int length;
length = 11; // 11
length *= 3;
length *= length;
length /= 50;
System.out.println(length);
If it shows an error, just type error.
21
Assume that you are given the following declarations:
int num = 0;
double val = 0.0;
val = 111 % 5 / 3 - 1;
Show the value that will be stored in the variable on the left. If the expression causes an error, just type ‘error.’
-1.0
What would be the output of the following code?
int x = 110;
int y = 3;
x %= y;
System.out.println(x);
5
107
3
2
2
What would be the output of the following code?
int input = 5;
int output = 3;
input++;
output += input;
System.out.println(output);
9
11
12
10
9
Assume that you are given the following declarations:
int num = 0;
double val = 0.0;
val = 17 % 6 / 4 - 3;
Show the value that will be stored in the variable on the left. If the expression causes an error, just type ‘error.’
-2.0
What would be the output of the following code?
int x = 10;
int y = 5;
x--;
y += x;
System.out.println(y);
14
11
12
15
14
Assume that you are given the following declarations:
int num = 0;
double val = 0.0;
num = 9 % 6 / 4 - 4;
Show the value that will be stored in the variable on the left. If the expression causes an error, just type ‘error.’
-4
What would be the output of the following code?
int x = 10;
int y = 5;
x %= y;
System.out.println(x);
1
5
0
10
0
What will be printed after the following statements are executed?
int length;
length = 11; // 11
length *= 3;
length *= length;
length /= 50;
System.out.println(length);
If it shows an error, just type error.
21
Assume that you are given the following declarations:
int num = 0;
double val = 0.0;
num = 11 % 6 / 2 - 1;
Show the value that will be stored in the variable on the left. If the expression causes an error, just type 'error.'
1
Assume that you are given the following declarations:
int num = 0;
double val = 0.0;
num = 2 % 6 / 2 - 4;
Show the value that will be stored in the variable on the left. If the expression causes an error, just type ‘error.’
-3
Which of the following returns the correct average when 3 values had been added to an integer total?
(int) total / 3;
(double) total / 3;
total / 3;
(double) (total / 3);
(double) total / 3;
Given the following code segment, what is the value of b when it finishes executing?
double a = 9.6982;
int b = 12;
b = (int) a;
9
9.6982
10
12
9
When a double is converted into an integer in Java, it truncates (throws away) the digits after the decimal.
Given the following declarations:
int i = 15;
short s = 25;
long m = 50;
float f = 2.5f;
double d = 0.25;
Show the value that will be stored in the variable on the left after the expression below is executed. If it shows an error, just type error.
s = (short)5;
5
The value of 5 on the right is an integer which could cause problems with moving into a smaller int, so explicitly casting it as a short is smart; however, the value of 5 is small enough to fit into a smaller short. No need to explicitly cast unless the value would increase substantially in the future.
Given the following declarations:
int i = 15;
short s = 25;
long m = 50;
float f = 2.5f;
double d = 0.25;
Show the value that will be stored in the variable on the left after the expression below is executed. If it shows an error, just type error.
i = f * 2;
error
We cannot convert a float (precision) into an int UNLESS we explicitly cast:
i = (int)f * 2;
Given the following declarations:
int i = 15;
short s = 25;
long m = 50;
float f = 2.5f;
double d = 0.25;
Show the value that will be stored in the variable on the left after the expression below is executed. If it shows an error, just type error.
i = s + 1;
26
Moving a short into an int is implicit casting which needs not specification (int); A smaller data type into a larger data type.
The following code is illegal. Rewrite the code using an integer cast to make it legal.
double d = 187.2;
int j = d;
int j = (int) d;
int j = 187.2;
double d = d + 187.2;
int double d = 187.2;
int j = (int) d;
Given the following declarations:
int i = 15;
short s = 25;
long m = 50;
float f = 2.5f;
double d = 0.25;
Show the value that will be stored in the variable on the left after the expression below is executed. If it shows an error, just type error.
f = 3.14;
error
We cannot convert a double into a smaller float UNLESS we explicitly cast
Given the following declarations:
int i = 15;
short s = 25;
long m = 50;
float f = 2.5f;
double d = 0.25;
Show the value that will be stored in the variable on the left after the expression below is executed. If it shows an error, just type error.
f = 3.14f;
3.14
The ‘f’ added to the value shows that the value is a float. So, the value of a float moving into a float works with no problem. If the ‘f’ were not there, we’d assume the 3.14 was a double.
Assume that you are given the following declarations:
int num;
double val;
val = 17 / 2.0 + 4;
Show the value that will be stored in the variable on the left. If the expression causes an error, just type 'error'.
12.5
Assume that you are given the following declarations:
int num;
double val;
num = 17 / 2.0 + 4;
Show the value that will be stored in the variable on the left. If the expression causes an error, just type 'error'.
error
Given the following declarations:
int i = 15;
short s = 25;
long m = 50;
float f = 2.5f;
double d = 0.25;
Show the value that will be stored in the variable on the left after the expression below is executed. If it shows an error, just type error.
f = (float) 3.14;
3.14
The value of 3.14 on the right is a double which could cause problems with moving into a smaller float, so we need to explicitly cast as a float.
Which of the following returns the correct average when 3 values had been added to an integer total?
(int) total / 3;
(double) (total / 3);
(double) total / 3;
total / 3;
(double) total / 3;
Of the following declarations, which are declared properly? Choose all that apply.
byte t = 130;
int q = 0;
short s = 130;
byte r = 100;
int q = 0;
short s = 130;
byte r = 100;
short data types have a range of -32,768 to 32,767 with a memory size of 2 bytes. int data types have a range of -2,147,483,648 to 2,147,483,647 with a memory size of 4 bytes. byte data types have a range of -128 to 127 with a memory size of 1 byte.
What is the size in bytes for a float data type?
1
8
4
2
4
Given the following declarations:
int i = 15;
short s = 25;
long m = 50;
float f = 2.5f;
double d = 0.25;
Show the value that will be stored in the variable on the left after the expression below is executed. If it shows an error, just type error.
i = f * 2;
error
Which value below falls within the range of an short? Choose all that apply.
130
0
1000
100
130
0
1000
100
short data types have a range of -32,768 to 32,767 with a memory size of 2 bytes.
Consider the following code segment where x is a local variable:
int x;
System.out.println(x);
What would be the output? (Choose the best answer.)
None, Compiler error
x
None, runtime error
0
None, Compiler error
Given the following declarations:
int i = 12;
short s = 35;
long m = 50;
float f = 2.5f;
double d = 0.25;
Show the value that will be stored in the variable on the left after the expression below is executed. If it shows an error, just type error.
f = i * 2;
24.0
What makes a constant in Java?
The keyword constant
Writing it in all capitals
The keyword static
The keyword final
The keyword final
What is the size in bytes for an int data type?
2
1
8
4
4
Given the following declarations:
int i = 15;
short s = 25;
long m = 50;
float f = 2.5f;
double d = 0.25;
Show the value that will be stored in the variable on the left after the expression below is executed. If it shows an error, just type error.
i = f * 2;
error
Given the following declarations:
int i = 15;
short s = 35;
long m = 50;
float f = 2.5f;
double d = 0.25;
Show the value that will be stored in the variable on the left after the expression below is executed. If it shows an error, just type error.
i = s + 3;
38
What can be stored in a char data type?
“name”
true
‘A’
“aa”
‘A’
Consider the following code segment.
System.out.print("Hello System.out.println");
System.out.print("!!!");
What is printed as a result of executing the code segment?
Nothing is printed because the text "System.out.println" cannot appear inside a print statement.
Hello !!!
Hello!!!
Hello System.out.println!!!
Hello System.out.println!!!
Consider the following code segment.
System.out.print("AP");
System.out.println();
System.out.println("CS");
System.out.print("A");
What is printed as a result of executing the code segment?
AP
CS
A
APCS
A
APCSA
AP CSA
AP
CS
A
Consider the following code segment.
System.out.print(*); // Line 1
System.out.print("*"); // Line 2
System.out.println(); // Line 3
System.out.println("*"); // Line 4
The code segment is intended to produce the following output, but may not work as intended.
**
*
Which line of code, if any, causes an error?
Line 4
Line 1
Line 3
The code segment works as intended
Line 2
Line 1
A code segment (not shown) is intended to determine the number of players whose average score in a game exceeds 0.5. A player’s average score is stored in avgScore, and the number of players who meet the criterion is stored in the variable count.
Which of the following pairs of declarations is most appropriate for the code segment described?
double avgScore;
boolean count;
double avgScore;
int count;
int avgScore;
int count;
double avgScore;
double count;
double avgScore;
int count;
In this course, to gather input for a user, we used a class from the Java APi named the ____________ class.
Scan
Input
Sc
Scanner
Scanner
Consider the following code segment.
System.out.print(5);
System.out.print(4);
System.out.print(3);
System.out.print(2);
System.out.print(1);
System.out.print(“ liftoff!”);
What is printed as a result of executing the code segment? (Choose the best answer.)
54321liftoff!
54321 Liftoff!
54321 liftoff!
5 4 3 2 1 liftoff!
54321 liftoff!
Consider the following code segment.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your name:" );
String name = input.nextLine();
System.out.print("Hello, " name);
}
}
What will be the output if the user enters Jack?
There is a compiler error because input is a keyword and we cannot use it as a variable
Hello, Jack
There is a compiler error because there is no concatenation operator to join "Hello" and the value of name.
Hello Jack
There is a compiler error because there is no concatenation operator to join "Hello" and the value of name.
Consider the following code segment.
System.out.print("One"); // Line 1
System.out.print("Two"); // Line 2
System.out.print("Three"); // Line 3
System.out.print("Four"); // Line 4
The code segment is intended to produce the following output, but does not work as intended.
OneTwo
ThreeFour
Which of the following changes can be made so that the code segment produces the intended output?
Changing print to println in lines 1, 2, 3, and 4
Changing print to println in line 3 only
Changing print to println in line 2 only
Changing print to println in line 1 only
Changing print to println in line 2 only
Consider the following code segment.
System.out.print(I do not fear computers. ); // Line 1
System.out.println(I fear the lack of them.); // Line 2
System.out.println(--Isaac Asimov); // Line 3
The code segment is intended to produce the following output but may not work as intended.
I do not fear computers. I fear the lack of them.
--Isaac Asimov
Which change, if any, can be made so that the code segment produces the intended output?
In lines 2 and 3, println should be changed to print.
No change is needed; the code segment works correctly as is.
In line 1, print should be changed to println.
In lines 1, 2, and 3, the text that appears in parentheses should be enclosed in quotation marks.
In lines 1, 2, and 3, the text that appears in parentheses should be enclosed in quotation marks.
Consider the following code segment.
System.out.print("*");
System.out.println("**");
System.out.println("***");
System.out.print("****");
What is printed as a result of executing the code segment?
*
**
*******
***
***
****
*
**
***
****
*
*****
****
***
***
****
What would be the output of the following code?
int x = 110;
int y = 3;
x %= y;
System.out.println(x);
2
3
107
5
2
x and y are declared as integer variables.
You are given the following expression:
x >= y
Which of these expressions is the opposite of the above expression and has valid syntax?
x =< y
x < y
There’s not enough information to determine this.
x == y
x <= y
x < y
If an int variable weight currently holds the value 150, what is its value after the following statement is executed?
weight -= 27;
If it shows an error, just type error.
123
What will be printed after the following statements are executed?
int length;
length = 11; // 11
length *= 3;
length *= length;
length /= 50;
System.out.println(length);
If it shows an error, just type error.
21
Consider the following static method.
public static int calculate(int x)
{
x = x + x;
x = x + x;
x = x + x;
return x;
}
Which of the following can be used to replace the body of calculate so that the modified version of calculate will return the same result as the original version for all x?
return 2 * x;
return 4 * x;
return 8 * x;
return 3 * calculate(x);
return x + calculate(x - 1);
return 8 * x;
Consider the following code segment.
int a = 5;
int b = 8;
int c = 3;
System.out.println(a + b / c * 2);
What is printed as a result of executing this code?
14
8
6
2
9
9
Consider the following code segment.
int x = 5;
x += 6 * 2;
x -= 3 / 2;
What value is stored in x after the code segment executes?
-1.5
9
16
1
15.5
16
Consider the following code segment.
System.out.print(I do not fear computers. ); // Line 1
System.out.println(I fear the lack of them.); // Line 2
System.out.println(--Isaac Asimov); // Line 3
The code segment is intended to produce the following output but may not work as intended.
I do not fear computers. I fear the lack of them.
--Isaac Asimov
Which change, if any, can be made so that the code segment produces the intended output?
No change is needed; the code segment works correctly as is.
In lines 2 and 3, println should be changed to print.
In lines 1, 2, and 3, the text that appears in parentheses should be enclosed in quotation marks.
In line 1, print should be changed to println.
The statement System.out.println() should be inserted between lines 2 and 3.
In lines 1, 2, and 3, the text that appears in parentheses should be enclosed in quotation marks.
Consider the following code segment.
System.out.print("One"); // Line 1
System.out.print("Two"); // Line 2
System.out.print("Three"); // Line 3
System.out.print("Four"); // Line 4
The code segment is intended to produce the following output, but does not work as intended.
OneTwo
ThreeFour
Which of the following changes can be made so that the code segment produces the intended output?
Changing print to println in line 2 only
Changing print to println in lines 1, 2, 3, and 4
Changing print to println in line 3 only
Changing print to println in line 1 only
Changing print to println in lines 2 and 3 only
Changing print to println in line 2 only
The code segment below is intended to calculate the circumference c of a circle with the diameter d of 1.5. The circumference of a circle is equal to its diameter times pi.
/* missing declarations */
c = pi * d;
Which of the following variable declarations are most appropriate to replace / missing declarations / in this code segment?
final int pi = 3.14159;
int d = 1.5;
int c;
double pi = 3.14159;
double d = 1.5;
final double c = 0.0;
int pi = 3.14159;
int d = 1.5;
final int c;
final double pi = 3.14159;
final double d = 1.5;
final double c = 0.0;
final double pi = 3.14159;
double d = 1.5;
double c;
final double pi = 3.14159;
double d = 1.5;
double c;
The following code segment is intended to interchange the values of the int variables x and y. Assume that x and y have been properly declared and initialized.
int temp = x;
/* missing code */
Which of the following can be used to replace /* missing code */ so that the code segment works as intended?
y = x;
temp = y;
x = y;
x = temp;
x = y;
y = temp;
y = x;
x = temp;
y = x;
temp = x;
x = y;
y = temp;
Consider the following code segment.
int a = 4;
int b = 5;
a++;
b++;
int c = a + b;
a -= 1;
System.out.println(a + c);
What is printed when the code segment is executed?
14
9
10
25
15
15
In the code segment below, assume that the int variable n has been properly declared and initialized. The code segment is intended to print a value that is 1 more than twice the value of n.
/* missing code */
System.out.print(result);
Which of the following can be used to replace / missing code / so that the code segment works as intended?
I.
int result = 2 * n;
result = result + 1;
II.
int result = n + 1;
result = result * 2;
III.
int result = (n + 1) * 2;
I only
I and III
III only
II only
II and III
I only
Consider the following code segment.
double firstDouble = 2.5;
int firstInt = 30;
int secondInt = 5;
double secondDouble = firstInt - secondInt / firstDouble + 2.5;
What value will be assigned to secondDouble when the code segment is executed?
12.5
29.0
30.5
25.5
5.0
30.5
Consider the following code segment, which is intended to find the average of two positive integers, x and y.
int x;
int y;
int sum = x + y;
double average = (double) (sum / 2);
Which of the following best describes the error, if any, in the code segment?
In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for even values of sum.
In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be greater than the expected result for even values of sum.
In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for odd values of sum.
In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be greater than the expected result for odd values of sum.
There is no error, and the code works as intended.
In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for odd values of sum.
Consider the following code segment.
double x = (int) (5.5 - 2.5);
double y = (int) 5.5 - 2.5;
System.out.println(x - y);
What is printed as a result of executing the code segment?
1.0
-1.0
0.0
-0.5
0.5
0.5
In the code segment below, assume that the int variables a and b have been properly declared and initialized.
int c = a;
int d = b;
c += 3;
d--;
double num = c;
num /= d;
Which of the following best describes the behavior of the code segment?
The code segment causes a runtime error in the last line of code because num is type double and d is type int.
The code segment stores the value of (a + 3) / (1 - b) in the variable num.
The code segment stores the value of (a + 3) / (b - 1) in the variable num.
The code segment stores the value of (a + 3) / b in the variable num.
The code segment stores the value of (a + 3) / (b - 2) in the variable num.
The code segment stores the value of (a + 3) / (b - 1) in the variable num
Consider the following code segment.
num += num;
num *= num;
Assume that num has been previously declared and initialized to contain an integer value. Which of the following best describes the behavior of the code segment?
The value of num is the square its original value.
It cannot be determined without knowing the initial value of num.
The value of num is two times the square of its original value.
The value of num is the square of twice its original value.
The value of num is two times its original value.
The value of num is the square of twice its original value.
Consider the following code segment, which is intended to print the digits of the two-digit int number num in reverse order. For example, if num has the value 75, the code segment should print 57. Assume that num has been properly declared and initialized.
/* missing code */
System.out.print(onesDigit);
System.out.print(tensDigit);
Which of the following can be used to replace /* missing code */ so that the code segment works as intended?
int onesDigit = num / 100;
int tensDigit = num % 100;
int onesDigit = 10 / num;
int tensDigit = 10 % num;
int onesDigit = num / 10;
int tensDigit = num % 10;
int onesDigit = num % 100;
int tensDigit = num / 100;
int onesDigit = num % 10;
int tensDigit = num / 10;
int onesDigit = num % 10;
int tensDigit = num / 10;
Which of the following expressions evaluate to 7 ?
I. 9 + 10 % 12
II. (9 + 10) % 12
III. 9 – 2 % 12
I and III
II and III
I only
II only
I, II, and III
II and III
Which of the following statements stores the value 3 in x?
int x = 5 % 8;
int x = 7 / 4;
int x = 4 / 7;
int x = 7 / 3;
int x = 8 % 5;
int x = 8 % 5;
Which of the following expressions evaluate to 3.5 ?
I. (double) 2 / 4 + 3
II. (double) (2 / 4) + 3
III. (double) (2 / 4 + 3)
I and II only
I only
III only
II and III only
I, II, and III
I only
Consider the following code segment, where k and count are properly declared and initialized int variables.
k++;
k++;
count++;
k--;
count++;
k--;
Which of the following best describes the behavior of the code segment?
The code segment leaves k unchanged and increases count by 2.
The code segment increases both k and count by 2.
The code segment leaves both k and count unchanged.
The code segment increases k by 4 and count by 2.
The code segment increases k by 2 and leaves count unchanged.
The code segment leaves k unchanged and increases count by 2.