CS A Final Semester One

¡Supera tus tareas y exámenes ahora con Quizwiz!

The value that a non-void method outputs is called

a return value.

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 method call output given yesOrNo(true)? public String yesOrNo(boolean myBoolean) { if(myBoolean == true) { return "Yes"; } else { return "No"; } }

"Yes"

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

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

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

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

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

Consider the following code segment: int b = 10; String result = ""; while(b < 100) { result += b; b *= 2; } System.out.println(result); What, if anything, is printed as a result of executing this statement?

10204080

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

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

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.0

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

14

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

How many times will the word "Heyo!" be printed in the following code segment? int count = 3; while(count <= 7) { for(int i = 2; i < 5; i++) { System.out.println("Heyo!"); } count ++; }

15

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

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

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?

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

3

What is the output running Main.bar();? public class Main { private static int n = 0; public static void bar() { Main m1 = new Main(); Main m2 = new Main(); Main m3 = new Main(); m1.foo(); } public Main() { n = n + 1; } public void foo() { System.out.println(n); } }

3

What would the method call myMethod("Karel The Dog", 'e') output? 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; }

3

What will the call to the method funWithNumbers(314159) print to the screen? public void funWithNumbers(double myDouble) { int myInt = (int) myDouble; String myString = ""; while(myInt != 0) { myString = myInt % 10 + myString; myInt /= 10; } System.out.println(myString); }

314159

What would the call to method divideByTen(340) output? public int divideByTen(int num) { while(num / 10 >= 10) { num /= 10; } return num; }

34

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 value of x after this code runs? int x = 5; x = 10; x = 4;

4

Refer to this code snippet. public class Main { private static int n = 0; public static void bar() { Main m1 = new Main(); Main m2 = new Main(); Main m3 = new Main(); m1.foo(); } public Main() { n = n + 1; } public void foo() { System.out.println(n); } } Suppose the following method is added: public void setN(int newValue) { n = newValue; } What would be the output of the program if bar() were changed to the following: public static void bar() { Main m1 = new Main(); Main m2 = new Main(); Main m3 = new Main(); m1.setN(5); m3.foo(); }

5

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

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

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

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

Consider the following method: public int countPs(String word) { int sum = 0; for(int i = 0; i < word.length(); i++) { word = word.toLowerCase(); if(word.substring(i,i+1).equals("p")) { sum++; } } return sum; } What would the value of sum be after the method call countPs("Peter Piper picked a pack of pickled peppers")?

9

Which of the following statements is true about variables? A. The memory associated with a variable of a primitive type holds an actual primitive value. B. When a variable is declared final, its value can only be changed through a direct reassignment. C. A variable originally created as an int can be changed to store a double through casting. D. All of these choices are true

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

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.

Mark the valid way to create an instance of Athlete given the following Athlete class definition: public class Athlete { String first_name; String last_name; int jersey; public Athlete(String first_name, String last_name, int jersey) { this.first_name = first_name; this.last_name = last_name; this.jersey = jersey; } }

Athlete athlete = new Athlete("Dirk", "Nowitzki", 41);

What will the following code output when executed? 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

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

C

Which of the following is not part of the ACM Code of Ethics and Professional Conduct's General Ethical Principles?

Credit original creator when using other's work.

Which of the following would be the best example of how the Internet has impacted the economy?

Email has reduced the amount of mail that is produced and shipped.

Mark the valid way to create an instance of Foo given the following code: public class Foo { int bar; String stoo; public Foo() { this.bar = 0; this.stoo = ""; } public Foo(int bar) { this.bar = bar; stoo = "You."; } }

Foo fee; fee = new Foo();

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

Hello Karel

Consider the following method: public static void mix(String word, String letter) { int counter = 0; while(word.indexOf(letter) > 0) { if(word.substring(counter,counter + 1).equals(letter)) { word = word.substring(0, counter) + "0" + word.substring(counter +2, word.length()); } counter++; } System.out.println(word); } What value word would be printed as a result of the call mix("Hippopotamus", "p")?

Hi0o0tamus

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

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

