Ap Computer Science A Midterm Review

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Given the following, what will be printed out? int a = 2; int b = 3; int c = 4; System.out.println(a * b + b / a + (a * c / 4.0) * c); 15.0 24 15 24.0

15.0

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

Which expression is true? true && !true !false || !true true && false false || false || !true

!false || !true

What will this method call output given yesOrNo(true)? public String yesOrNo(boolean myBoolean) { if(myBoolean == true) { return "Yes"; } else { return "No"; } } "No" "Yes" 0 This program will error

"Yes"

What is returned by this method call: translator("pokemon")? public String translator(String word) { return word.substring(1) + word.charAt(0) + "ay"; } "pokemonpay" "okemonpay" "okemonay" This method call will error.

"okemonpay"

What will the call to method patternGrid(3,4,'#') print? public void patternGrid(int rows, int columns, char symbol) { for(int m = 0; m < rows; m++) { for(int n = 0; n < columns; n++) { System.out.print(symbol); } System.out.println(); } } #### #### #### ### ### ### #### #### #### #### This code will error.

#### #### ####

What does the call to the method someMethod(3,1) output? public int someMethod(int x, int y) { int sum = 0; while (x < 10) { sum += x % y; x++; y++; } return sum; } 9 12 11 10

10

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 -1 7 10 0 4 -1 10 7 3 10 3 -1

10 4 -1

What is the result of this expression? 4 + 8 * 3 / 4 + 5 % 2 5 6 12 11

11

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; } 694 122 572 The code will error.

122

Consider the following method: public double doubleVal(double x) { return x *2; } The following code segment calls the method doubleVal: Double val = 6.5; System.out.println(doubleVal(val)); What is printed when the code segment is executed? 13 13.0 12.0 12.5 Double values cannot be used in double methods before being converted.

13.0

What will the following code print? int n = 5; n ++; n ++; n += n; System.out.println(n); 12 40 14 18

14

What does this method call doubleNumber(7.8); return, given the following method? public double doubleNumber(double myNumber) { return (double) (int) myNumber * 2; } 16 15.6 14.0 This method is improperly written. 15.0

14.0

What will the method call formatText(2018, 17, "Dec") print to the screen? public static void formatText(int a, int b, String c) { System.out.println(b + " " + c + ", " + a); } formatText(2018, 17, "Dec")Dec 17, 2018 Dec 17 2018 17 Dec 2018 17 Dec, 2018

17 Dec, 2018

Consider the following code segment: int x = 10; int y = 2; int count = 1; while(x > y) { x /= y; count++; } What will the value of count be after executing the code segment? 2 1 10 4 3

3

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."); 1 2 3 4

3

What would the method call myMethod("Karel The Dog", 'e') return? public int myMethod(String x, char y) { int z = 1; for(int i = 0; i < x.length(); i++) { if(x.charAt(i) == y) { z++; } } return z; } 1 2 3 4

3

Assume that a, b, and c are all integers with values of 90, 5, and 4 respectively. What would the value of xbe in this expression? int x = a / b / c; 4.5 90 4 72

4

What is the value of x after this code runs? int x = 5; x = 10; x = 4; 5 10 4 true

4

What is the result of the following expression when x is 125? x % 6 5 20 20.8 0.8

5

What is the value of myInteger after this line of code is executed? int myInteger = (int) 5.6; 6 5.6 5 9

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)); 14 9 11.0 5 9.0 11 5 9 11.0 14 11.0 9

5 9 11.0

What is the result of this expression? (int) (5 + 2 / 3 + 1) 5 6 6.67 7 0

6

The following code is intended to print 8. int x = 23; double y = 3; System.out.println((int)(x / y)); What is printed and why? 7 because the values of x and y are integers so 23 / 3 evaluated to 7 7 becausex / y calculates to 7.66 then the cast to an int results in the value getting truncated to 7 8 because the values of x and y are integers so 23 / 3 evaluated to 8 8 because x / y calculates to 7.66 then the cast to an int results in the value getting rounded up to 8

7 becausex / y calculates to 7.66 then the cast to an int results in the value getting truncated to 7

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.

