AP CSA Units 1 & 2 Midterm Study Guide
The value that a non-void method outputs is called
a return value.
After the execution of the following lines of code, what will be output onto the console? String letters = "ABCde"; String name = "Karel the Dog"; String letter = "D"; System.out.println(name.indexOf(letter)); System.out.println(letters.indexOf(name.substring(3,4))); System.out.println(letters.indexOf(letter));
10 4 -1
What will the value of myBankAccount be after the method call depositMoney(myBankAccount, 572);? int myBankAccount = 122; public void depositMoney(int bankAccount, int deposit) { bankAccount += deposit; }
122
What does this method call doubleNumber(7.8); return, given the following method? public double doubleNumber(double myNumber) { return (double) (int) myNumber * 2; }
14.0
int a = 2; int b = 3; int c = 4; System.out.println(a * b + b / a + (a * c / 4.0) * c);
15.0
What is the result of this expression? (int) (5 + 2/ 3 + 1)
6
One of these is not like the others! Find the integer operation that is not equal to the other three: 29 / 10 92 % 10 67 / 20 67 % 5
67/20 = 3; the rest equal 2
int x = 14; int y = 8; int ans = (int) (y + x / 5 - 3); System.out.println(ans); What will be displayed?
7
int x = 23; double y = 3; System.out.println((int)(x / y));
7 becausex / y calculates to 7.66 then the cast to an int results in the value getting truncated to 7
What output will be produced by: System.out.println("Hello"); System.out.println("Karel");
Hello Karel
Which of the following print: Hello Java! I. System.out.println("Hello Java!"); II. System.out.print("Hello Java!"); III. System.out.print("Hello"); System.out.print("Java!"); IV. System.out.println("Hello"); System.out.println("Java!");
I and II only
Why do we use methods in Java? I. To make code easier to understand II. To define global variables III. To avoid repeated code IV. To simplify code V. To avoid needing to define them as public or private
I-V all
Which of the following would equal 2? I. int x = 0; x ++; x += x; II. int y = 4; y ++; y /= 2; III. int z = 4; z += 2; z /= 2;
II & III only
What is true of a void method? It returns any value. It takes no parameters. It returns no value. It can take any parameter.
It returns no value
int ans = 10000; ans *= ans; ans *= ans; System.out.println(ans); What will be displayed?
Overflow error
System.out.println("The answer is " + 10 + 3); What will be displayed?
The answer is 103
A procedure that is defined by the user is called a
method.
final int x = 10; x++; System.out.println(x); What will be displayed?
nothing/error
int x = 10; int y = 34; x=y; y=x; System.out.println("x is " + x + ", y is " + y); What will be displayed?
x is 34, y is 34
What if I wanted to display the message "The answer is 13"? Fill in the blank: System.out.println______________
("The answer is " + (10 + 3));
What is returned by this method call: translator("pokemon")? public String translator(String word) { return word.substring(1) + word.charAt(0) + "ay"; }
"okemonpay"
int x = 10; x--; x *= x; x++; x /= x; System.out.println(x); What will be displayed?
1
What is the result of this expression? 4 + 8 * 3 / 4 + 5 % 2
11
How many lines will be printed with the following statement? System.out.println("Hello"); System.out.println(" World"); System.out.print("Welcome to"); System.out.print("Java.");
3
Assume that a, b, and c are all integers with values of 90, 5, and 4 respectively. What would the value of x be in this expression? int x = a / b / c;
4
What is the result of the following expression when x is 125? x % 6
5
What is the value of myInteger after this line of code is executed? int myInteger = (int) 5.6;
5
Consider the following class: public class RandomCalculator { private int x; private int y; public RandomCalculator(int one, int two) { x = one; y = two; } public int add(int num) { return x + y + num; } public int add(double num) { return (int)(num) + x / y; } public int add(double num, int num2) { return (int)(num2 + num + x - y); } public double add(int num, double num2) { return num + num2 - x + y; } What would the output be of the following code segment: RandomCalculator calc = new RandomCalculator(4, 5); System.out.println(calc.add(5.0)); System.out.println(calc.add(5.0, 5)); System.out.println(calc.add(5, 5.0));
5 9 11.0
A coffee shop has created a DrinkOrder class. The class contains variables to represent the following: -A String variable called name to represent the name of the drink. -An int variable called ounces to indicate how many ounces the drink should be. -A boolean variable called isIced to indicate whether the drink is iced. An object latte has been declared as type DrinkOrder after someone has ordered a Latte. Which of the following descriptions is accurate? An instance of latte is DrinkOrder. An attribute of latte is DrinkOrder. An attribute of name is latte. An instance of the DrinkOrder class is latte. An attribute of DrinkOrder is latte.
An instance of the DrinkOrder class is latte.
Which Java Data Type would be best suited to represent WPCP's operating budget for 2020-2021?
Double
What is wrong with this method definition? public int printPayAmount(int amount) { System.out.println(amount); }
Nothing is returned from this method
What does the method call tripleString("APCSA"); return for the following method? public String tripleString(String x) { return x * 3; }
Nothing/error
int x = 10; double y = 3.141519; int ans = (int) x * y; System.out.println(ans); What will be displayed?
Nothing/error - x*y in "int ans" is a double
Which is the correct syntax for creating a Scanner object?
Scanner input = new Scanner(System.in);
Which of the following is not a primitive type? int double String boolean char
String
Which of the following is a valid variable declaration in Java? String 3rdPeriod; String student.id; String student#4; String $name;
String $name;
What are parameters?
The formal names given to the data that gets passed into a method.
Which of the following statements is true about variables? -The memory associated with a variable of a primitive type holds an actual primitive value. -When a variable is declared final, its value can only be changed through a direct reassignment. -A variable originally created as an int can be changed to store a double through casting.
The memory associated with a variable of a primitive type holds an actual primitive value.
Which of the following lists primitive types in Java in order of increasing memory allocation?
boolean, int, double
Refer to the following code segment: double myDouble = 1/4; System.out.println("1 / 4 = " + myDouble); The output of the code is: 1 / 4 = 0.0 The student wanted the output to be: 1 / 4 = 0.25 Which change to the first line of their code segment would get the student the answer that they wanted?
double myDouble = (double) 1/4;
Given a and b as properly initialized integers, which of the following will result in a correct calculation with a decimal answer? double y = (double) (a / b); double y = a / b * 1.0; double y = a / b; double y = 1.0 * a / b;
double y = 1.0 * a / b;
char example; A variable is declared as shown. Which of the following are valid variable assignments? example = 'E' ; example = "E" ; example = "Espinoza" ; All of these are valid
example = 'E' ;
Consider the following class: public class Dog { private String name; private String breed; public String getName() { return name; } } An object Karel is created using the Dog class. What would the result of the command System.out.println(Karel.getName()); be?
null
What expression returns the 1's place of an integer x?
x % 10
If y is a properly initialized int, which of the following assignments always results in zero? y += y y -= y y *= y y /= y
y-=y