Console Output

Help Questions

AP Computer Science A › Console Output

Questions 1 - 10
1

LOGIC WITH 2-D ARRAYS

Given:

int\[\]\[\] myArray = { {1, 2},

{3, 4} };

What would the following statement print out to the console?

System.out.print(myArray\[1\]\[1\] + 10);

14

12

11

13

Explanation

Before anything is printed out into the console, the following is first evaluated:

myArray\[1\]\[1\] + 10

myArray\[1\]\[1\] is referring to the item that is in row=1 and column=1 of myArray. Taking note that arrays start with row 0 and column 0, we see that the item in row 1 column 1 is the number 4. Now we have the following: 4+10. This evaluates to 14. Therefore, the Java print statement will print out the number 14 on the console.

2

Does the code compile and if yes, what is the the output?

public class Test

{

public static void main ( String \[\] args )

{

int x = 2;

if (x = 2)

System.out.println("Good job.");

else

System.out.println("Bad job);

}

}

No, it doesn't compile.

Yes it compiles and it prints good job.

Yes it compiles and it prints bad job.

Explanation

It doesn't compile. It is supposed to be x == 2 within the if statement not x = 2. The if statement checks for booleans (only true or false) and cannot convert an integer into a boolean to meet that check.

3

LOGIC WITH 2-D ARRAYS

Given:

int\[\]\[\] myArray = { {1, 2},

{3, 4} };

What would the following statement print out to the console?

System.out.print(myArray\[1\]\[1\] + 10);

14

12

11

13

Explanation

Before anything is printed out into the console, the following is first evaluated:

myArray\[1\]\[1\] + 10

myArray\[1\]\[1\] is referring to the item that is in row=1 and column=1 of myArray. Taking note that arrays start with row 0 and column 0, we see that the item in row 1 column 1 is the number 4. Now we have the following: 4+10. This evaluates to 14. Therefore, the Java print statement will print out the number 14 on the console.

4

Does the code compile and if yes, what is the the output?

public class Test

{

public static void main ( String \[\] args )

{

int x = 2;

if (x = 2)

System.out.println("Good job.");

else

System.out.println("Bad job);

}

}

No, it doesn't compile.

Yes it compiles and it prints good job.

Yes it compiles and it prints bad job.

Explanation

It doesn't compile. It is supposed to be x == 2 within the if statement not x = 2. The if statement checks for booleans (only true or false) and cannot convert an integer into a boolean to meet that check.

5

Does the code compile and if yes, what is the the output?

public class Test

{

public static void main ( String \[\] args )

{

int x = 2;

if (x = 2)

System.out.println("Good job.");

else

System.out.println("Bad job);

}

}

No, it doesn't compile.

Yes it compiles and it prints good job.

Yes it compiles and it prints bad job.

Explanation

It doesn't compile. It is supposed to be x == 2 within the if statement not x = 2. The if statement checks for booleans (only true or false) and cannot convert an integer into a boolean to meet that check.

6

Consider the following code:

for(int i = 1; i <= 10; i++) {

for(int j = 0; j < (20 - i * 2) / 2; j++) {

System.out.print(" ");

}

for(int j = 0; j < i * 2; j++) {

System.out.print("*");

}

for(int j = 0; j < (20 - i * 2) / 2; j++) {

System.out.print(" ");

}

System.out.println();

}

Describe the output of the code above.

An arrow pointing upward, having a base 20 characters wide and a top that is 2 characters wide.

An arrow pointing upward, having a base 20 characters wide and a top that is 1 character wide.

An arrow pointing downward, having a base 20 characters wide and a top that is 2 characters wide.

A right-facing arrow with large base of 20 characters in size and a point that is 2 characters wide.

A left-facing arrow with a large base of 20 characters in size and a point that is 1 character wide.

Explanation

This code is best explained by analyzing it directly. Comments will be provided in bold below:

// The loop runs from 1 to 10

for(int i = 1; i <= 10; i++) {

// First, you run through 0 to some number

// That number is computed by taking the current value of i, doubling it,

// subtracting that from 20, then dividing by 2.

// So, consider the following values for i:

// 0: 20 - 0 = 20; Divided by 2: 10

// 1: 20 - 2 = 18; Divided by 2: 9

// 2: 20 - 2 = 16; Divided by 2: 8

// etc...

// Thus, you will output single spaces that number of times (10, 9, 8...)

for(int j = 0; j < (20 - i * 2) / 2; j++) {

System.out.print(" ");

}

// Next, you will output an asterix i * 2 number of times. Thus, your

// smallest value is 2 and your largest 20.

for(int j = 0; j < i * 2; j++) {

System.out.print("*");

}

// This is just a repeat of the same loop from above

for(int j = 0; j < (20 - i * 2) / 2; j++) {

System.out.print(" ");

}

// This bumps us down to the next line

System.out.println();

}

Thus, you have output that basically has even padding on the left and right, with an increasing number of asterix characters (2, 4, 6, etc). This is an upward-facing arrow, having a point of width 2 and a base of width 20.

7

Consider the following code:

for(int i = 1; i <= 10; i++) {

for(int j = 0; j < (20 - i * 2) / 2; j++) {

System.out.print(" ");

}

for(int j = 0; j < i * 2; j++) {

System.out.print("*");

}

for(int j = 0; j < (20 - i * 2) / 2; j++) {

System.out.print(" ");

}

System.out.println();

}

Describe the output of the code above.

An arrow pointing upward, having a base 20 characters wide and a top that is 2 characters wide.

An arrow pointing upward, having a base 20 characters wide and a top that is 1 character wide.

An arrow pointing downward, having a base 20 characters wide and a top that is 2 characters wide.

A right-facing arrow with large base of 20 characters in size and a point that is 2 characters wide.

A left-facing arrow with a large base of 20 characters in size and a point that is 1 character wide.

Explanation

This code is best explained by analyzing it directly. Comments will be provided in bold below:

// The loop runs from 1 to 10

for(int i = 1; i <= 10; i++) {

// First, you run through 0 to some number

// That number is computed by taking the current value of i, doubling it,

// subtracting that from 20, then dividing by 2.

// So, consider the following values for i:

// 0: 20 - 0 = 20; Divided by 2: 10

// 1: 20 - 2 = 18; Divided by 2: 9

// 2: 20 - 2 = 16; Divided by 2: 8

// etc...

// Thus, you will output single spaces that number of times (10, 9, 8...)

for(int j = 0; j < (20 - i * 2) / 2; j++) {

System.out.print(" ");

}

// Next, you will output an asterix i * 2 number of times. Thus, your

// smallest value is 2 and your largest 20.

for(int j = 0; j < i * 2; j++) {

System.out.print("*");

}

// This is just a repeat of the same loop from above

for(int j = 0; j < (20 - i * 2) / 2; j++) {

System.out.print(" ");

}

// This bumps us down to the next line

System.out.println();

}

Thus, you have output that basically has even padding on the left and right, with an increasing number of asterix characters (2, 4, 6, etc). This is an upward-facing arrow, having a point of width 2 and a base of width 20.

8

Consider the following code:

for(int i = 1; i <= 10; i++) {

for(int j = 0; j < (20 - i * 2) / 2; j++) {

System.out.print(" ");

}

for(int j = 0; j < i * 2; j++) {

System.out.print("*");

}

for(int j = 0; j < (20 - i * 2) / 2; j++) {

System.out.print(" ");

}

System.out.println();

}

Describe the output of the code above.

An arrow pointing upward, having a base 20 characters wide and a top that is 2 characters wide.

An arrow pointing upward, having a base 20 characters wide and a top that is 1 character wide.

An arrow pointing downward, having a base 20 characters wide and a top that is 2 characters wide.

A right-facing arrow with large base of 20 characters in size and a point that is 2 characters wide.

A left-facing arrow with a large base of 20 characters in size and a point that is 1 character wide.

Explanation

This code is best explained by analyzing it directly. Comments will be provided in bold below:

// The loop runs from 1 to 10

for(int i = 1; i <= 10; i++) {

// First, you run through 0 to some number

// That number is computed by taking the current value of i, doubling it,

// subtracting that from 20, then dividing by 2.

// So, consider the following values for i:

// 0: 20 - 0 = 20; Divided by 2: 10

// 1: 20 - 2 = 18; Divided by 2: 9

// 2: 20 - 2 = 16; Divided by 2: 8

// etc...

// Thus, you will output single spaces that number of times (10, 9, 8...)

for(int j = 0; j < (20 - i * 2) / 2; j++) {

System.out.print(" ");

}

// Next, you will output an asterix i * 2 number of times. Thus, your

// smallest value is 2 and your largest 20.

for(int j = 0; j < i * 2; j++) {

System.out.print("*");

}

// This is just a repeat of the same loop from above

for(int j = 0; j < (20 - i * 2) / 2; j++) {

System.out.print(" ");

}

// This bumps us down to the next line

System.out.println();

}

Thus, you have output that basically has even padding on the left and right, with an increasing number of asterix characters (2, 4, 6, etc). This is an upward-facing arrow, having a point of width 2 and a base of width 20.

9

LOGIC WITH 2-D ARRAYS

Given:

int\[\]\[\] myArray = { {1, 2},

{3, 4} };

What would the following statement print out to the console?

System.out.print(myArray\[1\]\[1\] + 10);

14

12

11

13

Explanation

Before anything is printed out into the console, the following is first evaluated:

myArray\[1\]\[1\] + 10

myArray\[1\]\[1\] is referring to the item that is in row=1 and column=1 of myArray. Taking note that arrays start with row 0 and column 0, we see that the item in row 1 column 1 is the number 4. Now we have the following: 4+10. This evaluates to 14. Therefore, the Java print statement will print out the number 14 on the console.

10

True or False.

The output of this code snippet will be "Hello, I'm hungry!"

public static void meHungry() {

String hungry = "hungry";

String iAm = "I'm";

String hello = "Hello";

String message = "";

if (hungry != null) {

message += hungry;

}

if (hello != null && iAm != null) {

message = hello + iAm + hungry;

}

System.out.println(message);

}

False

True

Explanation

The message that is printed out is "Hello I'm hungry"

Notice there is no punctuation in the message. The code does not add punctuation to the message, but prints the words out in the same order as the phrase in the prompt. Be mindful of what's actually happening in the code.

Page 1 of 4
Return to subject