Preview

04 - AP Exam question set #4 (Medium)

 1. What are the values of x, y, and z after the following code executes?
int x = 3;
int y = x;
int z = x * y;
x++;

  x = 0, y = 3, z = 0

  x = 3, y = 3, z = 9

  x = 4, y = 4, z = 9

  x = 4, y = 3, z = 9

 2. When a double is converted into an integer in Java, it truncates the digits after the decimal. The ouput here is therefore 9.
double a = 9.6982;
int b = 12;
b = (int) a;

  FALSE

  TRUE

 3. Assume that Class1 and MainClass are properly defined in separate files. What is the output of the code in main()?
class Class1
{
    public Class1()
    {
        System.out.print("Hello ");
    }

    void printSomething(String name)
    {
        System.out.print("Hello " + name + " ");
    }
}

public class MainClass
{
    public static void main(String[] args)
    {
        Class1 class1 = new Class1();
        class1.printSomething("John");
    }
}

  Hello John Hello

  Hello John Hello John

  Hello John

  Hello Hello John

 4. A method called palindrome checks to see if a string is the same forwards and backwards. What advantage does Data Set A have over Data Set B for testing?
Data Set A
===========
adam
badam


Data Set B
===========
adam
madam
eve

  All strings in Data Set A have the same number of characters.

  Data Set A has fewer values than Data Set B.

  Data Set A contains one string which returns true and the other that returns false.

  There are no advantages.

 5. Given the following code segment, what is the value of s1 after the code executes?
String s1 = "Test Track";
String s2 = s1;
String s3 = s2;
String s4 = s1;
s2 = s2.toLowerCase();
s3 = s3.toUpperCase();
s4 = null;

  test track

  tEST tRACK

  TEST TRACK

  Test Track

 6. The reason for the above answer (assumign you got it right!) is because:

  Strings are immutable meaning that any changes to a string creates and returns a new string

  Strings are mutable so they can easily change their values or adapt

  Strings are essentially integer based. They start at index 1

  Strings cannot be altered even if they are stored in a variable

 7. What is the output from the following code?
String s = "Testand Trac";
String s1 = s.substring(0,7);
String s2 = s1.substring(2);
String s3 = s2.substring(0,3);
System.out.println(s3);

  stan

  rac

  and

  sta

 8. Which of the following is equivalent to the code segment below?
if (x > 0)
   x = -x;
if (x < 0)
   x = 0;

  if (x < 0) x = 0; else x = -1;

  x = 0;

  if (x > 0) x = -x; else x = 0;

  if (x > 0) x = 0;

 9. What is printed as a result of the following code segment?
for (int k = 0; k < 20; k+=2) {
   if (k % 3 == 1)
      System.out.print(k + " ");
}

  Answer: 0 2 4 6 8 10 12 14 16 18

  Answer: 1 4 7 10 13 16 19

  Answer: 4 10 16

  Answer: 4 16

 10. Which of the following preconditions is reasonable for the Liquid constructor?
This code defines a Liquid class with given variables. For example, Liquid water = new Liquid(100, 50, 0); defines a water object with a boiling point of 100, current temperature of 50, and a freezing temperature of 0.

public class Liquid
{
    private int boilingPoint;
    private int currentTemp;
    private int freezingPoint;

    public Liquid(int bp, int ct, int fp)
    {
        boilingPoint = bp;
        currentTemp = ct;
        freezingPoint = fp;
    }
    /* Other methods not shown */
}

  Precondition: currentTemp > 0

  Precondition: fp > ct > bp

   Precondition: fp > 0

  Precondition: fp < ct < bp

 11. Consider the following field arr and method checkArray. Which of the following best describes what checkArray returns?
private int[] arr;

// precondition: arr.length != 0
public int checkArray()
{
    int loc = arr.length / 2;
    for (int k = 0; k < arr.length; k++)
    {
        if (arr[k] > arr[loc])
        {
            loc = k;
        }
    }
    return loc;
}

  Returns the index of the largest value in the second half of array arr.

  Returns the index of the first element in array arr whose value is greater than arr[loc].

  Returns the index of the largest value in array arr.

  Returns the index of the last element in array arr whose value is greater than arr[loc].

 12. Which of the following best explains why the getAge method does NOT work as intended?
Consider the following Fossil class, 
with the fossil?s age stored in the method?s 
int attribute. 

The getAge method is intended to allow methods
in other classes to access a Fossil object?s age
value; however, it does not work as intended! 


public class Fossil
{
    private int age;

    public Fossil(int a)
    {
        age = a;
    }

    public int getAge()
    {
        return a;
    }
}

  The return type of the getAge method should be void.

  The getAge method should have at least one parameter.

  The getAge method should be declared as private.

  The instance variable age should be returned instead of a, which is local to the constructor.

 13. What is printed as a result of executing the following code segment?
List aList = new ArrayList();
aList.add(new Integer(1));
aList.add(new Integer(2));
aList.add(1, new Integer(5));
aList.set(1, new Integer(4));
aList.add(new Integer(6));
aList.add(new Integer(3));
System.out.println(aList);

  [1, 2, 4, 6, 3]

  [1, 2, 5, 4, 6, 3]

   [6, 5, 4, 3, 2, 1]

   [1, 4, 2, 6, 3]

 14. Consider the following code segment. The value of sum after this code executes is 6.
int[][] m = { {1,1,1,1},{1,2,3,4},{2,2,2,2},{2,4,6,8}};

int sum = 0;
for (int k = 0; k < m.length; k++) {
    sum = sum + m[m.length-1-k][1];
}

  TRUE

  FALSE

 15. Given the following, which of the following is a correct call to method1?
public class Test1
{
   public void method1(Test2 v1, Test3 v2)
   {
      // rest of method not shown
   }
}

public class Test2 extends Test1
{
}

public class Test3 extends Test2
{
}

The following initializations appear in a different class.
Test1 t1 = new Test1();
Test2 t2 = new Test2();
Test3 t3 = new Test3();

   t3.method1(t3,t3);

  t2.method1(t3,t2);

  t1.method1(t1,t1);

  t3.method1(t1,t1);

 16. Given the following class declarations, the output from Student s1 = new HungryStudent(); followed by s1.getInfo(); will be: Pie,Strawberry Pie
public class Student {
   public String getFood() {
      return "Pie,Strawberry Pie";
   }
   public String getInfo()  {
      return this.getFood();
   }
}

public class HungryStudent extends Student {
   public String getFood() {
      return "Treacle,Tarty Treacle";
   }
}

  TRUE

  FALSE

 17. The output of the following code is: [1, 4, 5]
List aList = new ArrayList();
aList.add(new Integer(1));
aList.add(new Integer(2));
aList.remove(1);
aList.add(1, new Integer(3));
aList.set(1, new Integer(4));
aList.add(new Integer(5));
System.out.println(list);

  TRUE

  FALSE

 18. Given the following method declaration, which of the following is printed as the result of the call whatsup(1234)?
//precondition:  x >=0
public static void whatsup(int x)
{
   System.out.print(x % 10);

   if ((x / 10) != 0)
   {
      whatsup(x / 10);
   }
   System.out.print(x % 10);
}

  12344321

  1441

  43211234

  3443

 19. In the above recursive code, the method calls itself when:

  (x/10) is equal or less than zero

  (x/10) is equal to 10

  (x / 10) is greater than or equal to zero

  (x/10) is not equal to zero

 20. Given the following method declaration, what value is returned as the result of the call guess(5)?
public static int guess(int n)
{
   if (n == 0)
      return 1;
   else
      return 3 * guess(n - 1);
}

  81

  3

  27

  243