IU Bloomington Evaluation Services and Testing

Indiana University
Bloomington Evaluation Services and Testing

QuizSite  Students
Quizzes · Tests · Evaluations · Surveys
Activity: Practice_Seven
Student: dgerman (Adrian German)
Instructor: dgerman
Course: J201 Section: 4285
Question 1.
What does the following program's output look like?
public class One {
    public static void main(String[] args) {
        int i = 0;
        for (i = 0; i < 10; i++) {
            System.out.println(" " + i);
        }
    }
} 

a line of 10 numbers
a column of 10 numbers
a line of 9 numbers
a column of 9 numbers


Question 2.
What does the following program's output look like?
public class Two {
    public static void main(String[] args) {
        int i = 0;
        for (i = 0; i < 10; i++) {
            System.out.print(" " + i);
        }
    }
} 

a line of 10 numbers
a column of 10 numbers
a line of 9 numbers
a column of 9 numbers


Question 3.
What does the following program's output look like?
public class Three {
    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        for (j = 0; j < 4; j++) {
          for (i = 0; i < 10; i++) {
              System.out.print(" " + i);
          }
        }
    }
} 

4 lines of 10 numbers each
10 lines of 4 numbers each
1 line with 40 numbers on it
40 lines of 1 number each


Question 4.
What does the following program's output look like?
public class Four {
    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        for (j = 0; j < 4; j++) {
          for (i = 0; i < 10; i++) {
              System.out.println(" " + i);
          }
        }
    }
} 

4 lines of 10 numbers each
10 lines of 4 numbers each
1 line with 40 numbers on it
40 lines of 1 number each


Question 5.
What does the following program's output look like?
public class Five {
    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        for (j = 0; j < 4; j++) {
          for (i = 0; i < 10; i++) {
              System.out.print(" " + i);
          }
          System.out.println();
        }
    }
} 

4 lines of 10 numbers each
10 lines of 4 numbers each
1 line with 40 numbers on it
40 lines of 1 number each


Question 6.
What does the following program's output look like?
public class Six {
    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        for (j = 0; j < 10; j++) {
          for (i = 0; i < 4; i++) {
              System.out.print(" " + i);
          }
          System.out.println();
        }
    }
} 

4 lines of 10 numbers each
10 lines of 4 numbers each
1 line with 40 numbers on it
40 lines of 1 number each


Question 7.
What does the following program's output look like?
public class Seven {
    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        for (j = 0; j < 4; j++) {
          for (i = 0; i < 10; i++) {
              System.out.print(" " + j);
          }
          System.out.println();
        }
    }
} 

4 lines of 10 numbers each (which count the columns)
10 lines of 4 numbers each (which count the columns)
4 lines of 10 numbers each (that count the lines)
10 lines of 4 numbers each (that count the lines)


Question 8.
What does the following program's output look like?
public class Eight {
    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        for (i = 0; i < 4; i++) {
          for (j = 0; j < 10; j++) {
              System.out.print(" " + j);
          }
          System.out.println();
        }
    }
} 

4 lines of 10 numbers each (which count the columns)
10 lines of 4 numbers each (which count the columns)
4 lines of 10 numbers each (that count the lines)
10 lines of 4 numbers each (that count the lines)


Question 9.
What does the following program's output look like?
public class Nine {
    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        for (i = 0; i < 10; i++) {
          for (j = 0; j < 10; j++) {
              if (i == 0 || j == 0 || i == j) {
                System.out.print(" " + j);
              } else {
                System.out.print("  ");
              }
          }
          System.out.println();
        }
    }
} 

numbers organized in an "X" sign
numbers organized in a square (full of numbers)
numbers in the shape of an arrow
an empty square whose contour (border) is made out of numbers


Question 10.
What does the following program's output look like?
public class Ten {
    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        for (i = 0; i < 10; i++) {
          for (j = 0; j < 10; j++) {
              if ((i == j) || (i + j == 10)) {
                System.out.print(" " + j);
              } else {
                System.out.print("  ");
              }
          }
          System.out.println();
        }
    }
} 

numbers organized in an "X" sign
numbers organized in a square (full of numbers)
numbers in the shape of an arrow
an empty square whose contour (border) is made out of numbers


Question 11.
What does the following program's output look like?
public class Eleven {
    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        for (i = 0; i < 10; i++) {
          for (j = 0; j < 10; j++) {
              if ((i == j) && (i + j == 10)) {
                System.out.print(" " + j);
              } else {
                System.out.print("  ");
              }
          }
          System.out.println();
        }
    }
} 

