Analyze the following code: public class Test { public static void main (String
ID: 3854449 • Letter: A
Question
Analyze the following code:
public class Test {
public static void main (String args[]) {
int i = 0;
for (i = 0; i < 10; i++);
System.out.println(i + 4);
}
}
Question 1 options:
The program compiles despite the semicolon (;) on the for loop line, and displays 4
The program compiles despite the semicolon (;) on the for loop line, and displays 14.
The program has a compile error because of the semicolon (;) on the for loop line.
The program has a runtime error because of the semicolon (;) on the for loop line.
Analyze the following code.
int count = 0;
while (count < 100) {
// Point A
System.out.println("Welcome to Java!");
count++;
// Point B
}
// Point C
Question 2 options:
count < 100 is always false at Point A
count < 100 is always true at Point B
count < 100 is always false at Point C
count < 100 is always false at Point B
Question 3 (1 point)
What is i after the following for loop is finished?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
Question 3 options:
10
0
9
11
Question 4 (1 point)
Which of the following expression yields an integer between 0 and 100, inclusive?
Question 4 options:
(int)(Math.random() * 100 + 1)
(int)(Math.random() * 101)
(int)(Math.random() * 100)
(int)(Math.random() * 100) + 1
The following code displays ___________.
double temperature = 50;
if (temperature >= 100)
System.out.println("too hot");
else if (temperature <= 40)
System.out.println("too cold");
else
System.out.println("just right");
Question 6 options:
too hot
too cold
too hot too cold just right
just right
What is the output for y?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
System.out.println(y);
What is Math.floor(3.6)?
Question 8 options:
5.0
3
3.5
4
The program compiles despite the semicolon (;) on the for loop line, and displays 4
The program compiles despite the semicolon (;) on the for loop line, and displays 14.
The program has a compile error because of the semicolon (;) on the for loop line.
The program has a runtime error because of the semicolon (;) on the for loop line.
Analyze the following code.
int count = 0;
while (count < 100) {
// Point A
System.out.println("Welcome to Java!");
count++;
// Point B
}
// Point C
Question 2 options:
count < 100 is always false at Point A
count < 100 is always true at Point B
count < 100 is always false at Point C
count < 100 is always false at Point B
Question 3 (1 point)
What is i after the following for loop is finished?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
Question 3 options:
10
0
9
11
Question 4 (1 point)
Which of the following expression yields an integer between 0 and 100, inclusive?
Question 4 options:
(int)(Math.random() * 100 + 1)
(int)(Math.random() * 101)
(int)(Math.random() * 100)
(int)(Math.random() * 100) + 1
The following code displays ___________.
double temperature = 50;
if (temperature >= 100)
System.out.println("too hot");
else if (temperature <= 40)
System.out.println("too cold");
else
System.out.println("just right");
Question 6 options:
too hot
too cold
too hot too cold just right
just right
What is the output for y?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
System.out.println(y);
What is Math.floor(3.6)?
Question 8 options:
5.0
3
3.5
4
Explanation / Answer
NOTE:
The questions you have pasted are jumbled. So i tried my best to organize and answer them. Please let me know if you face any issues.
Q.1) Analyze the following code:
public class Test {
public static void main (String args[]) {
int i = 0;
for (i = 0; i < 10; i++);
System.out.println(i + 4);
}
}
Ans) The program compiles despite the semicolon (;) on the for loop line, and displays 14.
Explanation:
As the for loop has a semi colon it runs and after the for loop the value of i will be 10. When we are printing i+4 in the next statement it would i+4 i.e. 10+14 = 14
Q.2) Analyze the following code.
int count = 0;
while (count < 100) {
// Point A
System.out.println("Welcome to Java!");
count++;
// Point B
}
// Point C
Ans) count < 100 is always true at Point B
Explanation:
The reason is it enters the loop and executes until the value of count variable is less than 100. That means at Point B the value of count < 100. Once the loop terminates the value of count is 100.
Q.3) What is i after the following for loop is finished?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
Ans) 10
Explanation: Ideally when we check the value of i it will throw an error, because i is declared locally inside the for loop. So it will not exist once the loop terminates. But considering the value of i exists it would be 10 because thats where the loop terminates as you can see loop continues until value of i is less than 10 and breaks when i reaches 10.
Q.4) Which of the following expression yields an integer between 0 and 100, inclusive?
Ans) (int)(Math.random() * 101)
Explanation: Since the value is 101 it generates all integers between 0 and 100 inclusive.
Q.6) The following code displays ___________.
double temperature = 50;
if (temperature >= 100)
System.out.println("too hot");
else if (temperature <= 40)
System.out.println("too cold");
else
System.out.println("just right");
Ans) just right
Explanation:
The value of temperature variable is 50 and will not match first if condition (i.e. >=100) and the second if condition(i.e. <=40). So it falls back to else statement and prints "just right"
Q.7) What is the output for y?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
System.out.println(y);
Ans) 45
Explanation:
The variable y will have the i value added every time. That is i values will be 0,1,2,3,4,5,6,7,8,9 upto 10 which gets added to y. Hence the sum 45.
Q.8) What is Math.floor(3.6)?
Ans) 3.0
Explanation:
Math.floor returns the largest value which is less than or equal to the argument passed to the function and is equal to a mathematical integer. Hence the value 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.