What will this program print if the value of grade is 80? if(grade > 90) { System.out.println("A"); } else if(grade > 80) { System.out.println("B"); } else if(grade > 70) { System.out.println("C"); } A B C Nothing

C

What output will be produced by System.out.println("Hello"); System.out.println("Karel"); Hello Karel HelloKarel Hello Karel Error

Hello Karel

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; I Only II Only III Only I and II Only I, II, and III

I and II Only

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 I, II, and III only I and III only I, II, and IV only I, II, III, and IV

I and II only

Which of the following code segments would correctly assign word from the String sentence = "Grab the last word" to the variable lastWord? I. String lastWord = sentence.substring(sentence.length()-4, sentence.length()); II. String lastWord = sentence.substring(sentence.indexOf("w"), sentence.indexOf("d")); III. String lastWord = sentence.substring(14, 16) + sentence.substring(1, 2) + sentence.substring(sentence.length()-1, sentence.length()); I only III only I and II only I and III only I, II, & III

I and III only

Consider the following class: public class Greetings { private String greetings; public Greetings(String greeting) { greetings = greeting; System.out.println(greetings); } public void hello() { System.out.println("Hello"); } public void translate() { greetings = "Hola"; } public void changeGreeting(String greeting) { greetings = greeting; } public void greeting() { System.out.println(greetings); } } Which of the following code segments will produce this output in the console: "Hello" "Hola" I. Greetings hi = new Greetings("Hello"); hi.translate(); hi.greeting(); II. Greetings.hello(); Greetings.translate(); Greetings.greeting(); III. Greetings hello = new Greetings(); hello.hello(); hello.translate(); hello.greeting(); IV Greetings hola = new Greetings("Hello"); hola.changeGreeting("Hola"); hola.greeting(); II only I, III and IV IV only I and IV I and III

I and IV

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 III, IV, and V I, II, III, and IV I, III, and IV

I, III, and IV

A financial planner wants to calculate the average rate of return for clients. She does this by dividing the earnedIncome by the principal amount and displays the value as a double. Which of the following will correctly calculate and store the returnRate, assuming earnedIncome and principal are integers? I. double returnRate = earnedIncome / principal; II. double returnRate = (double) earnedIncome / principal; III. double returnRate = (double) (earnedIncome / principal); I only II only III Only II and III only I, II, III

II only

In order to ride the zip line, you need to be at least 12 years old and weigh at least 75 pounds, but no more than 300 pounds. Which of the following code segments would correctly determine if you can ride the zip line? I. if (age >= 12 && 75 <= weight <= 300) { return true; } return false; II. if (age >= 12) { if (weight >= 75 && weight <= 300) { return true; } } return false; III. if (age >= 12 && (weight >= 75 || weight <= 300)) { return true; } return false; I and II only I only II only III only I, II, III

II only

Consider the following class: public class Insect { private String name; private int numLegs; private boolean hasWings; private boolean hasExoskeleton; public Insect(String theName, int legNumber, boolean isWinged, boolean isExoskeleton) { name = theName; numLegs = legNumber; hasWings = isWinged; hasExoskeleton = isExoskeleton; } public Insect(String theName, int legNumber, boolean isWinged) { name = theName; numLegs = legNumber; hasWings = isWinged; hasExoskeleton = true; } } Which of the following is NOT a possible header for a new constructor for the Insect class? Insect(String theName, int legNumber) Insect(String theName, int legNumber, boolean isExoskeleton) Insect(String theName) Insect()

Insect(String theName, int legNumber, boolean isExoskeleton)

What is true of a void method?

It returns no value.

How do you square the minimum value between two values, such as x and y? Math.pow(Math.min(x, y), 2); Math.min(Math.pow(x, y)); Math.pow(Math.min(x, y)); Math.pow(2, Math.min(x, y));

Math.pow(Math.min(x, y), 2);

