Pages

Exam7-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

Don’t Panic!

A Gaelic Blessing

May you have rye bread to do you good,
Wheaten bread to sweeten your blood,
Barley bread to do you no harm
And oatmeal bread to strengthen your arm.

Q1 = questions[0];

Which of the following Java expressions always evaluate to false? Assume that args is an array of values of type String and args is not null.

(There is more than one correct answer.)

  1. true
  2. !false
  3. !(false || true)
  4. !(false && true)
  5. Math.random() > 1
  6. args.length < 0

Q2 = questions[1];

Which of the following Java data types can be implicitly cast to the primitive type int?

(There is more than one correct answer.)

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

Q3 = questions[2];

What is the minimum number of bits needed to refer to any one of ten distinct values?

(There is one correct answer.)

  1. 2
  2. 3
  3. 4
  4. 8
  5. 9
  6. 10

Q4 = questions[3];

What does the following program print if the value of args is the array of strings {"a", "b"}?

(There is 1 correct answer.)

public class HowManyArgs
{
    public static void main(String[] args)
    {
        String s = "args: ";
        switch (args.length)
        {
            case 2:
                s += "a couple; ";
            case 3:
                s += "a few; ";
            case 1:
                s += "at least one; ";
                break;
            default:
                s = "none or several; ";
        }
        System.out.print(s);
    }
}

  1. args: a couple;
  2. args: a couple; at least one;
  3. args: a couple; a few;
  4. args: a couple; a few; at least one;
  5. args: at least one;
  6. args: none or several;

Q5 = questions[4];

In Java, what is the maximum possible value of a variable of type byte?

(There is more than one correct answer.)

  1. 127
  2. 10000000 (base 2)
  3. 01111111 (base 2)
  4. 11111111 (base 2)
  5. (27) - 1
  6. ((28)/2) - 1

Q6 = questions[5];

Which of the following Java expressions does NOT evaluate to a value of type int?

(There is more than one correct answer.)

  1. 1
  2. 1.0
  3. 4 / 3
  4. 4.0 / 3.0
  5. 4 % 3
  6. 4.0 % 3.0

Q7 = questions[6];

What does the following program print if the value of args is the array of strings {"a", "b", "c", "a", "b", "c"}?

(There is 1 correct answer.)


public class FindDuplicate
{
    public static void main(String[] args)
    {
        final String first = args[0];
        String output = "not found";
        int count = 0;
        for (String next : args)
        {
            count++;
            if (next.equals(first))
                output = next;
        }
        System.out.print(output + ": " + count);
    }
}

  1. a: 1
  2. a: 2
  3. a: 3
  4. a: 4
  5. a: 5
  6. a: 6

Q8 = questions[7];

What does the following program print if the value of args is the array of strings {"a", "b", "c", "a", "b", "c"}?

(There is 1 correct answer.)


public class FindDuplicate
{
    public static void main(String[] args)
    {
        final String first = args[0];
        String output = "not found";
        int count = 0;
        for (String next : args)
        {
            if (next == first)
                continue;
            count++;
            if (next.equals(first))
                output = next;
        }
        System.out.print(output + ": " + count);
    }
}

  1. a: 1
  2. a: 2
  3. a: 3
  4. a: 4
  5. a: 5
  6. a: 6

Q9 = questions[8];

What does the following program print if the value of args is the array of strings {"a", "b", "c", "a", "b", "c"}?

(There is 1 correct answer.)


public class FindDuplicate
{
    public static void main(String[] args)
    {
        final String first = args[0];
        String output = "not found";
        int count = 0;
        for (String next : args)
        {
            if (next == first)
                continue;
            count++;
            if (next.equals(first))
            {
                output = next;
                break;
            }
        }
        System.out.print(output + ": " + count);
    }
}

  1. a: 1
  2. a: 2
  3. a: 3
  4. a: 4
  5. a: 5
  6. a: 6

Q10 = questions[9];

What does the following program print if the value of args is the array of strings {"0", "1", "2", "3", "4"}?

(There is 1 correct answer.)

public class Split
{
    static int count = 0;
    static int half(int n)
    {
        count++;
        if (n > 1)
            Split.half(n/2);
        return count;
    }
    public static void main(String[] args)
    {
        System.out.print(Split.half(args.length));
    }
}
  1. 0
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5

Q11 = questions[10];

What does the following program print if the value of args is the array of strings {"0", "1", "2", "3", "4"}?

(There is 1 correct answer.)

public class Split
{
    static int count = 0;
    static double half(double n)
    {
        count++;
        if (n > 1)
            Split.half(n/2);
        return count;
    }
    public static void main(String[] args)
    {
        System.out.print(Split.half(args.length));
    }
}
  1. 0
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5

Q12 = questions[11];

Which of the following are valid Java statements?

(There is more than one correct answer.)

  1. int x = null;
  2. Integer x = null;
  3. String x = null;
  4. String[] x = null;
  5. char x = null;
  6. Boolean x = null;

Q13 = questions[12];

Which of the following statements about constructor methods are true?

(There is more than one correct answer.)

  1. The name of a constructor method may be the same as the name of the class it belongs to.
  2. The name of a constructor method must be the same as the name of the class it belongs to.
  3. The signature of a constructor method does not include a return type.
  4. The return type of a constructor method must be void.
  5. A class may have more than one constructor method.
  6. Constructor methods are called when a program uses the keyword new.

Q14 = questions[13];

Which of the following statements are true?

(There is more than one correct answer.)

  1. An editor from Addison Wesley took Donald Knuth out to lunch and ask him to write a book about compilers.
  2. Knuth wanted to write a book about compilers but couldn't find a publisher that was interested in the idea.
  3. Knuth wrote a letter to Stanford informing the University that he couldn’t visit the school that year because he had to finish his book before his son was born.
  4. Knuth’s original motivation for writing The Art of Computer Programming was to share several of the original ideas he had invented while developing compilers.
  5. Knuth thought he could finish The Art of Computer Programming rather quickly, and he did.
  6. Knuth was motivated to write The Art of Computer Programming quickly, because he believed that if he didn't do so soon there were many others who would take advantage of the opportunity, and he wanted to be first.

Q15 = questions[14];

Which of the following statements are true?

(There is more than one correct answer.)

  1. Donald Knuth took longer than usual to earn his Ph.D. because he was working and earning a lot of money while going to school.
  2. Donald Knuth earned his Ph.D. in three years, because someone told him that it takes three years to earn a Ph.D. so he created a schedule and stuck to it.
  3. Knuth earned his Ph.D. at Stanford.
  4. Knuth earned his Ph.D. at Caltech.
  5. Knuth was hired by the same university that awarded him his Ph.D.
  6. Knuth was asked to teach a class about computers while he was a Ph.D. student.

Coding Exercise

Write a Java program that shuffles the values of the array of strings passed to the main method of your program. Use the Knuth shuffle algorithm, also known as the Fisher-Yates shuffle. 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. Consider writing your program in pencil in case you need to erase mistakes. (Erase thoroughly.) Correctness, neatness, style, clarity, and simplicity are important.