AP Computer Science Midterm Exam

20 Questions | Total Attempts: 792

SettingsSettingsSettings
AP Computer Science Midterm Exam - Quiz

.


Questions and Answers
  • 1. 
    Consider the following declaration:        final double FEE = 0.50; The value of the variable FEE cannot subsequently be altered in the program, and any attempt to do so will be caught by the compiler.
    • A. 

      True

    • B. 

      False

  • 2. 
    The following lines of code will swap the contents of the integer variables x and y: int z; x=y; y=z; z=x;
    • A. 

      True

    • B. 

      False

  • 3. 
    The expression  !(a||b) is equivalent to (!a)&&(!b)
    • A. 

      True

    • B. 

      False

  • 4. 
    When Java evaluates the boolean expression                    (num >= 0 && num <= 100) the final value of the expression is determined only after both sub-expressions (num >= 0, num <= 100) are evaluated.
    • A. 

      True

    • B. 

      False

  • 5. 
    String word = “computer”; System.out.println(word.substring(3,6));       The output of the code above would be: mpu
    • A. 

      True

    • B. 

      False

  • 6. 
    After the following code is executed, the value of x is 4 int x = 7; x += 4;
    • A. 

      True

    • B. 

      False

  • 7. 
    The ________ of a variable is that section of the program in which the variable exists.
    • A. 

      Client

    • B. 

      Definition

    • C. 

      Scope

    • D. 

      Constructor

    • E. 

      Object

  • 8. 
    A(n) ________ is a class method that is automatically called whenever an object of that class is created.
    • A. 

      Client

    • B. 

      Definition

    • C. 

      Scope

    • D. 

      Constructor

    • E. 

      Object

  • 9. 
    Consider the following code segment:     String S = “ILoveJava”;     System.out.print(S.substring(1,5)); What is output when this code segment is executed?
    • A. 

      ILove

    • B. 

      ILov

    • C. 

      LoveJ

    • D. 

      Love

    • E. 

      LoveJava

  • 10. 
    Assume x and y are String variables with x = “Smile” and y = null. The result of (x == y) is
    • A. 

      True

    • B. 

      False

    • C. 

      Syntax error

    • D. 

      Exception

    • E. 

      X being set to null

  • 11. 
    Assume x and y are String variables with x = “Smile” and y = null. The result of  x.length( )+ y.length( ) is
    • A. 

      0

    • B. 

      5

    • C. 

      6

    • D. 

      9

    • E. 

      Exception

  • 12. 
    The following method determines whether any character occurs more than once in the given String.  However, the method has a bug.           boolean ContainsDouble(String S ) {          int n;                   for ( n=1; n < S.length( ); n++ )  {              if (S.charAt(0) == S.charAt(n)) return true;          }          return false;     } For which of the following String parameters would method ContainsDouble correctly return the value true? I. “dttd”    II. “xxxx”    III. “look”
    • A. 

      I only

    • B. 

      II only

    • C. 

      I and II

    • D. 

      I and III

    • E. 

      II and III

  • 13. 
    Assume that x and y are int variables with x = 8, y = 3, and a and d are char variables with a =‘c’ and d =‘D’, and examine the following conditions: Condition 1:  !(true && false) Condition 2:  (a != d || x != 8) Condition 3:  (x < y && x > 0) Condition 4:  (x > y || a == ‘D’ || d != ‘D’)  
    • A. 

      A 4 conditions are true

    • B. 

      Only condition 2 is true

    • C. 

      Only conditions 2 and 4 are true

    • D. 

      Only condition 3 is false

    • E. 

      All four conditions are false

  • 14. 
    Consider the following method: int boo (int x, int y) {    x -= 2; ++y;    return x * y; } Consider the following code: int x = 7, y = -3, z; z = x + y + boo ( y, x ); What is z?  
    • A. 

      -36

    • B. 

      -37

    • C. 

      -6

    • D. 

      -7

    • E. 

      None of the above

  • 15. 
    Consider the following code: int  b = 6; int c = 11; int a = b * (-c + 2) / 7; The value stored in a is:
    • A. 

      -7.71428

    • B. 

      -7.0

    • C. 

      -7

    • D. 

      -11.0

    • E. 

      -11

  • 16. 
    Consider the following swap method.  public void sillyStrings(String a, String b) {     a = a + “One”;     b = b + a; } If String x = “Hello” and String y = “Goodbye”, then sillyStrings(x, y); results in which of the following?
    • A. 

      X is now “HelloOne” and y is “GoodbyeHelloOne”

    • B. 

      X is still “Hello” and y is now “HelloOne”

    • C. 

      X and y remain unchanged

    • D. 

      X and y are now aliases

    • E. 

      X is now “GoodbyeOne” and y is now “HelloGoodbyeOne”

  • 17. 
    Consider a Point class with the following constructors and methods instance data description private int myX the current x coordinate private int myY the current y coordinate Methods description Point () Default constructor: Initialize the point to (0,0) Point(int x, int y) Another constructor: Initialize the point to (x,y) void SetX(int x) Set the x coordinate to the given value void SetY (int y) Set the y coordinate to the given value int GetX() return the x coordinate int GetY() return the y coordinate Which of the following code segments correctly defines a Point variable that represents the point 3,5? Segment I  Point P();   P.myX = 3;  P.myY = 5;   Segment II Point P();    P.SetX(3); P.SetY(5); Segment III Point P(3,5);
    • A. 

      I only

    • B. 

      II only

    • C. 

      III only

    • D. 

      I and II

    • E. 

      II and III

  • 18. 
    Consider a Point class with the following constructors and methods instance data description private int myX the current x coordinate private int myY the current y coordinate Methods description Point () Default constructor: Initialize the point to (0,0) Point(int x, int y) Another constructor: Initialize the point to (x,y) void SetX(int x) Set the x coordinate to the given value void SetY (int y) Set the y coordinate to the given value int GetX() return the x coordinate int GetY() return the y coordinate Assume that P is a Point object that represents the point x,y.  Which code segment correctly changes P to represent y,x? A)  P.SetX (P.GetY( )); P.SetY (P.GetX( )); B) P.GetX( ) = P.GetY( ); P.GetY( ) = P.GetX( ); C int tmp = P.myX; P.myX = P.myY; P.myY = tmp; D) int tmp = P.GetX( ); P.SetX(P.GetY( ) ); P.SetY(tmp);
    • A. 

      A

    • B. 

      B

    • C. 

      C

    • D. 

      D

  • 19. 
    For questions 16 and 17, use the following class definition: public class StaticExample { private static int x; public StaticExample (int y) { x = y; } public int incr( ) { x++; return x; } }   16) What is the value of z after the third statement executes below? StaticExample a = new StaticExample(12); StaticExample b = new StaticExample(5); int z = a.incr( ); A)  5 B)  6 C)  12 D) 13 E)  The code is syntactically invalid
    • A. 

      5

    • B. 

      6

    • C. 

      12

    • D. 

      13

    • E. 

      Syntax error

  • 20. 
    Consider the following class public class StaticExample {     private static int x;     public StaticExample (int y) {         x = y;     }     public int incr( ) {         x++;         return x;     } } If there are 4 objects of type StaticExample, how many different instances of x are there?
    • A. 

      4

    • B. 

      3

    • C. 

      1

    • D. 

      0

    • E. 

      Don't know

Back to Top Back to top