Program Correctness

Help Questions

AP Computer Science A › Program Correctness

Questions 1 - 10
1

Which of the following code excerpts would output "10"?

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

int num = 5;

num = (num > 0) ? 11: 10;

System.out.println(num);

int num = 5;

num = (num < 0) ? 10: 11;

System.out.println(num);

int num = 10;

num = (num > 0) ? 11: 12;

System.out.println(num);

None of the answers are correct.

Explanation

Each bit of code has something similar to this:

num = (boolean statement) ? X : Y;

The bit at the end with the ? and : is called a ternary operator. A ternary operator is a way of condensing an if-else statement. A ternary operator works like this:

<boolean statement> ? <do this if true> : <do this if false>

The correct answer is

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

Therefore, the ternary operator portion of code, when converted to an if-else, looks like this:

if (num > 0) {

num = 10;

else {

num = 11;

}

Because num is 5, which is greater than 0, it would go into the if, so num would then get 10. Then, num gets printed, which means 10 gets printed (the correct answer).

2

Which of the following code excerpts would output "10"?

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

int num = 5;

num = (num > 0) ? 11: 10;

System.out.println(num);

int num = 5;

num = (num < 0) ? 10: 11;

System.out.println(num);

int num = 10;

num = (num > 0) ? 11: 12;

System.out.println(num);

None of the answers are correct.

Explanation

Each bit of code has something similar to this:

num = (boolean statement) ? X : Y;

The bit at the end with the ? and : is called a ternary operator. A ternary operator is a way of condensing an if-else statement. A ternary operator works like this:

<boolean statement> ? <do this if true> : <do this if false>

The correct answer is

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

Therefore, the ternary operator portion of code, when converted to an if-else, looks like this:

if (num > 0) {

num = 10;

else {

num = 11;

}

Because num is 5, which is greater than 0, it would go into the if, so num would then get 10. Then, num gets printed, which means 10 gets printed (the correct answer).

3

Which of the following code excerpts would output "10"?

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

int num = 5;

num = (num > 0) ? 11: 10;

System.out.println(num);

int num = 5;

num = (num < 0) ? 10: 11;

System.out.println(num);

int num = 10;

num = (num > 0) ? 11: 12;

System.out.println(num);

None of the answers are correct.

Explanation

Each bit of code has something similar to this:

num = (boolean statement) ? X : Y;

The bit at the end with the ? and : is called a ternary operator. A ternary operator is a way of condensing an if-else statement. A ternary operator works like this:

<boolean statement> ? <do this if true> : <do this if false>

The correct answer is

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

Therefore, the ternary operator portion of code, when converted to an if-else, looks like this:

if (num > 0) {

num = 10;

else {

num = 11;

}

Because num is 5, which is greater than 0, it would go into the if, so num would then get 10. Then, num gets printed, which means 10 gets printed (the correct answer).

4

Which of the following code excerpts would output "10"?

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

int num = 5;

num = (num > 0) ? 11: 10;

System.out.println(num);

int num = 5;

num = (num < 0) ? 10: 11;

System.out.println(num);

int num = 10;

num = (num > 0) ? 11: 12;

System.out.println(num);

None of the answers are correct.

Explanation

Each bit of code has something similar to this:

num = (boolean statement) ? X : Y;

The bit at the end with the ? and : is called a ternary operator. A ternary operator is a way of condensing an if-else statement. A ternary operator works like this:

<boolean statement> ? <do this if true> : <do this if false>

The correct answer is

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

Therefore, the ternary operator portion of code, when converted to an if-else, looks like this:

if (num > 0) {

num = 10;

else {

num = 11;

}

Because num is 5, which is greater than 0, it would go into the if, so num would then get 10. Then, num gets printed, which means 10 gets printed (the correct answer).

5

Which of the following code excerpts would output "10"?

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

int num = 5;

num = (num > 0) ? 11: 10;

System.out.println(num);

int num = 5;

num = (num < 0) ? 10: 11;

System.out.println(num);

int num = 10;

num = (num > 0) ? 11: 12;

System.out.println(num);

None of the answers are correct.

Explanation

Each bit of code has something similar to this:

num = (boolean statement) ? X : Y;

The bit at the end with the ? and : is called a ternary operator. A ternary operator is a way of condensing an if-else statement. A ternary operator works like this:

<boolean statement> ? <do this if true> : <do this if false>

The correct answer is

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

Therefore, the ternary operator portion of code, when converted to an if-else, looks like this:

if (num > 0) {

num = 10;

else {

num = 11;

}

Because num is 5, which is greater than 0, it would go into the if, so num would then get 10. Then, num gets printed, which means 10 gets printed (the correct answer).

6

Which of the following code excerpts would output "10"?

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

int num = 5;

num = (num > 0) ? 11: 10;

System.out.println(num);

int num = 5;

num = (num < 0) ? 10: 11;

System.out.println(num);

int num = 10;

num = (num > 0) ? 11: 12;

System.out.println(num);

None of the answers are correct.

Explanation

Each bit of code has something similar to this:

num = (boolean statement) ? X : Y;

The bit at the end with the ? and : is called a ternary operator. A ternary operator is a way of condensing an if-else statement. A ternary operator works like this:

<boolean statement> ? <do this if true> : <do this if false>

The correct answer is

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

Therefore, the ternary operator portion of code, when converted to an if-else, looks like this:

if (num > 0) {

num = 10;

else {

num = 11;

}

Because num is 5, which is greater than 0, it would go into the if, so num would then get 10. Then, num gets printed, which means 10 gets printed (the correct answer).

7

Which of the following code excerpts would output "10"?

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

int num = 5;

num = (num > 0) ? 11: 10;

System.out.println(num);

int num = 5;

num = (num < 0) ? 10: 11;

System.out.println(num);

int num = 10;

num = (num > 0) ? 11: 12;

System.out.println(num);

None of the answers are correct.

Explanation

Each bit of code has something similar to this:

num = (boolean statement) ? X : Y;

The bit at the end with the ? and : is called a ternary operator. A ternary operator is a way of condensing an if-else statement. A ternary operator works like this:

<boolean statement> ? <do this if true> : <do this if false>

The correct answer is

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

Therefore, the ternary operator portion of code, when converted to an if-else, looks like this:

if (num > 0) {

num = 10;

else {

num = 11;

}

Because num is 5, which is greater than 0, it would go into the if, so num would then get 10. Then, num gets printed, which means 10 gets printed (the correct answer).

8

Which of the following code excerpts would output "10"?

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

int num = 5;

num = (num > 0) ? 11: 10;

System.out.println(num);

int num = 5;

num = (num < 0) ? 10: 11;

System.out.println(num);

int num = 10;

num = (num > 0) ? 11: 12;

System.out.println(num);

None of the answers are correct.

Explanation

Each bit of code has something similar to this:

num = (boolean statement) ? X : Y;

The bit at the end with the ? and : is called a ternary operator. A ternary operator is a way of condensing an if-else statement. A ternary operator works like this:

<boolean statement> ? <do this if true> : <do this if false>

The correct answer is

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

Therefore, the ternary operator portion of code, when converted to an if-else, looks like this:

if (num > 0) {

num = 10;

else {

num = 11;

}

Because num is 5, which is greater than 0, it would go into the if, so num would then get 10. Then, num gets printed, which means 10 gets printed (the correct answer).

9

Which of the following code excerpts would output "10"?

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

int num = 5;

num = (num > 0) ? 11: 10;

System.out.println(num);

int num = 5;

num = (num < 0) ? 10: 11;

System.out.println(num);

int num = 10;

num = (num > 0) ? 11: 12;

System.out.println(num);

None of the answers are correct.

Explanation

Each bit of code has something similar to this:

num = (boolean statement) ? X : Y;

The bit at the end with the ? and : is called a ternary operator. A ternary operator is a way of condensing an if-else statement. A ternary operator works like this:

<boolean statement> ? <do this if true> : <do this if false>

The correct answer is

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

Therefore, the ternary operator portion of code, when converted to an if-else, looks like this:

if (num > 0) {

num = 10;

else {

num = 11;

}

Because num is 5, which is greater than 0, it would go into the if, so num would then get 10. Then, num gets printed, which means 10 gets printed (the correct answer).

10

Which of the following code excerpts would output "10"?

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

int num = 5;

num = (num > 0) ? 11: 10;

System.out.println(num);

int num = 5;

num = (num < 0) ? 10: 11;

System.out.println(num);

int num = 10;

num = (num > 0) ? 11: 12;

System.out.println(num);

None of the answers are correct.

Explanation

Each bit of code has something similar to this:

num = (boolean statement) ? X : Y;

The bit at the end with the ? and : is called a ternary operator. A ternary operator is a way of condensing an if-else statement. A ternary operator works like this:

<boolean statement> ? <do this if true> : <do this if false>

The correct answer is

int num = 5;

num = (num > 0) ? 10: 11;

System.out.println(num);

Therefore, the ternary operator portion of code, when converted to an if-else, looks like this:

if (num > 0) {

num = 10;

else {

num = 11;

}

Because num is 5, which is greater than 0, it would go into the if, so num would then get 10. Then, num gets printed, which means 10 gets printed (the correct answer).

Page 1 of 4
Return to subject