Given the following definition for the class Athlete: public class Athlete { String first_name; String last_name; int jersey; public Athlete(String first_name, String last_name) { this.first_name = first_name; this.last_name = last_name; this.jersey = 0; } public Athlete(String first_name, String last_name, int jersey) { this.first_name = first_name; this.last_name = last_name; this.jersey = jersey; } } Which of the following are valid instantiations of the class Athlete? I - Athlete joe = new Athlete("Joe", "Montana"); II - Athlete joe = new Athlete("Joe, "Montana", "16"); III - Athlete joe = new Athlete("Joe", "Montana", 16);

I and III

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

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

Consider the following code segment: int counter = 5; int sum = 0; while(counter < 10) { sum+= counter; counter+=2; } Which of the following for loops would return the same value if System.out.println(sum) were printed? I. int sum = 0; for(int i = 0; i < 3; i++) { sum += i; } II. int sum = 11; for(int i = 0; i < 5; i ++) { sum+=i; } III. int sum = 0; for(int i = 5; i < 10; i+=2) { sum +=i; } IV. int counter = 5; int sum = 0; for(int i = counter; i < 10; i+=2) { sum+= counter; }

II and III

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

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)

What is true of a void method?

It returns no value.

Given this code snippet, public class Person { public String name; public Person(String name) { this.name = name; } public void changeName(String name) { this.name = name; } public void printName() { System.out.println(this.name); } } what will this code print? Person myPerson = new Person("Bob"); myPerson.changeName("Joe"); myPerson.name = "John"; myPerson.printName();

John

What will the following code output when executed? 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

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

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

Given the code snippet, public class Pokemon { private String name; private int health; public Pokemon(String name, int health) { name = name; health = health; } } what is wrong with the class definition? Hint : We expect the instance variables name and health to be initialized in the constructor.

Must use this in constructor when the constructor parameters have the same name as instance variables.

Given this code snippet, public class Athlete { public Athlete(String name) { this.name = name; } } what is missing from the class definition?

Need to declare the name instance variable

Consider the following code segment: for(int j = 0; j < 4; j++) //line 1 { for(int k = 0; k < j+1; k++) { System.out.print(k + ""); } } Which of the following best explains the result of changing j < 4 to j > 4 on line 1?

No output will be produced, as the boolean condition will never be met in the outer for loop.

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

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

Nothing is returned from this method

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.

Consider the following code segment: public static String mystery(String word, int i, int j) { String mystery = word.substring(i, i+ 1); mystery += word.substring(i, j); return mystery; } Which of the following is the most appropriate precondition for the variable i in mystery so that substring does not throw an exception?

Precondition: i >= 0, i < word.length, i <= j.

Consider the following code segment: public static String mystery(String word, int i, int j) { String mystery = word.substring(i, i+ 1); mystery += word.substring(i, j); return mystery; } Which of the following is the most appropriate precondition for the variable j in mystery so that substring does not throw an exception?

Precondition: j >= i and j <= word.length.

What kind of error would the method call myMethod("Frog") cause? public void myMethod(String x) { for(int i = 0; i <= x.length(); i++) { System.out.println(x.substring(i, i+1)); } }

Runtime Error: String index out of range

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!

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

String

Given the following code public class Storm { private int lighteningCount; private int precipTotal; private String precipType; public Storm(String precipType) { this.precipType = precipType; precipTotal = 0; l ighteningCount = 0; } public Storm(String precipType, int precipTotal) { this.precipType = precipType; this.precipTotal = precipTotal; lighteningCount = 0; } public Storm(String precipType, int lighteningCount) { this.precipType = precipType; this.lighteningCount = lighteningCount; precipTotal = 0; } }

The code will not compile because there are two constructors with the same signature.

What are parameters?

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

Given the following code: public class TvShow { private String name; private int channel; public TvShow (String name, int channel) { this.name = name; this.channel = channel; } public void getName() { return name; } public void setName(String name) { this.name = name; } } Which of the following explains why this code will not compile?

The return type for the getName method should be set to String.

Consider the following code segment: int num = 8; for(int i = num; i > 0; i -= 3) //Line 2 { System.out.print(" " + i + " "); } Which of the following best explains how changing i > 0 to i >= 0 will change the result?

There will be no change in the program because the for loop will iterate the same number of times.

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.

