Preview

01 - AP Exam question set #1 (Easy)

 1. What does the following code print?
System.out.println(18 % 5);

  1

  4

  2

  3

 2. The following code will print 16.
System.out.println(2 + 3 * 5 - 1);

  TRUE

  FALSE

 3. Given the following code, what is the value of b when it finishes executing?
double a = 9.6982;
int b = 12;
b = (int) a;

  9.6982

  12

  10

  9

 4. What is the value of s1 after the following code executes?
String s1 = "Hey";
String s2 = s1.substring(0,1);
String s3 = s2.toLowerCase();

  H

  he

  Hey

  h

 5. The value of pos after the following code executes is 5.
String s1 = "ac ded ca";
int pos = s1.indexOf("d");

  TRUE

  FALSE

 6. What is printed when the following code executes and x has been set to zero and y is set to 3?
if (x > 0 || (y / x) == 3) System.out.println("first case");
else System.out.println("second case");

  none of these options are correct

  An error will occur. This is because division by zero cannot be achieved

  first case

  second case

 7. If the score is less than 60, the value of grade would be 'E'.
if (score >= 90) grade = "A";
if (score >= 80) grade = "B";
if (score >= 70) grade = "C";
if (score >= 60) grade = "D";
else grade = "E";

  TRUE

  FALSE

 8. Assume that SomeClass and MainClass are properly defined in separate files. What is the output of main()?
class SomeClass
{
    int someVar;
}

public class MainClass
{
    public static void main(String[] args)
    {
        SomeClass x = new SomeClass();
        System.out.println(x.someVar);
    }
}

  Answer: unknown value

  Answer: 0

  Answer: compile error

  Answer: runtime error

 9. The following code will print 'second'.
int x = 3;
int y = 2;
if (y / x > 0)
   System.out.print("first ");
   System.out.print("second ");

  TRUE

  FALSE

 10. What are the values of var1 and var2 after the following code segment is executed and the while loop finishes?
int var1 = 0;
int var2 = 2;

while ((var2 != 0) && ((var1 / var2) >= 0)) {
   var1 = var1 + 1;
   var2 = var2 - 1;
}

  var1 = 1, var2 = 1

  var1 = 0, var2 = 2

  The loop won't finish executing due to a division by zero

  var1 = 2, var2 = 0

 11. Analyse the code and the desired output. The code snippet would correctly produce the desired output.
OUTPUT
======
11111
2222
333
44
5

CODE
=====

for (int j = 1; j <= 5; j++) {
        for (int k = 5; k >= 1; k--) {
           System.out.print(j + " ");
        }
        System.out.println();
     }

  FALSE

  TRUE

 12. Which of the following best explains why the code will not compile?
public class Liquid
{
    private int currentTemp;
    private int boilingPoint;

    public Liquid(int ct, int bp)
    {
        currentTemp = ct;
        boilingPoint = bp;
    }

    public void changeTemp(int newTemp)
    {
        currentTemp = newTemp;
    }

    public void increaseTemp(int howMuch)
    {
        currentTemp = newTemp + howMuch;
    }

}

  The Liquid class is missing a constructor.

  The variable newTemp is not defined in the increaseTemp method.

   The Liquid constructor needs a return type.

   The class is missing an accessor method.

 13. What are the values in a after mult(2) executes?
private int[ ] a = {1, 3, -5, -2};

public void mult(int amt)
{
   int i = 0;
   while (i < a.length)
   {
      a[i] = a[i] * amt;
   } // end while
} // end method

   {1, 3, -5, -2}

  Due to an infinite loop, this code does not stop running!

  {3, 9, -15, -6}

   {2, 6, -10, -4}

 14. The values in a after multAll(3) executes are: {3, 9, -15, -6}
private int[ ] a = {1, 3, -5, -2};

public void multAll(int amt)
{
   int i = 0;
   while (i < a.length)
   {
      a[i] = a[i] * amt;
      i++;
   } // end while
} // end method

  TRUE

  FALSE

 15. When the following code executes, ["Selasie", "Lillith", "Sharon"] will be printed.
List list1 = new ArrayList();
list1.add("Adam");
list1.add("Lillith");
list1.add("Sharon");
list1.set(0, "Dylan");
list1.add(0, "Selasie");
System.out.println(list1);

  FALSE

  TRUE

 16. What will print when this code is run?
List list1 = new ArrayList();
list1.add(new Integer(1));
list1.add(new Integer(2));
list1.add(new Integer(3));
list1.remove(1);
System.out.println(list1);

  [1, 2, 3]

   [2, 3]

   [1, 3]

  [1, 2]

 17. In the following array, the value 6 is at row 0 and column 2.
int[][] a = { {2, 4, 6, 8}, {1, 2, 3, 4}};

  TRUE

  FALSE

 18. Which of the following statements assigns the letter S to the third row and first column of a two-dimensional array named strGrid (assuming row-major order).

  strGrid[3][1] = "S";

  strGrid[2][0] = "S";

  strGrid[1][3] = "S";

  strGrid[0][2] = "S";

 19. Read the following excerpt and select the correct answer.
A vintage store wishes to go online.
Customers can search for and order via a website.

The store has books, films and old records. 
For each type of published material 
(books, films, records) they need to track the 
id, title, author(s), date published, and price. 

Which of the following would you suggest as the
best, most efficient, design?

  Create classes for each

  Create one class Vintage Material with the requested fields plus type.

  Create the class Vintage Material with children classes of Book, Film, and Record.

  Create classes Book, Film, and Record with the requested fields.

 20. Read the following excerpt and select the correct answer.
A platform game app is being written by a
small team of programmers. 

One coder is implementing a class called Player; 
another in the team is writing code that will 
use the Player class. 

Which of the following aspects of the public methods
and fields of the Player class does not need to be 
known by both programmers?

  How the methods are implemented

  The number and types of the method parameters

  The method names

  The method return types

 21. How many columns does a have if it is created as follows?
int[][] a = { {2, 4, 6, 8}, {1, 2, 3, 4}};

  2

  8

  4

  1

 22. Read the following excerpt and select the correct answer.
A realtor (real estate) house selling company
needs a program to store information
about their latest houses for sale. 

For each house, they want to store and
track the following information: 

>number of bedrooms (<2 or 4+),
>whether the house has a garden
>and its average gas and electricity bills per month. 

Which of the following is the best design?

  Use a class House, with fields: numbedrooms, hasGarden, and avgbills.

  Use a class House with three subclasses: Bedrooms,Garden, and AvgBills.

  Use classes: Bedrooms,Garden, AvgBills, each with a subclass House.

  Use four unrelated classes: House, Bedrooms, Garden, and AverageBills.

 23. How many recursive calls does the following method contain?
public static int evenMult(int n)
{
   if (n == 1) {
      return 2;
   } else {
      return 2 * n * evenMult(n - 1);
   }
}

  3

  1

  2

  None

 24. In the following code, line 7 has two recursive calls.
public static int fibonacci(int n)
{
   if (n == 0)
      return 0;
   else if (n == 1)
      return 1;
   else return fibonacci(n-1) + fibonacci(n-2);
    }

  TRUE

  FALSE

 25. Which line has the recursive call in the following code?
public String moonStar(int n)
{
   if (n == 0) {
      return "*";
   } else {
      return moonStar(n - 1) + moonStar(n - 1);
   }
}

  4

  6

  1

  3