Wednesday, January 22, 2014

APCS Chap 3 Test

 

AP Computer Science                      Test 3 Part I

Multiple Choice Questions:

 

1)  Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x is negative but leave x alone if x is 0?

a)         if (x > 0)  x++;

                 else x--;

 

b)         if (x > 0) x++;

                 else if (x < 0) x--;

 

c)         if (x > 0) x++;

                 if (x < 0) x--;

                     else x = 0;

 

d)         if (x == 0) x = 0;

                  else x++;

                     x--;

 

e)         x++;

               x--;

 

 

 

2)  Consider the following code that will assign a letter grade of ‘A’, ‘B’, ‘C’, ‘D’, or ‘F’ depending on a student’s test score. 

if(score >= 90) grade = 'A';

if(score >= 80) grade = 'B';

if(score >= 70) grade = 'C';

if(score >= 60) grade = 'D';

else grade = ‘F’;

 

a)  This code will work correctly in all cases

b)  This code will work correctly only if grade >= 60

c)  This code will work correctly only if grade < 60

d)  This code will work correctly only if grade < 70

e)  This code will not work correctly under any circumstances

 

 

3)  Given two String variables, s1 and s2, to determine if they are the same length, which of the following conditions would you use?

a)  (s1.equals(s2))

b)  (s1.length( ).equals(s2))

c)  (s1.length( ).equals(s2.length( ))

d)  (s1.length( ) == s2.length( ))

e)  length(s1) == length(s2)


 

4) What does this program segment print?

for( f = 0; f < 3; ++f)

for( g = 0; g < 2;  ++g)

System.out.print(f + " " + g + " ");

 

a. 0 0 0 1 1 0 1 1 2 0 2 1                                b. 0 1 0 2 0 3 1 1 1 2 1 3                  c. 0 1 0 2 1 1 1 2

                            d . 0 0 0 1 0 2 1 0 1 1 1 2 2 0 2 1 2 2                 e. nothing

 

5) The output of the statement;

for (b = 1; b > 3; ++b) System.out.print(b + “  “);

 

a. 1 1 1               b. 1 2 3             c. 1 2 3 4                  d. nothing             e. syntax error

 

6) What is the output of the following code?

b = 1;

while (b < 4)

System.out.println(b + " ");

 

a. 1                      b. 1 2 3                  c. 1 2 3 4                  d. 1 1 1 1 1 1 ...            e. nothing

 

7) What does this program segment print?

for(m=0;m<4; ++m);

for(n=0;n<2; ++n);

System.out.print(m + " " + n + " ");

 

a. 0 0 0 1 1 0 1 1 2 0 2 1 3 0 3 1               b. 0 1 0 2 1 1 1 2 2 1 2 2                 c. 4 2

             d. 3 1                                                                             e. nothing

 

 

8)  In the String major = "Computer Science", what is returned by major.charAt(1)?

a.  'C'            b.  'o'                c.  'm'                    d.  "C"                 e.  "Computer"

 

 

9)         Which of the following would return the last character of the String x?

a.  x.charAt(0);                              b.  x.charAt(last);                c.  x.charAt(length(x));

                 d.  x.charAt(x.length( )-1);                             e.  x.charAt(x.length( ));

 

 

10)       Which library package would you import to use the class Random?

a.  java.beans                       b.  java.io                c.   java.lang

                      d.  java.text                               e.  java.util

 

11)       Since you cannot take the square root of a negative number, you might use which of the following instructions to find the square root of the variable x?

a.  Math.sqrt(x*x);                                 b.  Math.sqrt((int) x);                  c.  Math.sqrt(Math.abs(x));

                         d.  Math.abs(Math.sqrt(x));                                   e.  Math.sqrt(-x);

 

12)  Given the following code, where x = 0, what is the resulting value of x after the for-loop terminates?

for(int i=0;i<5;i++)

                  x += i;

 

a.  0                               b.  4                 c.  5                 d.  10                     e.15

 

 

13)  Given a String, s, which is assumed to have at least one character in it, which of the following conditions would determine if the first character of the String is the same as the last character?

a.  (s.charAt(0) == s.charAt(s.length( )))

b.  (s.charAt(1) == s.charAt(s.length( )))

c.  (s.charAt(0) == s.charAt(s.length( ) - 1))

d.  (s.charAt(0) == s.charAt(s.length( ) + 1))

e.  (s.charAt(0) == s.charAt(last))

 

14)  Given that s is a String, what does the following loop do?

for(int j = s.length( );  j  >  0;  j--)

      System.out.print(s.charAt( j - 1));

a.  it prints s out backwards

b.  it prints s out forwards

c.  it prints s out backwards after skipping the last character

d.  it prints s out backwards but does not print the 0th character

e.  it yields a run-time error because there is no character at s.charAt(j-1) for j = 0

 

 

15)  The following nested loop structure will execute the inner most statement (x++) how many times?

