Ap Comp - Sci A - Unit 3 Review
The following method is designed to return true if the passed phrase contains either the word cat or dog. public boolean containsPet(String input) { if (input.indexOf("cat") >= 0) { return true; } else if (input.indexOf("dog") >= 0) { return true; } else { return false; } } Which of the following test cases can be used to show the code does NOT work as intended? - containsPet("I have a dog."); - containsPet("I don't have pets."); - containsPet("I can catch fish."); - containsPet("My dog caught my cat");
containsPet("I can catch fish.");
What is the output of the following code snippet? Circle one = new Circle(10); Circle two = new Circle(10); System.out.println(one == two); - true - false
false
boolean shortCircuit = true || (5 / 0 == 0); Will the 5 / 0 == 0 operation be evaluated? - Yes - No
no
!(!weekday || holiday) - weekday || !holiday - weekday && !holiday - !weekday && !holiday - !(weekday || !holiday)
weekday && !holiday
student is trying to determine if the following two expressions are equivalent. A. x && (!x || y) B. x && !(x || !y) What values of x and y would prove that the expressions are NOT equivalent? - x = true; y = true - x = true; y = false - x = false; y = true - x = false; y = false
x = true; y = true
findTheMiddle(2110890125)? public static String findTheMiddle(int number) { String stringNum = "" + number; int mid = stringNum.length()/2; if(stringNum.length() % 2 == 1) { return stringNum.substring(mid,mid+1); } else { return stringNum.substring(mid-1,mid+1); } } - 8 - The code will error - 9 - 89
89
Reference equality uses the equality operator (==) and compares the references (addresses in memory) of two objects. -True -False
false
Which of the following logical statements is equivalent to !(A || B) Where A and B are boolean values !A && !B !A || !B !A || B A && B
!A && !B
Assuming a and b are properly initialized boolean values, which expression would be equivalent to the following: !(a && b) a || b !(a || b) !a && !b !a || !b
!a || !b
Logical equality compares the data of the objects. False True
true
What values for x and y will cause the program to execute the /* missing code */? if (x > 10) { x -= 5; if (x > 10 || y <= 10) { x ++; y++; } else { /* missing code */ } } - x = 18; y = 12 - x = 12; y = 8 - x = 12; y = 12 - x = 18; y = 18 - x = 18; y = 8
x = 12; y = 12;
boolean shortCircuit = true && (5 / 0 == 0); Will the 5 / 0 == 0 operation be evaluated? - Yes - No
yes
What value of x would make this boolean expression evaluate to false? (x < 10) && (x != 5) - Any value less than 10 - Any value less than 10 except for 5 - The only time it is false is when x = 5. - Any value greater than or equal to 10
Any value less than 10
String ursa = new String("2849"); String major = new String("2849"); I.System.out.println(ursa.equals(major)); II. System.out.println(ursa == major); III. System.out.println(ursa.equals("2849")); IV. System.out.println(major == "2849"); - I, II, III, and IV - IV only - I, III, and IV only - II and IV only - I and III only
I and III only
Which of the following logical statements is equivalent to !(A && B) Where A and B are boolean values - !A && B - !A && !B - !A || !B - !A || B
!A || !B
Which of the following statements is true? - There can only be one else-if statement in an if-else if statement. - An if- else if statement must always have an else clause. - An if- else if statement cannot have an else clause. - An if- else if statement must always start with an if statement.
An if- else if statement must always start with an if statement.
public class Circle { private int radius; public Circle(int theRadius) { radius = theRadius; } public void setRadius(int newRadius) { radius = newRadius; } public int getRadius() { return radius; } public boolean equals(Circle other) { return radius == other.getRadius(); } } What is the output of the following code snippet? public void run() { Circle one = new Circle(5); Circle two = new Circle(10); foo(two); System.out.println(one.equals(two)); } public void foo(Circle x) { x.setRadius(5); } - true - false
true
public int returnEven(int number) { if (number % 2 == 0) { return number; } else if (number == 0) { return number + 2; } else { return number + 1; } } Does the code work as intended? - Yes. - No, the mod function on line 3 should read number % 2 == 1 to find even numbers. - No, the else if on line 7 and the else on line 11 should just be if statements. - No. Zero will get returned on line 5 and not make it to line 7.
No. Zero will get returned on line 5 and not make it to line 7.
Which of the following Boolean expressions will yield true after the code snippet executes? String str1 = new String("Karel"); String str2 = "CodeHS"; String str3 = "Karel"; str2 = str1; - str1 == str2 && str1 == str3 - str1 != str2 && str1.equals(str3) - str1 == str2 && str1.equals(str3) - None of the above will evaluate to true after this code snippet executes.
str1 == str2 && str1.equals(str3)