a long line of numbers
a long column of numbers
a single number will be printed, the number 5
numbers organized in a pattern other than those listed above


Question 12.
Which one of the for loops presented below is NOT equivalent to this while loop:
i = 0;
sum = 0;
while (i < 100) {
  sum += i;
  i += 2;
} 
Assume that i and sum have been declared already.

for (i = sum = 0; i < 100; sum += i) i += 2;
for (i = sum = 0; i < 100; i += 2) sum += i;
for (i = sum = 0; i < 100; sum += i, i += 2) ;
for (i = 0, sum = 0; i < 100; i += 2) sum += i;
for (i = sum = 0; (i += 2) < 100 ; ) sum += i;


Question 13.
Describe in a few words what the loop above does.

calculates the sum of all even integers in the interval [0, 100]
calculates the sum of all odd integers in the interval [0, 100]
calculates the sum of all odd integers in the interval [0, 100)
calculates the sum of all even integers in the interval [0, 100)
none of the above, but i does take 100 different values


Question 14.
Assume two integer variables n and m. Write a boolean expression that reads like this:
m is greater than n or (m + n) is divisible by 19
Hint: assume m has a value of 1 and n a value of 1899 for testing.

(m > n) || (m + n % 19) == 0
(m > n) || (m + n % 19 == 0)
(m > n) || m + n % 19 == 0
(m > n) || (m + n) % 19 == 0
none of the above


Question 15.
Write a statement that increments a variable i by 3.

i - 3;
i + 3;
i += 3;
i + 3 = i;
i =+ 3;


Question 16.
The price of some item is stored in a double variable named cost. The sales tax on this item is 5 percent. Which of the following statements will increase the value in cost by the sales tax?

cost * .05;
cost + cost * .05;
cost = cost * .05;
cost = cost + cost * .05;
cost = Integer.parseInt(cost * .05);


Question 17.
The expression 2 + 3 * 4 - 5 evaluates to:

19
15
9
-1
-5


Question 18.
The expression 5 - 6 - 7 evaluates to:

6
-8
8
-6
none of the above


Question 19.
The expression 5 - (6 - 7) evaluates to:

6
-8
8
-6
none of the above


Question 20.
The expression 3 - 2 / 5 evaluates to:

0.2
2.6
0
1
3


Question 21.
Consider the following program fragment:
System.out.println(2 - 4 / 3); 
What is it printing?

-1.3
1
0.7
1.3
0.66


Question 22.
The expression "age = " + 20 evaluates to:

"age = + 20"
"age=20"
"age = 20"
20
the expression contains a syntax error


Question 23.
The user runs a program containing the following fragment and then types 37 at the prompt.
ConsoleReader c = new ConsoleReader(System.in);
System.out.print("Enter the weight in ounces: ");
int weight = c.readInt();
int pounds = weight / 16,
    ounces = weight % 16;
System.out.println(pounds + " pounds, " + ounces + " ounces.");
What is printed by the last output statement?

2 pounds, 5 ounces.
5 pounds, 2 ounces.
weight / 16 pounds, weight % 16 ounces.
"2 pounds, 0 ounces."
nothing is printed; the code contains a syntax error


Question 24.
A program creates two different objects of type String labeled with the names quitCommand and userInput. Which expression would you use to determine if they spell the same (and have the same capitalization)?

Please check all that apply.


quitCommand = userInput
quitCommand == userInput
quitCommand.equals(userInput)
equals(quitCommand, userInput)
userInput.equals(quitCommand)


Question 25.
The grocery store has run out of all change except nickels. After paying for some items, you should receive $5.35 in change.
double change = 5.35;
System.out.println("You get " + ??? + " nickels.");
Which of the following expressions should you use to replace the ??? in the program fragment above so that the correct number of nickels is computed?

Math.round(change / 5)
change / 5
(change * 100) / 5
Math.round(change) * 100 / 5
Math.round(change * 100) / 5


Question 26.
Consider the following program fragment:
int x = 3, y = 5;
x = x + 1; y = x; x = x - 1;
System.out.println("x = " + x + ", y = " + y);
What is printed by the last statement in the program?

x = 3, y = 5
x = 3, y = 3
x = 4, y = 4
x = 3, y = 4
none of the above


Question 27.
The expression 69 % 11 evaluates to:

6
0.69
60
3
0.5454545454545454


Question 28.
Given
String vegetable;
vegetable = "red pepper";
What does vegetable.length() evaluate to?

