AP Comp Sci Unit 3 Test
5. Which choice would evaluate to true based on the following code? public class Rectangle { private int width; private int height; public Rectangle(int rectWidth, int rectHeight) { width = rectWidth; height = rectHeight; } public int getWidth() { return width; } public int getHeight() { return height; } public boolean equals(Rectangle other) { return width == other.getWidth() && height == other.getHeight(); } } public class RectangleTester { public static void main(String[] args) { Rectangle one = new Rectangle(4, 5); Rectangle two = new Rectangle(4, 5); Rectangle three = new Rectangle(3, 7); Rectangle four = new Rectangle(3, 7); } }
C. boolean compare3 = one.equals(two);
Define aliases.
In computer programming, aliasing refers to the situation where the same memory location can be accessed using different names.
What is the difference between logical equality and reference equality?
Logical equality compares data, while reference equality compares references.
3. Write the syntax for comparing two objects. Use the object names one and two.term-3
Object one = new Object(); Object two = new Object(); System.out.println(one.equals(two));
What is short circuit evaluation?
Short-circuit evaluation means that when evaluating boolean expressions, you only have to evaluate the first boolean expression, since if one is true, you automatically know if the condition is true or false.
2. Write logic to compare two Strings. String names are name1 and name2
String string1 = "pepper"; String string2 = "mint"; System.out.println(string1.equals(string2))
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;
c.str1 == str2 && str1.equals(str3)
7.What value of x would make this boolean expression evaluate to false?(x < 10) && (x != 5)
d.Any value greater than or equal to 10
6. Create code that reads in two numbers from the user, dividend and divisor, and prints out whether dividend is evenly divisible by divisor. Use Short Circuiting to prevent the condition inside the if statement from dividing by 0.
import java.util.*; public class Divisibility { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the dividend: "); int dividend = input.nextInt(); System.out.print("Enter the divisor: "); int divisor = input.nextInt(); if((divisor != 0) && (dividend % divisor == 0)) { System.out.println(dividend + " is divisible by " + divisor + "!"); } else { System.out.println(dividend + " is not divisible by " + divisor + "!"); } } }
1. Create a code that asks the user for 2 integers and determines if the user's two numbers are BOTH odd. If both numbers are odd, print "Both numbers are odd." Otherwise, print "Both numbers are NOT odd."
import java.util.*; public class MyProgram { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter 2 positive integers"); int num1 = input.nextInt(); int num2 = input.nextInt(); boolean bothOdd = !(num1 % 2 == 0 || num2 % 2 == 0); if (bothOdd) { System.out.println("Both numbers are odd"); } else { System.out.println("Both numbers are NOT odd."); } } }
Write a program that asks the user for three strings. Then, print out whether or not the first string concatenated to the second string is equal to the third string.
import java.util.*; public class ThreeStrings { public static void main(String[] args) { Scanner input = new Scanner(System.in); String string1 = input.nextLine(); String string2 = input.nextLine(); String string3 = input.nextLine(); String concatenated = string1 + string2; if(concatenated.equals(string3)){ System.out.println(string1 + " + " + string2 + " is equal to " + string3 + "!"); }else{ System.out.println(string1 + " + " + string2 + " is not equal to " + string3 + "!"); } } }
Write the syntax for comparing two integers. Use the variable names int1 and int2.
int int1 = 1; int int2 = 2; System.out.println(int1 == int2);