Given an instance of the Athlete class called athlete, what is the proper way to set the value of the jersey number after it has been instantiated? public class Athlete { private String first_name; private String last_name; private int jersey; public int getJersey() { return this.jersey; } public Athlete(String first_name, String last_name, int jersey) { this.first_name = first_name; this.last_name = last_name; t his.jersey = jersey; } }

You cannot set the jersey, since jersey is private and there is no setter method.

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

true

Given an instance of the Athlete class called athlete, what is the proper way to get the value of the jersey number? public class Athlete { private String first_name; private String last_name; private int jersey; public int getJersey() { return this.jersey; } public Athlete(String first_name, String last_name, int jersey) { this.first_name = first_name; this.last_name = last_name; this.jersey = jersey; } }

athlete.getJersey()

What is the output of the following program? public class Main { private String str = "bar"; public static void main(String[] args) { Main m = new Main("foo"); System.out.println(m.getString()); } public Main(String str) { str = str; } public String getString() { return str; } }

bar

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

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

Consider the following incomplete code segment, which is intended to increase the value of each digit in a String by one. For example, if num is 12345, then the resulting String would be 23456. String num = "12345"; int counter = 0; String result = ""; while(/* Missing Loop Header */) { int newNum = Integer.valueOf(num.substring(counter,counter+1)); result+= (newNum + 1); counter++; } System.out.println(result); Which of the following should replace /* Missing Loop Header */ so that the code segment works as intended?

counter < num.length()

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;

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 = 1.0 * a / b;

Which methods of class Foo can be called without an actual instance of the class Foo? public class Foo { public static void foo() { ... } public void bar() { ... } public void baz() { ... } }

foo()

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

Consider the following code segment: String word = "Cafeteria"; for(/* missing condition */) { System.out.print(word.substring(i+1,i+2) + " "); } The code segment is intended to print every other letter in the word, starting with index 0, to produce the result C f t r a. Which of the following can be used to replace /* missing condition */ so that the code segment works as intended?

int i = -1; i < word.length(); i+=2

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

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

The Insect class will contain a String for the insect's name, an int for the number of legs it has, and a boolean for whether or not the insect has wings. Which of the following is the most appropriate implementations for the Insect Class?

public class Insect { private String name; private int numLegs; private boolean hasWings; //constructor and methods not shown }

Write a method that will ask for user input until user inputs the String "no". Allow the user to input any capitalization of the String "no" ("no", "No", "NO", "nO") Also, have the method return the number of loops. Assume that a Scanner object input has already been created.

public int loopTillNo() { int count = 0; String nextLine = ""; while(!nextLine.toLowerCase().equals("no")) { nextLine = input.nextLine(); count++; } return count; }

Write a method that loops until the user inputs the correct secret password or until the user fails to enter the correct password 10 times. The secret password the program should look for is the String "secret" Assume that a Scanner object called input has been correctly initialized.

public void secretPassword() { int count = 0; while(true) { if(count == 10) { System.out.println("You are locked out!"); return; } String readLine = input.nextLine(); if(readLine.equals("secret")) { System.out.println("Welcome!"); return; } count++; } }

Given this Athlete class, which of the following setter methods for the jersey variable is correct? public class Athlete { private String first_name; private String last_name; private int jersey; public int getJersey() { return this.jersey; } public Athlete(String first_name, String last_name, int jersey) { this.first_name = first_name; this.last_name = last_name; this.jersey = jersey; } }

public void setJersey(int jersey) { this.jersey = jersey; }

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

Tree question

tree question will not appear on test

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

weekday && !holiday

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

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

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

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

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

Consider the following method: public static String mystery(String word, int index) { String result = ""; for(int i = word.length()- 1; i > index; i--) { result += word.substring(i, word.length()); } return result; } What is returned as a result of the call mystery("Lunchbox", 4)?

xoxbox

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

y /= y;


Conjuntos de estudio relacionados

Sources and Uses of short-term and long-term funds

View Set

Microeconomics Final Ch. 10,11,12,13

View Set

Chapter One: Introduction to the Human Body

View Set

Health Assessment: Chapter 20: Peripheral Vascular and Lymphatic System

View Set

Energy Careers Education and Qualifications

View Set