Uploaded by davidoffbro1

JAVA OOP study guide

advertisement
Midterm 1 Study Guide
The following topics can appear on the midterm:





















Print statements
Primitive types (boolean, int, double, char)
Math and concatenation
Assignment operators
Increment and decrement operators
Casting
If/else/else if statements
Comparison operators -> ==, !=, >, >=, <, <=
Logical operators and short-circuiting
DeMorgan’s Laws and boolean simplification
While, do-while, for, and for-each loops
Scope – variable only exists for a certain portion of your code.
Static methods
Math methods – random, pow, sqrt, abs
Escape sequences – \n – newline, \t – tab, \” – to show quote, \\ - to
show backslash.
String methods – length, substring, substring, charAt, equals, indexOf
Null – can only exist with object, not with primitives. If you point to an
object that’s null, you will get a null pointer error. Example – if(x==null)
1D arrays
2D arrays
ArrayLists – size, add, add, remove, get, set.
Wrapper Classes – used to wrap primitive data types into wrapper object
class.
Example Problems (from previous exams):
1.
Which of the following code segments will produce an integer between 5 and 10, inclusive?
(A) (int) (Math.random() * 5) + 5
(B) (int) (Math.random() * 5) + 6
(C) (int) (Math.random() * 5) + 10
(D) (int) (Math.random() * 6) + 5
– (int)(Math.random()* (high-low + 1) + low
(E) Math.random(5, 10)
2.
Consider the following code segment:
int x = 3;
int y = 0;
if(/* condition */){
System.out.print(“Yes”);
}else{
System.out.print(“No”);
}
Which of the following options can be used in place of condition to make the program print
Yes?
I. x / y == 0 – divided by zero – runtime error
II. x / y == 0 || y == 0 – divided by zero in the first half so it
will crash
III. y == 0 || x / y == 0 – short circuits after the first
condition because its true
(A) I only
(B) II only
(C) III only
(D) II and III – if it was double instead of int, this option would be
true. Integer division by zero crashes. Double divided by zero
would output NaN (not a number).
(E) I, II, and III
3.
The following method is intended to determine an AP score according to this scale.
Points
62 or more
44-61
31-43
25-30
24 or less
AP Score
5
4
3
2
1
public static int getAPScore(int points){
if (points >= 62){
int score = 5;
}else if (points >= 44){
int score = 4;
}else if (points > 30){
int score = 3;
}else if (points >= 25){
int score = 2;
}else{
int score = 1;
}
return score;
}
Which of the following changes must be made so the function works as intended?
I. Replace points > 30 with points >= 31.
II. Replace }else{ with }else if (points < 25){.
III. Insert int score = 0; between the first two lines of code and remove int from all
subsequent lines.
(A) II only
(B) III only – matter of scope. In the code as is, score is defined in each if statement and
does not exist outside it, so returning it will give an error.
(C) II and III only
(D) I, II, and III
(E) No changes are necessary; the code already works as intended.
4.
The getAPScore method is in the AP class. If a student earned int pointsEarned points
on the exam, which line of code will correctly call the getAPScore method from outside the AP class
to determine that student’s AP score?
(A) AP.getAPScore(pointsEarned)
(B) getAPScore(pointsEarned)
(C) getAPScore.AP(pointsEarned)
(D) getAPScore(pointsEarned).AP
(E) pointsEarned.getAPScore
5.
What is the output of the following code segment?
String output = “”
int x = 2;
int y = 1;
while (x < 20){
output = output + x + 1 / y + “\n”;
y = x;
x = (int) Math.pow(3, x);
}
System.out.print(output);
(A) 3
4
(B) 3
4.5
(C) 3
5
(D) 21
80
(E) 21
90
6.
Which of the following code segments will correctly print all elements of a 2D int array called
arr?
I. for (int i = 0; i < arr.length; i++){
for (int j = 0; j < arr.length; j++){
System.out.println(arr[i][j]);
}
System.out.println();
}
II. for (int[] x : arr){
for (int j = 0; j < x.length; j++){
System.out.println(x[j]);
}
System.out.println();
}
III. for (int[] i : arr){
for (int j : i){
System.out.println(arr[i][j]);
- syntax error (i is
an array not an int so cant use it like that in
print statement.
}
System.out.println();
}
(A) I only
(B) II only
(C) III only
(D) I and II
(E) I and III
7.
Assume that you are writing a program which asks the user for a grade. You want this program to
prevent the user from entering any grades that are outside the range of [0, 100]. You are using the
following program segment to query the user until a single proper grade is entered.
int grade = input.nextInt();
while ( ????? ){
System.out.print(“That is invalid.
grade = input.nextInt();
}
Enter a grade: “);
Which of the following conditions can be put in place of the ????? in the while loop to
accomplish the desired task?
I. grade < 0 || grade > 100
II. !(grade < 0) || !(grade > 100)
III. !(grade >= 0 && grade <= 100)
(A)
(B)
(C)
(D)
(E)
I only
I and II only
II and III only
I and III only
I, II and III
8.
What is the output of the program segment below?
int num1 = 500;
int num2 = 200;
int num3 = 300;
int average = num1 + num2 + num3 / 3;
System.out.println(average);
(A)
(B)
(C)
(D)
(E)
9.
333
333.333333333333
334
800
Syntax Error
Consider the following code segment.
double a = 10;
double b = 3.5;
int c = (int) (a / b);
int d = (int) a / (int) b;
int e = c – d;
What is the value of the variable e?
(A)
(B)
(C)
(D)
(E)
-1
-0.5
0
0.5
1
10.
Consider the following boolean expression.
boolean result = !(a <= b && c != d)
Which of the following options is equivalent to result?
I. !(a <= b) && !(c != d)- wrong because demorgans law it should be
|| instead of &&
II. !(d != c && b >= a)
III. a > b || c == d
(A)
(B)
(C)
(D)
(E)
11.
What value is returned from Math.pow(3, 2)?
(A)
(B)
(C)
(D)
(E)
12.
I only
II only
III only
I and II
II and III
5.0
6.0
7.0
8.0
9.0
Consider the following method.
public static void changeArrays(int[] arr1, int[] arr2){
arr1 = arr2;
arr1[3] = 3;
}
What are the values of a and b after running the following code?
int[] a = {1, 2, 3, 4, 5};
int[] b = {6, 7, 8, 9, 10};
changeArrays(a, b);
(A) {1,
(B) {1,
(C) {6,
(D) {6,
(E) {6,
a
2, 3,
2, 3,
7, 3,
7, 8,
7, 8,
4,
4,
9,
3,
3,
5}
5}
10}
10}
10}
{6,
{6,
{6,
{6,
{6,
b
7,
7,
7,
7,
7,
8,
8,
8,
8,
8,
3,
9,
9,
3,
9,
10}
10}
10}
10}
10}
13.
Which of the following lines of code will double the value stored in the variable x?
I.
II.
III.
x *= 2;
x += x;
x + x = x;
(A) II only
(B) III only
(C) I and II only
(D) II and III only
(E) I, II, and III
14.
Consider the following method.
public static void halveString(String a){
a = a.substring(a.length() / 2);
}
Consider the following code segment.
String x = “Hello”;
halveString(x);
What is the value of x at the end of the code segment?
(A) “He”
(B) “Hel”
(C) “Hello”
(D) “llo”
(E) “lo”
15.
Consider the following methods.
public boolean affirmative(){
System.out.print(“yes ”);
return true;
}
public boolean negative(){
System.out.print(“no ”);
return false;
}
What is the output of the following code segment?
if(negative() && affirmative()){
System.out.print(“Success!”);
}
(A)
(B) no
(C) no Success!
(D) no yes
(E) no yes Success!
16.
Consider the following method intended to count the number of null elements in an array.
public static int countNulls(Object[] arr){
int count = 0;
for (Object obj : arr){
if(/* missing code */){
count++;
}
}
return count;
}
Which of the following code segments should replace /* missing code */?
(A) arr[obj] == null
(B) arr[obj].equals(null)
(C) obj == null
(D) obj.equals(null)
(E) None of the above
17.
Which of the following is NOT a valid escape sequence in Java?
(A) \”
(B) \\
(C) \n
(D) \t
(E) These are all valid Java escape sequences.
18.
Which of the following variables could be assigned a value of null?
(A) String a
(B) boolean b
(C) double c
(D) int d
(E) None of the above
Directions: For questions 19-20, use the following method that is intended to return a random
letter from word.
1
2
3
4
5
6
19.
public static String getRandomLetter(String word){
int wordLength = word.length();
int letterIndex = (int) Math.random() * wordLength;
String letter = word.substring(letterIndex, 1);
return letter;
}
Which of the following options is the best replacement for line 3?
(A) int letterIndex
(B) int letterIndex
(C) int letterIndex
(D) int letterIndex
(E) Line 3 is best as-is.
20.
=
=
=
=
((int) Math.random()) * wordLength;
(int) (Math.random() * wordLength) + 1;
(int) (Math.random() * wordLength);
(int) Math.random() * wordLength + 1;
Which of the following options is the best replacement for line 4?
(A) String letter
(B) String letter
(C) String letter
(D) String letter
(E) Line 4 is best as-is.
21.
=
=
=
=
word.charAt(letterIndex);
word.substring(letterIndex);
word.substring(letterIndex, letterIndex + 1);
word.substring(letterIndex, letterIndex);
What is the output of the following code segment?
List<String> a = new ArrayList<String>();
List<String> b = new ArrayList<String>();
List<String> c = b;
a.add(“apple ”);
b.add(“banana ”);
c.add(“cherry ”);
b = a;
a.add(“durian ”);
b.add(“elderberry “);
c.add(“fruit” );
a.remove(0);
b.remove(0);
c.remove(0);
System.out.println(a.get(0) + b.get(0) + c.get(0));
(A) apple apple cherry
(B) durian durian durian
(C) durian elderberry fruit
(D) elderberry elderberry cherry
(E) The code throws an IndexOutOfBoundsException exception.
Download