Pages

Exam3-APCSA-17-18

This exam consists of 15 multiple choice questions and one coding exercise. For each multiple choice question there is at least one correct answer, and there might be more than one correct answer.

Instructions

  1. Write your last name in the upper right hand corner of each page of this exam.
  2. Below your last name, write the name you prefer to be called in class in parentheses.
  3. For each multiple choice question, circle the letter of the correct answer(s).
  4. Follow the instructions for the coding exercise that are provided in that section of the exam.
Each multiple choice question will be graded as follows.
  • Five points will be awarded if and only if all of the correct answers and none of the incorrect answers are circled.
  • Two points will be deducted for each correct answer that is not circled.
  • Three points will be deducted for each incorrect answer that is circled.
  • No more than five points will be deducted per question.

The coding exercise is worth 25 points. Partial credit will be awarded. The following grading scale, which is analogous to the scale use for grading programming assignments, will be used when scoring the coding exercise.

MarkPoints
++26
+25
✓+24
23
✓-21
-19
--17
00

Please, do your own work. And Don’t Panic!

(Several of these questions should look familiar to you!)

IMPORTANT NOTE:

Java’s + operator behaves in ways that are sometime unexpected.

  • If both operands are of type char, the values are converted to integers and the integers are added, yielding an integer.
  • If one operand is of type char and the other is of type String, then the character is converted to value of type String (where the converted value is a string consisting of the original character) and the two strings are concatenated, yielding another string.





Which of the following Java code snippets are examples of boolean expressions?

Assume:
  • The identifier args is the parameter of a main method of a Java program.
  • The identifiers a and b are names of variables of type String that have been initialized to values that are not null.
  • The identifier i is the name of a variable of type int that has been initialized.
  1. a = b
  2. a != b
  3. a.equals(b)
  4. true && false
  5. 3 % 2
  6. i < args.length

Which of the following primitive Java data types are used to represent integers (i.e. whole numbers)?

  1. byte
  2. short
  3. int
  4. long
  5. double
  6. float

The authors of Computer Science - An Interdisciplinary Approach formally define the idea of a data type as set of X and a set of Y, where X and Y are what?

  1. ones and zeros
  2. names and values
  3. values and operations
  4. operations and methods
  5. functions and parameters
  6. none of the above

Which of the following are not Java reserved words used to specify a primitive data type?

  1. String
  2. Double
  3. character
  4. Boolean
  5. integer
  6. int








Consider the following Java program:
public class Foo {
  public static void main(String[] args) {
    String x = args[0];
    String y = args[1];
    while (x != y && x.length() < 3) {
      x = x + y;
      System.out.print(x);
    }
  }
}

Which of the following statements about the program above are true?

  1. It will always produce an error when compiled.
  2. It will never produce an error when compiled.
  3. Whether it will produce a compile-time error depends on the value of args.length.
  4. Whether it will produce a compile-time error depends on the value of args[1].
  5. Whether it will produce a compile-time error depends on the name of the file containing the code.
  6. None of the above.

Which of the following statements about the program above are false?

  1. It will always produce a run-time error when executed.
  2. It will never produce a run-time error when executed.
  3. It will always terminate when executed if the value of args.length is greater than one
  4. It will never terminate when executed if x.length() and y.length() both evaluate to zero.
  5. Whether it will terminate depends on the values of args.length and args[0].
  6. Whether it will terminate depends on the values of args.length and args[1].

Assume the program above is stored in a file named Foo.java and the following commands are executed:

javac Foo.java 
java Foo 1 1

What output will be produced?

  1. 2
  2. 3
  3. 23
  4. 11
  5. 111
  6. 11111

The following program outputs the digits 5011. Why?

public class Plus {
  public static void main(String[] args) {
    System.out.print(1 + '1');
    System.out.print("1" + '1');
  }
}
  1. When converted to a string, the value of the number 2 is "50".
  2. When converted to an integer, the value of the character '1' is 50.
  3. When converted to an integer, the value of the character '1' is 49.
  4. When one operand is a character and the other is an integer, the + operator converts the character to an integer and then adds the integers, yielding an integer.
  5. When one operand is a character and the other is a string, the + operator concatenates values, yielding a string.
  6. The number 2 in decimal is represented by the digits 11 in binary.

