CSC 211 GAE Self-Test

The purpose of this CTI placement test is to help us place you in the right programming course. The current test is to help determine whether you should by-pass CSC 211 (the first programming course in Java) and take CSC 212 (the second programming course in Java) or some other programming course Take this test by yourself and note the score. Working with your advisor, we can then choose the course that best works for you.

  1. What is the output from this code segment? Assume that floating-point values are printed to four decimal places (e.g., 9.1234).

    int x = 2, y = 3;
    System.out.print(x / y);
    System.out.print("!"); // separator
    double p = 2.0, q = 3.0;
    System.out.print(p / q);
    0!0.6666
    0!0.0
    0.6666!0.6666
    0.0000!0.0000
  2. Consider the code segment

    public class SomeClass{
    private int j;
    public void m() {
    int n;
    int q = -2;
    int k = p(n * j) + q;
    /* remainder of method m */
    }
    public int p(int param) { return param / 2; }
    }
    What is the error?
    The parameter in method p must be named n.
    Local variable n is used before being initialized.
    A method such as p cannot be written on a single line.
    There is no error in the code segment.
  3. The following method is added to the class SomeClass from Question 2.

    public int myMethod(int k) {return (k + q) * j;}

    Which of the following statements best describes the result of the compilation of the modified class?
    No compile-time error is found
    Variable k cannot be used in method myMethod because it was already used in m
    Variable q cannot be used in method myMethod because it is local to the method m
    Variable j cannot be used by methods myMethod and m because it is declared private
  4. Constructors are fundamental in the design and implementation of a class. Which of the following statements is correct?
    The class designer must provide at least one constructor for every class to avoid a compile-time error.
    Java automatically provides a no-argument constructor for every class, regardless of whether the class author provides other constructors.
    If no constructor is provided by the developer, Java provides a no-argument constructor for the class.
    It is good practice to overloaded constructors for every class
  5. Which one of the following statements best describes how overloaded methods in the same class can be differentiated from each other?
    The methods will have different names.
    The methods will have different return types.
    The methods will have different argument types, different numbers of arguments, or a different argument order.
    The methods will be operating on different objects.
  6. Consider the following BankAccount class, which models a simplified bank account

    public class BankAccount {
    private double balance;
    public BankAcount(double initialAmount){ balance = initialAmount; }
    public void deposit(double amount) { balance += amount; }
    public void withdraw(double amount){ balance -= amount; }
    }

    The following Driver class attempts to instantiate a BankAccount object, withdraw money from it and print its current balance, but it generates compiler errors. Why? Choose the answer that more accurately describes why it doesn't compile and how to best fix it.

    public class Driver {
    public static void main(String[] args) {
    BankAccount acct1 = new BankAccount(100);
    acct1.withdraw(100.00);
    System.out.println(acct1.balance);
    }
    }
    It doesn't compile because you are trying to access a non static variable (balance), from a static environment. The variable balance should be declared as static.
    It doesn't compile because your are passing a int as parameter (100) to the constructor instead of the required double. You should pass 100.00 instead.
    It doesn't compile because balance is declared as private. You should declare it as public instead.
    It doesn't compile because balance is declared as private. You should create a public double getBalance() (getter) method in the BankAccount class and invoke it on acct1 instead.
  7. Suppose acct1 refers to a BankAccount (from Question 6) with a balance of $300.00, while acct2 refers to a BankAccount with a balance of $200.00. After the assignment

    acct1 = acct2;

    Which of the following is true?
    Withdrawing $100.00 from acct1 will leave its balance at $200.00
    Withdrawing $100.00 from acct2 and then withdrawing $100.00 from acct1 will leave the balance of acct1 at $200.00
    Withdrawing $100.00 from acct2 and then withdrawing $100.00 from acct1 will leave the balance of acct1 at $0.00
    None of the above
    All of the above
  8. The following Account class is a modification of the BankAccount from Question 6. It contains a new variable, trsnsactions, which keeps count of the number of deposits and withdraws, and the corresponding getter method getTrasnsactionsCount. The deposit and withdraw methods have also been modified.
                                 
     public class Account
     {
        private double balance;
        private static int transactions=0;
    
        public Account(double initialAmount){ balance = initialAmount; }
        public static int getTransactionsCount(){ return transactions; }
        public void deposit(double amount) {
          balance += amount;
          transactions++;
        }
        public void withdraw(double amount){
          balance -= amount;
          transactions++;
        }
     }
    
    What is the output of the following Driver class?
    public class Driver {
    public static void main(String[] args) {
    Account acct1 = new Account(100.0);
    Account acct2 = new Account(300.0);
    acct2.withdraw(100.00);
    acct1.deposit(100);
    acct2.deposit(300.);
    System.out.print(acct2.getTransactionsCount()+" ");
    System.out.print(Account.getTransactionsCount());
    }
    }
    2 3
    3 3
    2 0
    No output. The Driver class will not compile because of the invalid method call: acct2.getTransactionsCount(). You can't invoke a static method on an object.
    No output. The Driver class will not compile because of the invalid method call: Account.getTransactionsCount(). Account was never instantiated.
  9. Given the class

    public class Test {
    private int k;
    public Test(int n) { set(n + 1); }
    public void set(int n) { k = k + n; }
    public void print() {System.out.print(k + " ");}
    }

    What is the output from the code segment below?

    Test test1 = new Test(3);
    Test test2 = new Test(-1);
    test1.print();
    test2.set(2);
    test1.print();
    test2.set(1);
    test2.print();
    3 5 0
    4 5 6
    4 4 3
    4 6 7
  10. The class Z

    public class Z {
    int k;
    public Z(int n) { k = n; }
    public void m(int n, boolean b) {
    if (b == true) k -= n;
    else k += n;
    }
    public void m(int n) { k += n; }
    public void dump() {
    System.out.print(k + " ");
    }
    }

    has three methods and a field. What is the output from the code segment

    Z obj = new Z(6);
    obj.m(7);
    obj.dump();
    obj.m(4, false);
    obj.dump();
    obj.m(3, true);
    obj.dump();
    14 15 15
    18 15 14
    13 17 14
    15 18 14
  11. What is the output from this code segment?

    int[ ] nums = {9, 7, 5, 3, 1};
    int i = nums.length - 1;
    while (i > 0) {
    System.out.print(nums[i] + " ");
    i = i - 1;
    }
    3 5 7 9
    1 3 5 3
    1 3 5 7 9
    7 3 5 1
    1 3 5 7
  12. What is the output from this code segment?

    String[ ] strings = new String[5];
    String prefix = "Q";
    for (int i = 0; i < strings.length; i++)
    strings[i] = prefix + i;

    String[ ] more_strings = new String[strings.length];
    int k = 0;
    for (int j = strings.length - 1; j >= 0; j--)
    more_strings[j] = strings[k++].toLowerCase();
    int i = 0;
    while (i < strings.length)
    System.out.print(more_strings[i++] + " ");
    Q1 Q2 Q3 q4
    q4 q3 q2 q1 q0
    q4 q3 q2 q1
    q0 q1 q2 q3 q4
  13. Here is a class R with three methods:

    public class R {
    public void foo(int n) {
    if (n == 0) print(n);
    else if (n < 0) bar(n + 1);
    else bar(n - 1);
    }
    void bar(int n) {
    print(n);
    if (n == 0) return;
    foo(n);
    }
    void print(int p) {
    System.out.print(p + " ");
    }
    }


    What is the output from the code segment

    R obj = new R();
    obj.foo(-4);
    -3 -2 -1 0
    -2 -1 0 1
    -2 -1
    -3 -2 -1
    -4 -3 -2 -1 0
  14. What is wrong with the following assignment statement?

    boolean ok = 1;
    The assigned value should be 1B rather than 1.
    The identifier ok is not well formed.
    The assignment operator for booleans is :=, not =.
    An int value cannot be assigned to a boolean variable.
  15. Here is the method named mys

    int mys(int[ ] array) {
    int n = 1;
    for (int i = 1; i < array.length; i++)
    if (array[i] < array[i - 1]) n++;
    else n = 1;
    return n;
    }

    and a code segment that invokes the method mys

    int[ ] nums = {13, 11, 15, 9, 7, 5, 8, 3, 1};
    int n = mys(nums);
    System.out.print(n);

    What does the code segment print?
    2
    3
    4
    5
  16. The code segment

    int i = 0;
    int n = 11;
    while (i < n)
    if (n % 2 == 0) n = n + 1;
    else n = n - 1;

    How many times does the body of the while loop execute?
    9
    10
    11
    Indefinitely many ("infinite loop")
  17. The code segment below contains a for loop and do/while loop.

    int i;
    int k = 5;
    for (i = 1; i <= k; i++)
    System.out.println(i);
    i = 0;
    do {
    System.out.println(++i);
    } while (i <= k);

    Which of the following best describes the behavior of the two loops?
    The bodies of the two loops execute the same number of times.
    The body of the for loop executes more than the body of the do/while loop.
    The body of the do/while loop executes nine times, whereas the body of the for loop executes seven times.
    The body of the do/while loop executes one time more than does the body of the for loop.
  18. The code segment below contains a for loop and do/while loop. The do/while loop is changed from Question 17 but the for loop is the same as in Question 17.

    int i;
    int k = 5;
    for (i = 1; i <= k; i++)
    System.out.println(i);
    i = 0;
    do {
    System.out.println(i);
    } while (i++ <= k);

    Which of the following best describes the behavior of the two loops?
    The bodies of the two loops execute the same number of times.
    The body of the do/while loop executes one more time than does the body of the for loop.
    The body of the do/while loop executes two more times than does the body of the for loop.
    The body of the do/while loop executes three more times than does the body of the for loop.
  19. The class java.nio.ByteBuffer has two overloads for the method putChar. The two declarations are:

    ByteBuffer putChar(char value);
    ByteBuffer putChar(int position, char value);

    The following code segment uses the ByteBuffer class:

    // Allocate a ByteBuffer with a capacity of 100.
    ByteBuffer buff = ByteBuffer.allocate(100);

    // Sample calls to putChar, each labeled with a
    // number 1, 2, 3, 4 for ease of reference.
    buff.putChar('A'); // 1
    buff.putChar('A', 23); // 2
    buff.putChar(24, 'B'); // 3
    buff.putChar('\n'); // 4

    The following questions refer to the putChar statements by their reference numbers: 1, 2, 3, and 4.
    All of the putChar calls are correct.
    None of the putChar calls is correct.
    Only putChar call 2 is incorrect.
    Both putChar calls 3 and 4 are incorrect.
  20. Consider the following modified BankAccount class

    public class BankAccount {
    private double balance;
    public BankAcount(double initialAmount){balance = initialAmount;}
    public void deposit(double amount) { balance += amount;}
    public void withdraw(double amount){ balance -= amount;}
    public String toString(){return "Account balance: "+balance;}
    }
    What output will running the Driver class produce?
    public class Driver{
    public static void RVS(BankAccount[ ] anArray){
    BankAccount temp;
    int left = 0;
    int right = anArray.length -1;
    while (left < right) {
    temp = anArray[left];
    anArray[left++] = anArray[right];
    anArray[right--] = temp;
    }
    }
    public static void main(String[] args){
    BankAccount[] bank = new BankAccount[3];
    for(int i=0; i<bank.length; i++)
    bank[i]= new BankAccount(i*100);
    RVS(bank);
    for(int i=0; i<bank.length; i++)
    System.out.println(bank[i]);
    }
    }
    Account balance: 0;
    Account balance: 100;
    Account balance: 200;
    Account balance: 200;
    Account balance: 100;
    Account balance: 0;
    200
    100
    0
    BankAccount@13f7281
    BankAccount@1d95492
    BankAccount@9980d5
  21. What does the method mys return when called with an argument of 6?

    int mys(int n) {
    int k = n;
    int s = 1;
    while (k > 1)
    s *= k--;
    return s;
    }
    121
    520
    720
    5043