comp sci

Ace your homework & exams now with Quizwiz!

Which of the following is not a primitive type?

String

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

14

What are parameters?

The formal names given to the data that gets passed into a method

The value that a non-void method outputs is called

a return value.

What will this program print if the value of grade is 80?

c

Assuming a and b are properly initialized boolean values, which expression would be equivalent to the following: !(a && b)

!a || !b

Which expression is true?

!false || !true

What will this code return given checkMethod(false, true)? public int checkMethod(boolean x, boolean y) { if(!x) { if(y) { return -1; } else { return 1; } } else { return 0; } }

-1

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

6

Given the following call, what value would be returned for 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); } }

89

What is true of a void method?

It returns no value.

Which of the following code segments would successfully square the minimum value between two intvariables x, y?

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

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 can catch fish.")

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

17 Dec, 2018

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

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

What is wrong with this method definition? public int printPayAmount(int amount) { System.out.println(amount); }

Nothing is returned from this method

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 DrinkOrderclass?

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; } }

Assuming weekday and holiday are properly initialized booleans, which expression would be equivalent to the following: !(!weekday || holiday)

weekday && !holiday

if (x > y) { y *= 2; } else if (y > x) { x *= 2; } if (x > y) { y *= 2; } if (y > x) { x *= 2; }

x = 24 y = 20

Given a and b as properly initialized integers, which of the following will result in a correct calculation with a decimal answer?

double y = 1.0 * a / b;

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

4

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, boolean isExoskeleton)

Which expression returns the 1's place of an integer x?

x % 10

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 output will be produced by System.out.println("Hello"); System.out.println("Karel");

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 and II Only

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

5

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

"okemonpay"

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

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

Which of the following values can correctly be saved in a boolean?

true

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?

int hours double salary

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

11

What is the result of the following expression when x is 125? x % 6

5

What is the proper syntax to declare and initialize a variable called temperature to have the value 70.4?

double temperature = 70.4;

What will this method call output given yesOrNo(true)?

"Yes"

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.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 the DrinkOrder class is latte.

String breakfast = new String("Pizza"); String lunch = new String("Pizza"); String dinner = breakfast; if (breakfast==lunch) { System.out.print("A"); } if (breakfast.equals(lunch)) { System.out.print("B"); } if (breakfast==dinner) { System.out.print("C"); } else if (breakfast.equals(dinner)) { System.out.print("D"); } else { System.out.print("E"); }

BC

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:

Greetings hi = new Greetings("Hello"); hi.translate(); hi.greeting(); Greetings hola = new Greetings("Hello"); hola.changeGreeting("Hola"); hola.greeting();

Consider the following code segment: String str = "I am"; str += 10 + 3; String age = "years old"; System.out.println(str + age); What is printed as a result of executing the code segment?

I am13years old

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 and III only

Given the following statements, which options will print true? 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 and iii

String word = "elementary"; if (word.length() > 8) { System.out.println("Long Word"); } if (word.length() > 5) { System.out.println("Medium Word"); } else if (word.length() > 0) { System.out.println("Short Word"); } else { System.out.println("No Word"); }

Long Word Medium Word

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.

No. Zero will get returned on line 5 and not make it to line 7.

Which of the following statements is true about variables?

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; }

This method is improperly written.

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 + 0.5);

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. Which of the following instance variables makes the most sense based on these attributes?

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

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 (int) (Math.random() * 100 +1);

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

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 = 12 y = 12

A procedure that is defined by the user is called a

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!"); }

Second if statement!

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 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 = 99 y = 199

A 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

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

y /= y;

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

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 becausex / y calculates to 7.66 then the cast to an int results in the value getting truncated to 7

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 does this method call doubleNumber(7.8); return, given the following method? public double doubleNumber(double myNumber) { return (double) (int) myNumber * 2; }

14.0

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

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);

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;

II only

Given a, b, and c are properly initialized boolean values, what values would make the following expression false? (a || b) || (b || c) || (!a || b);

Nothing. The expression will always be true.

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; } Which of the following test cases can be used to show that the code does NOT work as intended?

calculateRate(40);

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?

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?

double myDouble = (double) 1/4;


Related study sets

Chapter 49: Introduction to the Endocrine System

View Set

Positive and Negative Space Quick Check

View Set

Chapter 2 An Introduction to ABA Concepts

View Set

Week 2 | PrepU | Chapter 27 - ML 5 | Safety, Security, and Emergency Preparedness

View Set