The following code is intended to return only even numbers. If an even number is passed to the method, it should return that number. If an odd number or zero is passed, it should return the next highest even number. 1: public int returnEven(int number) 2: { 3: if (number % 2 == 0) 4: { 5: return number; 6: } 7: else if (number == 0) 8: { 9: return number + 2; 10: } 11: else 12: { 13: return number + 1; 14: } 15: } Java 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 ifstatements. 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.

What is wrong with this method definition? public int printPayAmount(int amount) { System.out.println(amount); } This method should be private Nothing is returned from this method This is a String type method The method is never called

Nothing is returned from this method

What will this code output? if (true && true && false) { System.out.println("Hello Karel"); } if (true && 4 == 2 + 2) { System.out.println("Second if statement!"); } Hello Karel Hello Karel Second if statement! Second if statement! This program will print nothing

Second if statement!

Which of the following is not a primitive type? int double String boolean char

String

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. All of these choices are true

The memory associated with a variable of a primitive type holds an actual primitive value.

What does the method call tripleString("APCSA"); return for the following method? public String tripleString(String x) { return x * 3; } APCSAAPCSAAPCSA APCSA APCSA APCSA APCSA3 This method is improperly written. APCSA 3

This method is improperly written.

The value that a non-void method outputs is called

a return value

A company uses the following table to determine pay rate based on hours worked: Hours Worked Rate1-40$1041-50$1550+$20 The following method is intended to represent this table: public int calculateRate(int hours) { if (hours < 40) { return 10; } else if (hours < 50) { return 15; } else { return 20; } } Java Which of the following test cases can be used to show that the code does NOT work as intended? calculateRate(35); calculateRate(40); calculateRate(45); calculateRate(55);

calculateRate(40);

he 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.");

Which of the following values can correctly be saved in a boolean? True Yes 1 true yes

true

Consider the following class: public class Coin { private String name; private double value; public Coin(String theName, double theValue) { name = theName; value = theValue; } public double getValue() { return value; } } Assume that a Coin object quarter has been properly declared and initialized. Which of the following code snippets will successfully assign the value of quarter to a new variable coinWorth? quarter.getValue(); double coinWorth = quarter.getValue(); int coinWorth = quarter.getValue(); double coinWorth = quarter;

double coinWorth = quarter.getValue();

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? int myDouble = 1/4; double myDouble = (double) 1/4; double myDouble = (int) 1/4; double myDouble = (int) (1.0/4.0);

double myDouble = (double) 1/4;

What is the proper syntax to declare and initialize a variable called temperature to have the value 70.4? int temperature = 70.4; double temperature = 70.4; temperature = 70.4; dbl temperature = 70.4; temperature = (double) 70.4

double temperature = 70.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;

what does str="i am"; str+=10+3; return? What is printed as a result of executing the code segment? I am103years old I am 13 years old I am13years old I am 103 years old years oldI am 13

iam13

Joe's Pizza is creating a program that will calculate the total amount of money an employee earns each week. Employees are paid by the hour and only work in 1 hour increments. Salaries start at minimum wage, but employees get a $0.50 raise after the first month. Which variables would be the best to store the hours and salary of the employees? double hours int salary int hours double salary boolean hours double salary int hours int salary double hours boolean salary

int hours double salary

A teacher has calculated the gradeAverage as a double, but for report cards, she needs to report it rounded to the nearest whole number. Assuming that we round up from 0.5, which of the following will correctly round the gradeAverage? int rcGrade = (int) gradeAverage; int rcGrade = gradeAverage % 0.5; int rcGrade = (int) gradeAverage + 0.5; int rcGrade = (int) (gradeAverage + 0.5); int rcGrade = (int) gradeAverage - 0.5;

int rcGrade = (int) (gradeAverage + 0.5);

A procedure that is defined by the user is called a

method

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? This code would result in an error, as name has yet to be initialized. This code would result in an error, as there is no constructor present in the Dog class. " " null false

null

