Java Programming College Quiz 1 Questions

Question 1

Which of the following keywords is useful for skipping to the next iteration of a loop?

Select one:

  • a. while
  • b. break
  • c. continue
  • d. do
  • e. switch

The correct answer is: continue


Question 2

Consider the following block of Java code. How many times will it output "Hello"?

1
2
3
4
for (int i = 1; i < 10; i--)
{
    System.out.println("Hello");
}

Select one:

  • a. 0
  • b. 9
  • c. 1
  • d. 10
  • e. Way too many!

Notice the update, "i--". "i" starts less than 10 and just keeps getting smaller. (The loop is technically not infinite. "i" will eventually reach the most-nega tive value that "int" can represent, and then it wil l wrap around to the largest possible "int", ending the loop.) The correct answers are: 1, Way too many!


Question 3

Consider the following class definition. Which variables can be used in the mi ssing "println" expression on line 20 ?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class PrintStuff
  {
      public static void main()
      {
          {
              int i = -1;
              System.out.println(_____);
          }
         int j = 1;
         for (j = 0; j < 10; j++) {
             System.out.println(_____);
         }
         {
                 int k;
                 for (k = 0; k < 10; k++) {
                System.out.println(_____);
             }
         }
        System.out.println(_____);
     }
 }

Select one:

  • a. Only "j"
  • b. Only "i"
  • c. "i" and "j"
  • d. "j" and "k"
  • e. Only "k"

"i" and "k" are no longer in scope. Only "j" is stil l in scope. The correct answer is: Only "j"


Question 4

Which of the following types is NOT a primitive type?

Select one:

  • a. short
  • b. boolean
  • c. String
  • d. char
  • e. double

The correct answer is: String


Question 5

Consider the following Java program:

1
2
3
4
5
6
 public class HelloWorld {
     // My first program!
     public static void main(String[] args) {
         System.out.println("Hello, World!");
     }
 }

What starts on line 4? Select one:

  • a. a comment
  • b. a method (subroutine) definition
  • c. a class definition
  • d. a statement
  • e. a variable declaration

The correct answers are: a statement, a class definition


Question 6 Incorrect

Consider the following line of Java code.

System.out.println("Hello, World!"); "Hello ,World" is which of the following? Select one:

a. a class b. a method (subroutine) c. an object d. a parameter e. a statement

'"Hello, World!"' is a parameter passed to the meth od "println". The correct answer is: a parameter


Question 7

In a for loop, how many times does the continuation condition run?

Select one:

  • a. Exactly once.
  • b. At least once, at the end of each iteration.
  • c. Zero or more times, at the beginning of each iteration.
  • d. At least once, at the beginning of each iteration.
  • e. Zero or more times, at the end of each iteration .

The correct answer is: At least once, at the beginning of each iteration.


Question 8

Which one of the following is used in Java programming to handle asynchronous events ?

Select one:

  • a. protocols
  • b. pragmatics
  • c. reserved words
  • d. short circuits
  • e. event handlers

The correct answer is: event handlers


Question 9

Each of the individual tasks that a CPU is working on is called:

Select one:

  • a. a message
  • b. a program counter
  • c. a thread
  • d. an object
  • e. an address

The correct answer is: a thread


Question 10

What is the output of the following Java program?

1
2
3
4
5
6
7
8
class Sum {
    static int sum = 0;
    static void add(int i) { i++; }
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) add(sum);
        System.out.println(sum);
    }
}

Select one:

  • a. 100
  • b. 9
  • c. 10
  • d. 0
  • e. 45

The "add" method increments the parameter "i", which is a copy of the argument "sum". The value of "sum" never changes. The correct answer is: 0


Question 11

Assume "test" is a boolean variable. Which of the following expressions is equival ent to "test == false"?

Select one:

a. test b. !test c. test.equals(true) d. test = true

The correct answer is: !test


Question 12

In a for loop, how many times does the update run?

Select one:

  • a. At least once, at the end of each iteration.
  • b. Zero or more times, at the end of each iteration .
  • c. Zero or more times, at the beginning of each iteration.
  • d. Exactly once.
  • e. At least once, at the beginning of each iteration.

The correct answer is: Zero or more times, at the end of each iteration.


Question 13

Consider the following Java program:

1
2
3
4
5
6
public class HelloWorld {
     // My first program!
     public static void main(String[] args) {
         System.out.println("Hello, World!");
    }
}

What is on line 3? Select one:

  • a. a class definition
  • b. a method (subroutine) definition
  • c. a statement
  • d. a variable declaration
  • e. a comment

The correct answers are: a method (subroutine) definition , a class definition


Question 14

Consider the following Java program:

1
2
3
4
5
6
public class HelloWorld {
     // My first program!
     public static void main(String[] args) {
         System.out.println("Hello, World!");
     }
 }

What starts on line 1? Select one:

  • a. a comment
  • b. a class definition
  • c. a variable declaration
  • d. a statement
  • e. a method (subroutine) definition

The correct answer is: a class definition


Question 15

Consider the following Java method, which term best describes "static"?

1
2
3
public static void main(String[] args) {
    System.out.println("Hello, World!");
}

Select one:

  • a. actual parameter or argument
  • b. method call
  • c. formal parameter
  • d. modifier
  • e. return type

The correct answer is: modifier


Question 16

Which of the following can a class NOT be used for?

Select one:

  • a. a container for static methods (subroutines)
  • b. a container for static variables
  • c. a primitive type
  • d. a type for method parameters
  • e. a type for variables

The correct answer is: a primitive type


Question 17

Which of the following keywords is useful for loops that should always execute at least once?

Select one:

  • a. switch
  • b. while
  • c. continue
  • d. break
  • e. do

The correct answer is: do


Question 18

Which of the following is NOT an effective strategy when your program does not work?

Select one:

  • a. Make random changes to code that you do not unders tand until it accidentally works.
  • b. Add debugging statements to output information ab out the state of your program while it runs.
  • c. Check each error message generated by the compiler or IDE.
  • d. Use a debugger to pause your program while it is running so you can check its state.
  • e. Read through your code and figure out what it does step by step.

The correct answer is: Make random changes to code that you do not understand until it accidentally works.


Question 19

Consider the following Java declaration and assignment statement.

float x = y; Which one of the following types is "y" NOT allowed to be? Select one: a. double b. long c. int d. short e. float

Java automatically converts values from short, int, and long to float. It does not automatically convert double to float. The correct answer is: double


Question 20

What is output by the following Java program?

1
2
3
4
5
6
7
class Compute {
    static int compute() { return 42; }
    static int compute(int i) { return i+1; }
    public static void main(String[] args) {
        System.out.println(compute(compute(0)));
    }
}

Select one: a. 1 b. 42 c. 2 d. 0 e. 43

The inner call to "compute" has an argument, so it us es the second method definition, which returns 0+1, or 1. This 1 becomes the argument to the outer ca ll, which then returns 1+1, or 2. The first definition of "compute" is never used. The correct answer is: 2

Related Posts

0 Comments

12345

    10