for(int j = 0; j < 100; j++)

            for(int k = 100; k > 0; k--)

                  x++;

a.  100              b.  200             c.  10,000                d.  20,000                   e.  1,000,000

 

 

True/False Questions:

 

 

16) In Java, the symbol “=” and the symbol “= =” are used synonymously (interchangeably).

 

17) If the integer variable answer is 5, then after answer %= 8; is executed, answer will be 3.

 

18)  Suppose that String name = "Frank  Zappa".  Then the instruction;

                name.toUpperCase( ).replace('A', 'I'); 

will return “FrInk  ZIppI”.

 

19)  The following for-loop is an infinite loop.

for(int j = 0; j < 1000; ) i++;

 

20)   The following loop is syntactically valid.

for(int j = 0; j < 1000; j++) j--;

 

For question 21, assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye".

 

21)  The expression (!done && x <= y) is true.

 

22)  In Java, it is possible to create an infinite loop out of while loops, but not for-loops.

 

23)  Any for loop can be written as a while loop.

 

24)  The third portion of a for loop, the increment portion, must increment or decrement a variable by 1.

 

25)  When reading an unknown amount of input from the user, a for loop would be the best choice.

 

APCS Test 2

AP Computer Science            Chapter 2 Test

Date:10/3/2013

Multiple Choice Questions:

 

Use the following class definition to answer questions 1-4.

 

public class Questions1_4

{

     public static void main(String[ ] args)

     {

             System.out.print("Here");

             System.out.println("There  " + "Everywhere");

             System.out.println("But  not" + "in  Texas");

     }

}

 

1)    The program will print the word "Here" and then print

a)    "There  Everywhere" on the line after "Here"

b)    "There" on the line after "Here" and "Everywhere" on the line after "There"

c)    "There  Everywhere" on the same line as "Here"

d)    "ThereEverywhere" on the same line as "Here"

e)    "ThereEverywhere" on the line after "Here"

 

2)    The final println command will output

a)    "But  not  in  Texas"

b)    "But  notin  Texas"

c)    "But  not" on one line and "in  Texas" on the next line

d)    "But  not+in  Texas"

e)    "But  not  +  in  Texas"

 

3)    How many lines of output are provided by this program?

a)    1

b)    2

c)    3

d)    4

e)    5


 

 

4)    A reasonable comment for this program might be

a)    //  a program that demonstrates the differences between print, println and how + works

b)    // a program that outputs a message about Texas

c)    // a program that demonstrates nothing at all

d)    // a program that outputs the message “Here There Everywhere But not in Texas”

e)    // a program that has three output statements in it

 

5)    Consider the following statement:

System.out.println("1 big bad wolf\t8 the 3 little pigs\n4 dinner\r2night");

          This statement will output ___ lines of text

a)    1

b)    2

c)    3

d)    4

e)    5

 

6)    If you want to output the text "hi there", including the quote marks, which of the following could do that?

a)    System.out.println("hi there");

b)    System.out.println(""hi there"");

c)    System.out.println("\"hi there");

d)    System.out.println("\"hi there\"");

e)    none, it is not possible to output a quote mark because it is used to mark the beginning and ending of the String to be output.

 

7)    Of the following types, which one cannot store a numeric value?

a)    int

b)    double

c)    char

d)    all of these can store numeric values

e)    none of these can store numeric values

 

8)    What value will z have if we execute the following assignment statement?    

double z = 5 / 10;

a)    z will equal 0.0

b)    z will equal 0.5

c)    z will equal 5.0

d)    z will equal 0.05

e)    none of the above, a run-time error arises because z is a double and 5 / 10 is an int

 

9)    What value will z have if we execute the following assignment statement?

int z = 50 / 10.00;

a)    5

b)    5.0

c)    50

d)    10

e)    none of the above, a run-time error arises because z is an int and 50 / 10.00 is not

 

10)                    If x is an int and y is a double, all of the following are legal except which assignment statement?

1)    y = x;

2)    x = y;

3)    y = (double) x;

4)    x = (int) y;

5)    all of the above are legal

 

11)                    Given the following assignment statement, which of the following answers is true regarding the order that the operators will be applied based on operator precedence? 

a = (b + c) * d / e – f;

a)    *, /, +, -

b)    *, +, /, -

c)    +, *, /, -

d)    +, /, *, -

e)    +, -, *, /

 

12)                    What will be the result of the following assignment statement?  Assume b = 5 and c = 10.

int a = b * (-c + 2) / 2;

a)    30

b)    –30

c)    20

d)    –20

e)    –6

 

13)                    Assume that x, y and z are all ints equal to 50, 20 and 6 respectively.  What is the result of x / y / z?

a)    0

b)    12

c)    16

d)    A syntax error as this is syntactically invalid

e)    A run-time error because this is a division by 0

 

14)                    What is output with the statement System.out.println(x+y); if x and y are int values where x=10 and y=5?