A science teacher wants to create an Elements class for the Periodic Table. They want to include several attributes to the Elements class: the atomic weight, the element name, and the atomic number for the element. For example, if you wanted to create an entry for Oxygen you would use the following data: Name: Oxygen Atomic Weight: 15.999 Atomic Number: 8 Which of the following instance variables makes the most sense based on these attributes? private int atomicWeight; private String name; private int atomicNum; private int atomicWeight; private String name; private double atomicNum; private double atomicWeight; private String name; private int atomicNum; private double atomicWeight; private String name; private double atomicNum; int atomicWeight; String name; int atomicNum;

private double atomicWeight; private String name; private int atomicNum;

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. Based on the information provided, which would be the most accurate class declaration for the DrinkOrder class? public class DrinkOrder { private String name; private int ounces; private boolean isIced; public DrinkOrder(String theName, int theOunces, boolean hasIce) { name = theName; ounces = theOunces; isIced = hasIce; } } public class DrinkOrder { private String name; private int ounces; private boolean isIced; public DrinkOrder(String name, int ounces, boolean isIced) { name = name; ounces = ounces; isIced = isIced; } } public class DrinkOrder { public DrinkOrder(String theName, int theOunces, boolean hasIce) { name = theName; ounces = theOunces; isIced = hasIce; } } DrinkOrder { private String name; private int ounces; private boolean isIced; public DrinkOrder(String theName, int theOunces, boolean hasIce) { name = theName; ounces = theOunces; isIced = hasIce; } }

public class DrinkOrder { private String name; private int ounces; private boolean isIced; public DrinkOrder(String theName, int theOunces, boolean hasIce) { name = theName; ounces = theOunces; isIced = hasIce; } }

Consider the following class: public class RaffleTicket { private String ownerName; private int ticketNum; public RaffleTicket(String name) { ownerName = name; ticketNum = getTicketNum(); } private int getTicketNum() { /* Code to be implemented */ } Which of the following can be used to replace /*Code to be implemented */ so that the code segment produces a random number between 1-100? return Math.random() * 100; return Math.random() * 100 + 1; return (int) (Math.random() * 100); return (int) (Math.random() * 100 +1); return (int) (Math.random() * 101);

return (int) (Math.random() * 100 +1);

Consider the following class: public class RaffleTicket { private String ownerName; private int ticketNum; public RaffleTicket(String name) { ownerName = name; ticketNum = getTicketNum(); } private int getTicketNum() { /* Code to be implemented */ } Which of the following can be used to replace /*Code to be implemented */ so that the code segment produces a random number between 1-100? return Math.random() * 100; return Math.random() * 100 + 1; return (int) (Math.random() * 100); return (int) (Math.random() * 100 +1); return (int) (Math.random() * 101);

return (int)(Math.random()*100+1)

Which expression returns the 1's place of an integer x? x % 10 x / 10 x % 100 x + 1

x % 10

What will the output of the following lines of code be? int x = 10; int y = x / 4; System.out.print("x + y = "); System.out.print(x + y); x + y = 12 x + y = 12 x + y = 12.5 x + y = 12.5 x + y = 102

x + y = 12

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

Given the following: if (x > y) { y *= 2; } else if (y > x) { x *= 2; } if (x > y) { y *= 2; } if (y > x) { x *= 2; } What will the final values of x and y be if their initial values are: x = 12; y = 5; x = 24 y = 20 x = 12 y = 20 x = 12 y = 10 x = 24 y = 5

x = 24 y = 20

What will the values of x and y be after this code segment runs? int x = 100; int y = 100; if (x <= 100) { if (y > 100) { x = 200; } else { x = 99; } } else { x++; } y = x + y; x = 100 y = 200 x = 99 y = 100 x = 101 y = 201 x = 99 y = 199

x = 99 y = 199

Assume y is a properly initialized positive integer. Which of the following will always result in a value of 1? y ++; y --; y += y; y /= y; y -= y;

y /= y;


Kaugnay na mga set ng pag-aaral

Anatomy 2 - Lecture Midterm Practice Questions - Quade

View Set

Spanish Capitals, Flags, And Currency

View Set

C# Type Conversion in Expressions

View Set

Architecture Since the Industrial Revolution

View Set

Leadership Skills Test 4 Combined

View Set

Kozier & Erbs Chapter 41: Spirituality

View Set