WorksheetsNISD CS AP Exam Prep
Total questions: 12
Worksheet time: 17mins
Name
Class
Date
1.
Which of the following will evaluate to true only if boolean expressions X, Y, and Z are all false?
a)
!X && !( Y && !Z )
b)
!X || !Y || !Z
c)
!( X || Y || Z )
d)
!X || !( Y || !Z )
2.
If you wanted to set a variable equal to some value, which operator would you use?
a)
==
b)
equals
c)
=
d)
!=
3.
If you wanted to check to see if two numerical values were equal, which operator would you use?
a)
=
b)
!=
c)
equals
d)
==
4.
To check if some integer x was evenly divisible by another integer y, which line of code would you use?
a)
if ( x / y = 0)
b)
if ( x / y == 0 )
c)
if ( x % y == 0 )
d)
if ( x % y = 0 )
5.
How would you create a new array of 5 integers?
a)
int[5] x;
b)
new int[5] x;
c)
int[] x = new int[5];
d)
int x = new int[5];
6.
How would you create a new array of 3 Strings?
a)
String[3];
b)
new String[3];
c)
String[] x = 3
d)
String[] x = new String[3];
7.
Assume aNum is a variable of type double that is positive. A program contains the boolean expression (Math.pow( aNum, 0.5 ) == Math.sqrt ( aNum )). Even though aNum1/2 is mathematically equivalent to √aNum the above expression returns the value false in a student's program. Which of the following is the most likely reason?
a)
Math.pow returns an int, while Math.sqrt returns a double.
b)
aNum was imprecisely calculated in a previous program statement.
c)
The computer stores floating-point numbers with 32-bit words.
d)
There is round-off error in calculating the pow and sqrt functions.
8.
Which of the following statements about parameters are false?
a)
Static methods have no implicit parameter this.
b)
Two overloaded methods in the same class must have parameters with different names.
c)
Parameters of primitive types in Java are passed by value.
d)
Two different constructors in a given class can have the same number of parameters.
9.
Consider the following data field and method.
private double[] list;
public int getIt(double value) {
int k = 0;
while(k < list.length && list[k] != value) k++;
if (k < list.length) return k;
else return -1; }
Which of the following best describes what is returned by this method?
private double[] list;
public int getIt(double value) {
int k = 0;
while(k < list.length && list[k] != value) k++;
if (k < list.length) return k;
else return -1; }
Which of the following best describes what is returned by this method?
a)
The number of times that value occurs in list, or -1 if all items in the list equal value.
b)
The number of items in list that are not equal to value, or -1 if value is not in list.
c)
The index of the first occurrence of value in list, or -1 if value is not in list
d)
The index of the last occurrence of value in list, or -1 if value is not in list
10.
Consider the following description. A forest has trees. A white pine is a pine which is a tree. Which of the following partial declarations would be the best choice for representing the relationships among these things?
a)
public class Forest
public class Tree extends Forest
public class Pine extends Tree
public class WhitePine extends Pine
public class Tree extends Forest
public class Pine extends Tree
public class WhitePine extends Pine
b)
public class WhitePine
public class Pine extends WhitePine
public class Tree extends Pine
public class Forest extends Tree
public class Pine extends WhitePine
public class Tree extends Pine
public class Forest extends Tree
c)
public class WhitePine
public class Pine extends WhitePine
public class Tree extends Pine
public class Forest {
private ArrayList<Pine> myPines;private ArrayList<WhitePine> myWhitePines; . . . }
public class Pine extends WhitePine
public class Tree extends Pine
public class Forest {
private ArrayList<Pine> myPines;private ArrayList<WhitePine> myWhitePines; . . . }
d)
public class Tree
public class Pine extends Tree
public class WhitePine extends Pine
public class Forest {
private List<Tree> myTrees; . . . }
public class Pine extends Tree
public class WhitePine extends Pine
public class Forest {
private List<Tree> myTrees; . . . }
11.
Which of the following statements about an abstract class is true?
a)
An abstract class can only have a default (no parameter) constructor.
b)
An abstract class cannot have instance variables.
c)
An abstract class can only have methods that are public.
d)
An abstract class can have some methods that are implemented and some that are not.
12.
Consider the following code segment. for(int num = 19; num > 0; num--) {
if(num % 4 == 1)
System.out.print(num + " "); }
What is printed as a result of executing this code segment?
if(num % 4 == 1)
System.out.print(num + " "); }
What is printed as a result of executing this code segment?
a)
19 17 15 13 11 9 7 5 3 1
b)
18 16 14 12 10 8 6 4 2
c)
17 13 9 5 1
d)
18 14 10 6 2
100 %