9
10
2
12
3


Question 29.
Given
String pet;
pet = "unicorn";
What does pet.substring(1, 4) evaluate to?

"uni"
"unic"
"nic"
"nico"
none of the above


Question 30.
The user runs a program containing the following fragment and then types sara at the prompt.
ConsoleReader c = new ConsoleReader(System.in);
System.out.print("Please enter your name: ");
String name = c.readLine();
name.toUpperCase();
System.out.println("Hello " + name + "!");
What is printed by the last output statement?

Hello SARA !
Hello Sara!
Hello sara!
Hello SARA!
none of the above


Question 31.
Given
String vehicle = "snowmobile";
Which of the following expressions is not of type String.

Check all that apply.


vehicle
"vehicle"
vehicle.substring(0, 5)
vehicle.length()
vehicle + vehicle.length()


Question 32.
At a certain university, if a student drops a class on or before September 20 (i.e. 9/20), then no record of the course appears on the student's transcript. A grade of W appears if the withdrawal takes place on or before October 15 (i.e. 10/15), and a grade of F or I is recorded otherwise.

Assume that the month and day that a student withdraws are stored in two separate int variables called month and day. Select the condition that should be inserted into the following if statement

if ( /* What goes here? */ )
   System.out.println("Assign a grade of W.");
so that the message will be printed if the student is to receive a grade of W.

(month >= 9 && month <= 10) && (day <= 15 || day > 20)
(month == 9 && day > 20) || (month == 10 && day <= 15)
(month >= 9) && (day > 20 || day <= 15)
(month == 9 && day > 20) && (month == 10 && day <= 15)
(month == 9 || month == 10) && (15 < day && day <= 20)


Question 33.
Given
String book = "Computing Concepts with Java";
Which of the following expressions will not produce an error?

book - "Computing"
book.substring(20, 30)
book.substring(0, 10).toUpperCase()
book.capitalize()
book.length().substring(0, 10)


Question 34.
A relational operator tests whether or not some relation holds for its two operands. For example, < is a relational operator that evaluates to true if the first operand is numerically smaller than the second operand, and false otherwise. Which of the following is a relational operator in Java?

=
!<
!=
=>
+


Question 35.
Which of the following expressions is logically equivalent to (x<= y)? Check all that apply.

(y >= x)
(x < y) && (x == y)
(x == y) || (x < y)
(x - y) <= 0
(x <= y) && (x == x)


Question 36.
Consider the following code fragment:
int t = 5;
if (t >= 3)
   System.out.print("Wicked ");
if (t >= 5)
   System.out.print("Witch ");
if (t >= 7)
   System.out.print("of the West");
What gets printed when you run it?

Wicked
Wicked Witch
Wicked Witch of the West
Witch of the West
Witch


Question 37.
Assume that expr1 evaluates to true, expr2 evaluates to false, and expr3 evaluates to true. Which of the following expressions evaluates to true? Check all that apply.

expr1 && expr2
expr1 && expr3
expr1 || expr3
expr3 || (expr1 && expr2)
(expr3 ||expr1) && expr2


Question 38.
Which of the following code fragments is logically equivalent to this one? Check all that apply. More than one answer may be correct.
if (x > y) z = z + 1; else z = z + 2;

if (x > y) z = z + 1; if (x <= y) z = z + 2;
if (!(x > y)) z = z + 2; else z = z + 1;
z = z + 1; if (x <= y) z = z + 1;
if (x > y) z = z + 1; else if (x <= y) z = z + 2;
z = z + 2; if (!(x > y)) z = z - 1;


Question 39.
What does the following program fragment print? Read carefully.
String s = "dogfood", t = "dormfood";
if (!s.equals(t))
   System.out.print("You could've fooled me! ");
else
   System.out.print("Figures. ");
   System.out.print("I'm outta here!");

You could've fooled me! Figures.
Figures. I'm outta here!
You could've fooled me!
I'm outta here!
You could've fooled me! I'm outta here!


Click to submit your answers.   Help


This activity is available between these dates and times: 07.08.10:00-08.11.19:15
It can be submitted a total of 30 times.
If it is a scored activity, the highest non-zero score will be counted.


Screen: TAKE_QUIZ
Last updated: Thu Mar 8 13:40:07 2001
URL: https://www.indiana.edu/~best/cgi-bin/quizsite/students.pl
Comments: best@indiana.edu
Copyright 2000, BEST & The Trustees of Indiana University