Console Output

Practice Questions

Computer Science › Console Output

Page 1 of 2
10 of 12
1

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.

2

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);

3

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);

}

}

4

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);

}

5

Consider the following code:

Q3

What is the output of the method call mystery("Green eggs and ham")

6

Which of the following blocks of code will output an evenly-spaced 2D array (i.e. an evenly-spaced matrix)? For example, a simple evenly-spaced matrix is:

1 2 3

4 5 6

7 8 9

7

int a = 10, b = 5;

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

for(int j = 0; j < i % b; j++) {

System.out.print("* ");

}

System.out.println();

}

What will be the console output for the code above?

8

In Swift (iOS), give a description of what this method does.

func getDivisors(num: Int, divisor: Int) -> Bool {

var result: Bool = False

if (num % divisor == 0) {

result = True

}

println(result)

}

9

What is the output of this program?

arr = \["hello", "my", "name", "is"\]

for (int i = 0; i < arr.length - 1; i++) {

System.out.println(arr\[i\]);

}

System.out.println("Sandra");

10

What will the following code result in?

int main(){

int i = 7;

const int * ip = &i;

cout<< *ip << endl;

}

Page 1 of 2
Return to subject