a)    15

b)    105

c)    10 5

d)    x+y

e)    An error since neither x nor y is a String

 

15)                    What is output with the statement System.out.println(""+x+y); if x and y are int values where x=10 and y=5?

a)    15

b)    105

c)    10 5

d)    x+y

e)    An error since neither x nor y is a String


 

 

16)                    If you want to store into the String name the value "George  Bush", you would use which statement?

a)    String name = "George  Bush";

b)    String name = new String("George  Bush");

c)    String name = "George" + "  " + "Bush";

d)    String name = new String("George" + "   " + "Bush");

e)    Any of the above would work

 

17)                    Consider having three String variables a, b and c.  The statement c = a + b; can also be achieved by doing

a)    c = a.length( ) + b.length( );

b)    c = (int) a + (int) b;

c)    c = a.concat(b);

d)    c = b.concat(a);

e)    c = a.plus(b);

 

18)                    In the String major = "Computer Science", what is returned by major.charAt(1)?

a)    'C'

b)    'o'

c)    'm'

d)    "C"

e)    "Computer"

 

19)                    Which of the following would return the last character of the String x?

a)    x.charAt(0);

b)    x.charAt(last);

c)    x.charAt(length(x));

d)    x.charAt(x.length( )-1);

e)    x.charAt(x.length( ));

 


 

 

20)                    Which library package would you import to use the class Random?

a)    java.beans

b)    java.io

c)    java.lang

d)    java.text

e)    java.util

 

21)                    Since you cannot take the square root of a negative number, you might use which of the following instructions to find the square root of the variable x?

a)    Math.sqrt(x*x);

b)    Math.sqrt((int) x);

c)    Math.sqrt(Math.abs(x));

d)    Math.abs(Math.sqrt(x));

e)    Math.sqrt(-x);

 


 

 

For questions 22 and 23, refer to the class defined below:

         

          import java.util.Scanner;

          public class Questions

          {

                public static void main(String[ ] args)

                {

                    int x, y, z;

                    double average;

                    Scanner scan = new Scanner (System.in);

                    System.out.println("Enter an integer value");

                    x = scan.nextInt( );

                    System.out.println("Enter another integer value");

                    y = scan.nextInt( );

                    System.out.println("Enter a third integer value");

                    z = scan.nextInt( );

                    average = (x + y + z) / 3;

                    System.out.println("The result of my calculation is " + average);

                }

          }

 

22)                    Questions computes

a)    The correct average of x, y and z as a double

b)    The correct average of x, y and z as an int

c)    The average of x, y and z as a double, but the result may not be accurate

d)    the sum of x, y and z

e)    the remainder of the sum of x, y and z divided by 3

 

23)                    What is output if x = 0, y = 1 and z = 1?

a)    0

b)    0.0

c)    0.6666666666666666

d)    0.6666666666666667

e)    0.67

 

 

True/False Questions:

 

1) If x is a string, then x = new String("OH"); and x = "OH"; will accomplish the same thing.

 

2) If x is the String "Hi There", then x.toUpperCase().toLowerCase(); will return the original version of x.

 

3) If String name = "George W. Bush"; then the instruction name.length(); will return 14.

 

4) If String a = "ABCD" and String b = "abcd" then a.equals(b); returns false but a.equalsIgnoreCase(b); returns true.

 

5) Unlike the String class where you must pass a message to an object (instance) of the class, as in x.length( ), in order to use the Math class, you pass messages directly to the class name, as in Math.abs( ).

 

6) A double is wider than an int.

 

7) A variable of type boolean will store either a 0 or a 1.

 

8) In Java, ‘a’ and ‘A’ are considered to be different values.

 

9) With an enumerated type, the programmer defines the possible values of the type.

 

10) An enumerated type defined by “enum Grade {A, A-, B+, B, B-, C, D, F}” is invalid.

 

 

11) The Random class has a method nextDouble( ) which returns a random double value between 0 and 1.

 

 

12) If a, b and c are int variables with a = 5, b = 7, c = 12, then the statement int z = (a * b – c) / a; will result in z equal to 4.

 

13) Java is a strongly typed language which means every variable has a single type associated with it throughout its existence in the program, and the variable can only store values of that type.

 

14) Suppose that String name = "Frank  Zappa".  Then the instruction name.toUpperCase( ).replace('A', 'I');  will return “FrInk  ZIppI”.

 

15) The mod operator, %, can only be performed on int values and its result is a double.

 

16) The word println is a reserved word.

 

17) Wrapper classes in Java allow you to create objects representing primitive data.

 

18) If you want to use classes from a package other than java.lang, you must import them.

 

19) The operators * and + have the same precedence.

 

20) Since double to int is a narrowing conversion, there is no way to convert a double to an int.

 

 

Free-format Question:

 

Write a set of instructions to prompt the user for an int value and input it using the Scanner class into the variable x and prompt the user for a double value and input it into the variable y.