Which statements about the following Java class are not true?

public class Foo {
  public static String bar(String Java) {
    return Java;
  }
  public static void main(String[] args) {
    String 1 = "Hello, ";
    String 2 = Foo.bar("Java");
    System.out.print(1 + 2);
  }
}
  1. Java and args are names of parameters.
  2. The code cannot be compiled, because Java is not a valid name for an identifier.
  3. The code cannot be compiled, because identifiers cannot begin with a number.
  4. The code cannot be compiled, because identifiers cannot contain digits.
  5. When executed, the program will print the string "Hello, Java".
  6. The argument passed to the method System.out.print is the string "Hello, Java".


Which of the following Java code snippets contain examples of literals?

  1. String s = "abc"
  2. int i = 0
  3. if (x > y) {
  4. int n;
  5. System.out.print(s);
  6. 3 % 2


Which of the following statements are true about the following line of Java code?

String a, b, c = "undefined";
  1. The string "undefined" cannot be used to initialize the variable c, because undefined is a reserved word.
  2. Three variables are declared and all three are initialized to the value "undefined".
  3. Three variables are declared but only one of them is initialized.
  4. The code will fail to compile, because only one variable can be declared at a time in a single declaration statement.
  5. The code will fail to compile, because variable initialization statements must follow variable declaration statements; the two types of statements cannot be combined.
  6. none of the above

For what values of a, b, and c does the following expression evaluate to true?

(a && b) && (!a || c)
  1. a is true; b is true; c is true;
  2. a is true; b is false; c is true;
  3. a is false; b is false; c is true;
  4. a is false; b is true; c is false;
  5. a is true; b is false; c is false;
  6. none of the above

Which of the following identifiers exemplify the coding style known as camel case?

  1. cc
  2. CC
  3. CamelCase
  4. camel_case
  5. Camel_Case
  6. camelCase
  7. none of the above


Which of the following Java statements produces a result that is different from the results produced by the other statements?

  1. System.out.println("\"");
  2. System.out.println('\"');
  3. System.out.println('"');
  4. System.out.print("\"\n");
  5. System.out.print('"' + "\n");
  6. System.out.print('"' + '\n');
  7. none of the above

Which of the following are true about Donald Ervin Knuth?

  1. Donald Knuth’s wife’s name is Jill.
  2. Knuth never dated anyone besides his wife.
  3. Knuth is glad that he cannot prove that God exists.
  4. Knuth believes that God exists.
  5. Knuth did not participate in very many extracurricular activities at Case, because he wanted to focus on getting good grades instead.
  6. Knuth came to believe that his education boiled down to 50% mathematics and 50% writing skills.






Coding Exercise

On a blank sheet of paper, write your name in the upper right hand corner of the page as usual — last name on top, preferred name in parentheses below. Then write a Java program that meets the following requirements. You should be able to fit your program on one side of one sheet of paper. When you turn in your exam, staple the page containing your code to the back of this exam.

  1. It must be possible to successfully compile and execute your program when the source code is saved in a file named NestedLoop.java.
  2. Your program must print the values of all of the command-line arguments passed to the program.
  3. Your program must print the nth argument n times on n separate lines. For example, if the three command-line arguments "xyz" "abc" "123" are passed to your program when the program is executed, then your program must output:
    xyz
    abc
    abc
    123
    123
    123
    

Practice writing your program on a piece of scratch paper before attempting to write the final version of your program. When you are satisfied with your preliminary work, plan out and neatly write the final version of your program on a clean sheet of paper. Consider writing your program in pencil in case you need to erase mistakes. (Erase thoroughly.) Correctness, neatness, style, clarity, and simplicity are important factors that will be considered when your work is subjectively evaluated.

Hint:

Your program should include a nested loop, i.e. a loop inside another loop.

An Answer

public class NestedLoop {
  public static void main(String[] args) {
    for (int i = 0; i < args.length; i++) {
      for (int j = 0; j <= i; j++) {
        System.out.println(args[i]);
      }
    }
  }
}