APCS final review

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Which of the following will be treated as a comment? \\ // / /*....*/ \*....*\

//

Examine the following code: int count = 1; while ( count < 5 ) { System.out.print( count + " " ); } System.out.println( ); What does this code print on the monitor? 1 2 3 4 1 2 3 4 5 2 3 4 1 1 1 1 1 1 1 1 1 1 1 . . . .

1 1 1 1 1 1 1 1 1 1 1 . . . .

What does the following code print? System.out.println(2 + 3 * 5 - 1); 24 14 This will give a compile time error. 16

16

Assume that boolean a = true and boolean b = false. Which of the following is equivalent to ! ( !( a || b ) && ( b && a ) ) ( a || b ) || ( !b || !a ) ( a || b ) && ( b && a ) ( !a && !b ) && ( b || a ) ( a && b ) || ( !b || !a ) None of the given choices are correct.

( a || b ) || ( !b || !a )

Which of the following is equivalent to !(x != 3 || y > 4) && false ( x > 3 ) || ( y > 4 ) && false ( x == 3 ) && ( y <= 4 ) && false ( x < 3 ) || ( y >= 4 ) ( x == 3 ) && ( y <= 4 ) || true None of the given choices are correct.

( x == 3 ) && ( y <= 4 ) && false

Given a String, s, which is assumed to have at least one character in it, which of the following conditions would determine if the first character of the String is the same as the last character? (s.charAt(0) == s.charAt(s.length( ))) (s.charAt(1) == s.charAt(s.length( ))) (s.charAt(0) == s.charAt(s.length( ) - 1)) (s.charAt(0).equals(s.charAt(s.length( ) - 1))) None of the given answers are correct.

(s.charAt(0) == s.charAt(s.length( ) - 1))

/* missing precondition */ public String getiToEnd(String str, int i) { return str.substring(i, str.length()); } /* Precondition: i >= 0 */ /* Precondition: i <= str.length() */ /* Precondition: 0 < i < str.length() */ /* Precondition: 0 <= i < str.length() */

/* Precondition: 0 <= i < str.length() */Check MeCompare me

public class TestScore { private String studentName; private double score; private double extraCredit; public TestScore (String n, double s, double ec) { studentName = n; score = s; extraCredit = ec; } /* Other methods not shown */ } Which of the following preconditions are reasonable for the TestScore constructor? /* Precondition: s <= 0 */ /* Precondition: score >= 0 */ /* Precondition: s and ec >= 0 */ /* Precondition: n is not the empty String */ /* Precondition: studentName is not the empty String */

/* Precondition: s and ec >= 0 */ /* Precondition: n is not the empty String */

Each of the following code segments is intended to print the word Apple. Which of the following code segments works as intended? 1. System.out.print("Apple"); 2. System.out.print(Apple); 3. System.out.print(Ap); System.out.print(ple); 1 only 2 only 3 only 1 and 2 only 2 and 3 only

1 only

What is the value of length after the following executes? String s1 = "Hey, buddy!"; int len = s1.length(); 8 10 11

11

What is output by the code below? System.out.print(3.0 / 6 + 3 * 3.5); 11.0 10.5 0.18 12.25 1.17

11.0

Given the BankAccount class definition below, what is the output of the code in the main method? (bank) 100.00 110.00 90.00 10.00

110.00

What does the following code print? System.out.println("13" + 5 + 3); A. 21 B. 1353 C. It will give a run-time error D. 138 E. It will give a compile-time error

1353

What is output by the code below? intx = 4; x--; x--; System.out.println( x ); x++; 5 2 8 3 4

2

How many stars are output when the following code is executed? for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) System.out.println("*"); } 10 5 50 25 15

25

What is output by the code below? int x = 15; x /= 5; System.out.print(x); 0 1 2 3 5

3

What is the value of pos after the following code executes? String s1 = "ac ded ca"; int pos = s1.indexOf("d"); 3 4 5 -1

3

What is output by the code below? String w1 = "bat"; String w2 = "bot"; String w3 = "bet"; System.out.println( w3.compareTo( w1 )); 0 true 4 -4 false

4

What is printed as a result of the following code segment? for (int k = 0; k < 20; k+=2) { if (k % 3 == 1) System.out.print(k + " "); } 0 2 4 6 8 10 12 14 16 18 4 16 0 6 12 18 1 4 7 10 13 16 19 4 10 16

4 10 16

What is output by the code below? int x = 5, y = 2; int z = x % y; double a = z * 5; System.out.print(a); 5 10 1 7 5.0

5.0

Given the following code segment, what is the value of b when it finishes executing? double a = 9.6982; int b = 12; b = (int) a; 9.6982 12 10 9

9

Fill in the blank so people under 21 years are offered Grape Soda. if ( age _________ 21 ) System.out.println("How about a Brew?"); else System.out.println("Care for some Grape Soda?"); < == != >=

>=

What two steps are performed when an assignment statement is executed? A. The expression on the right of the "=" is evaluated, "using" all the variables it contains. The result of evaluation is assigned to the variable on the left of the "=". B. All appropriate variables are incremented or decremented. The result is assigned to the variable on the left of the "=". C. All appropriate variables are incremented. All appropriate variables are decremented. D. The arithmetic expression is evaluated and assigned to the variable on the left of the "=". Variables are auto-incremented or auto-decremented. A B C D

A

Which of the following statements is false? An object is an instantiation of a class. A real class must have a main method. A real class is the concept of what each object knows about itself and its behaviors. All objects derive from the Object class. A real class must define its own constructor.

A real class must have a main method. A real class must define its own constructor.

Which of the following statements is true? A loop will always run at least once. A while loop is typically used when you don't know how many times the loop will execute. An infinite loop is a loop for which the condition is always false. A while loop has 3 parts: initialization, condition, and increment. A validation loop looks for a value in input to find its's termination condition.

A while loop is typically used when you don't know how many times the loop will execute.

What will be the output of the following program? (a and b) A is less than B A is equal to B A is greater than B Compilation Error

Compilation Error

What should be in the body of the constructor? Nothing; constructors do not have bodies. A call to the constructor in the Object class. An assignment statement for each of the instance variables. An instantiation of the object. The hexadecimal address of the object's data.

An assignment statement for each of the instance variables.

(blank) A B C D

B

Consider the following class. Which of the following code segments would successfully create a new Movie object? (movie image) A. Movie one = new Movie("Harry Potter", "Bob"); B. Movie two = new Movie("Sponge Bob"); C. Movie three = new Movie(title, rating, director); D. Movie four = new Movie("My Cool Movie", "Steven Spielburg", "4.4"); E. Movie five = new Movie(t);

B. Movie two = new Movie("Sponge Bob");

(star) A B C D

C

Consider the following code segment. String first = new String("welcome"); String second = new String("welcome"); String third = new String("gobye"); if (first == second) { System.out.print("A"); } else if (second == third) { System.out.print("B"); } else if (first.equals(second)) { System.out.print("C"); } else if (second.equals(third)) { System.out.print("D"); } else { System.out.print("E"); } What is printed as a result of executing the code segment? A B C D E

C

Consider the following code segment. System.out.println("A"); System.out.println("B"); System.out.print("C"); System.out.print("D"); What is printed as a result of executing the code segment? A) ABCD B) A BCD C) AB CD D) A B CD E) A B C D A B C D E

D

There is a method called checkString that determines whether a string is the same forwards and backwards. The following data set inputs can be used for testing the method. What advantage does Data Set 2 have over Data Set 1? Data Set 1 aba abba aBa Data Set 2 bcb bcd Data Set 2 contains one string which should return true and one that should return false. All strings in Data Set 2 have the same number of characters. The strings in Data Set 2 are all lowercase Data Set 2 contains fewer values than Data Set 1. There are no advantages.

Data Set 2 contains one string which should return true and one that should return false.

Consider the following code segment. System.out.println(welcome); // Line 1 System.out.print(class); // Line 2 The code segment is intended to produce the following output but does not work as intended. welcome class Which of the following changes can be made so that the code segment produces the intended output? Inserting System.out.print(); between lines 1 and 2 Inserting System.out.println(); between lines 1 and 2 Changing println in line 1 to print Changing print in line 2 to println Enclosing welcome in line 1 and class in line 2 in quotation marks

Enclosing welcome in line 1 and class in line 2 in quotation marks

Assume that SomeClass and MainClass are properly defined in separate files. What is the output of the code in main()? (someclass) Hello Bob Hello Hello Bob Hello Bob Hello Bob Hello Bob Hello

Hello Hello Bob

Given the following code segment, what is the value of s1 after the code executes? String s1 = "Hi There"; String s2 = s1; String s3 = s2; String s4 = s1; s2 = s2.toLowerCase(); s3 = s3.toUpperCase(); s4 = null; null hi there HI THERE Hi There hI tHERE

Hi There

Which of the following code segments will produce the displayed output? 11111 2222 333 44 5 I. for (int j = 1; j <= 5; j++) { for (int k = 5; k >= j; k--) { System.out.print(j); } System.out.println(); } II. for (int j = 1; j <= 5; j++) { for (int k = 5; k >= 1; k--) { System.out.print(j); } System.out.println(); } III. for (int j = 1; j <= 5; j++) { for (int k = 1; k <= j; k++) { System.out.print(j); } System.out.println(); } IV. for (int j = 1; j <= 5; j++) { for (int k = 1; k <= 5; k++) { System.out.println(j); } } V. for (int j = 1; j <= 5; j++) { for (int k = j; k <= 5; k++) { System.out.print(k); } System.out.println(); } I II III IV V

I

At a certain high school students receive letter grades based on the following scale: 93 or above is an A, 84 to 92 is a B, 75 to 83 is a C, and below 75 is an F. Which of the following code segments will assign the correct string to grade for a given integer score? I. if (score >= 93) grade = "A"; if (score >= 84 && score < 93) grade = "B"; if (score >=75 && score < 84) grade = "C"; if (score < 75) grade = "F"; II. if (score >= 93) grade = "A"; if (score >= 84) grade = "B"; if (score >=75) grade = "C"; if (score < 75) grade = "F"; III. if (score >= 93) grade = "A"; else if (score >= 84) grade = "B"; else if (score >= 75) grade = "C"; else grade = "F"; I and III only II only III only I and II only I, II, and III

I and III only

In the code segment below, the int variable temp represents a temperature in degrees Fahrenheit. The code segment is intended to print a string based on the value of temp. The following table shows the string that should be printed for different temperature ranges. (table) I only II only I and II only II and III only I, II, and III

I only

Consider the following class definition. public class Student { int studentID; int gradeLevel; boolean honorRoll; public Student(int s, int g){ studentID = s; gradeLevel = g; honorRoll = false; } public Student(int s){ studentID = s; gradeLevel = 10; honorRoll = false; } } Which of the following code segments would successfully create a new Student object? I. Student one = new Student(121, 1); II. Student two = new Student(122); III. int id = 123; int grade = 11; Student three = new Student(id, grade); I only II only III only I and II only I, II and III

I, II and III

After the following code is executed, which of I, II and/or III will evaluate to true? String s1 = "xyz"; String s2 = s1; String s3 = s2; I. s1.equals(s3) II. s1 == s2 III. s1 == s3 I, II, III I only II only III only II and III only

I, II, III

Given the following code segment, which of the following is true? String s1 = new String("Hi There"); String s2 = new String("Hi There"); String s3 = s1; I. (s1 == s2) II. (s1.equals(s2)) III. (s1 == s3) IV. (s2.equals(s3)) A. II and IV B. II, III, and IV C. I, II, III, IV D. II only E. IV only

II, III, and IV

Suppose a program is a client of the Player class. Here is a snippet of code contained in the program Player firstPlayer = new Player("Karan", "Warrior", "Atlanta", 180); Looking at the documentation of the class, you find the signature for the constructor, shown below. Player Player(String name, String role, String location, int health); Where would you find the formal parameters? In the program. In the library. In the documentation. Both the program and the documentation contain formal parameters.

In the documentation

What are the values of a, b, and c after this code segment has been executed? int a = 1; int b = 0; int c = -1; if ((b + 1) == a) { b++; c += b; } if (c == a) { a--; b = 4; } a = 0, b = 4, and c = 0 a = 0, b = 4, and c = 1 a = 1, b = 0, and c = -1 a = 1, b = 1, and c = 0 a = 1, b = 1, and c = 1

a = 1, b = 1, and c = 0

Consider the following class. Which of the following code segments will construct a Movie object m with a title of "Lion King" and rating of 8.0? (lion king image) Movie m = new Movie(8.0, "Lion King"); Movie m = Movie("Lion King", 8.0); Movie m = new Movie(); Movie m = new Movie("Lion King", "Disney", 8.0); Movie m = new Movie("Lion King");

Movie m = new Movie("Lion King", "Disney", 8.0);

Which of the following code segments will correctly create an instance of a Person object? public class Person { private String name; private int age; public Person(String a, int b) { name = a; age = b; } } A. new Person john = Person("John", 16); B. Person john("John", 16); C. Person john = ("John", 16); D. Person john = new Person("John", 16); E. Person john = new Person(16, "John");

Person john = new Person("John", 16);

Consider the following PointD class. public class PointD { double xCoord; double yCoord; public PointD(double x, double y) { xCoord = x; yCoord = y; } } Which of the following code segments, appearing in a class other than PointD, will correctly create an instance of a PointD object? PointD p = (2.5, 4.8); PointD p = PointD(2.5, 4.8); new p = PointD(2.5, 4.8); new PointD = p(2.5, 4.8); PointD p = new PointD(2.5, 4.8);

PointD p = new PointD(2.5, 4.8);

Consider the following class definition that defines a Liquid class with a boilingPoint, a currentTemperature, and a freezingPoint. For example, Liquid water = new Liquid(100, 50, 0); defines a water object with a boiling point of 100, a current temperature of 50, and a freezing temperature of 0. public class Liquid { private int boilingPoint; private int currentTemp; private int freezingPoint; public Liquid(int bp, int ct, int fp) { boilingPoint = bp; currentTemp = ct; freezingPoint = fp; } /* Other methods not shown */ } Which of the following preconditions is reasonable for the Liquid constructor? Precondition: fp < ct < bp Precondition: fp > 0 Precondition: currentTemp > 0 Precondition: fp > ct > bp

Precondition: fp < ct < bp

Assume that count is 0, total is 20 and max is 1. The following statement will do which of the following? if (count != 0 && total / count > max) max = total / count; The condition short circuits and the assignment statement is not executed The condition short circuits and the assignment statement is executed without problem The condition does not short circuit causing a division by zero error The condition short circuits so that there is no division by zero error when evaluating the condition, but the assignment statement causes a division by zero error The condition will not compile because it uses improper syntax

The condition short circuits and the assignment statement is not executed

Given the following Boolean expression, with numerical variables x and y, which of the following is the best description of its evaluation? !( x == 9 || x <= y ) The expression is always true. The expression is always false. The expression is true when x = 8 and y = 10 but false when x = 9 and y = 11. The expression is false when x = 9 and y = 10 but true when x = 7 and y = 6. None of the given choices are correct.

The expression is false when x = 9 and y = 10 but true when x = 7 and y = 6.

What are parameters? The value that a method returns. The formal values that a method prints to the screen. The formal names given to the data that gets passed into a method. The type that is given to a variable.

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

What is wrong, logically, with the following code? if (x > 10) System.out.println("Large"); if (x > 6 && x <= 10) System.out.println("Medium"); if (x > 3 && x <= 6) System.out.println("Small"); else System.out.println("Very small");(4/4 Points) There is no logical error, but there is no need to have (x > 6) in the second conditional or (x > 3) in the third conditional The logical error is that unless 3 < x <= 6 , "Very small" is always printed out There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x <= 6) in the third conditional The logical error is that no matter what value x is, "Large" is always printed out There is nothing wrong with the logic at all

The logical error is that unless 3 < x <= 6 , "Very small" is always printed out

Consider the following code segment. for (int j = 0; j < 4; j++) { for (int k = 0; k < j; k++) { System.out.println("hello"); } } Which of the following best explains how changing the inner for loop header to for (int k = j; k < 4; k++) will affect the output of the code segment? The output of the code segment will be unchanged. The string "hello" will be printed three fewer times because the inner loop will iterate one fewer time for each iteration of the outer loop. The string "hello" will be printed four fewer times because the inner loop will iterate one fewer time for each iteration of the outer loop. The string "hello" will be printed three additional times because the inner loop will iterate one additional time for each iteration of the outer loop. The string "hello" will be printed four additional times because the inner loop will iterate one additional time for each iteration of the outer loop.

The string "hello" will be printed four additional times because the inner loop will iterate one additional time for each iteration of the outer loop.

Consider the following code segment: /* data type 1 */ m = 0.5; /* data type 2 */ n = true; Which of the following best describes the data types that should be used to replace/* data type 1 */ and /* data type 2 */ so that the code segment compiles without error? The variable m should be declared as an int and the variable n should be declared as a boolean. The variable m should be declared as an int and the variable n should be declared as an int. The variable m should be declared as a double and the variable n should be declared as an int. The variable m should be declared as a double and the variable n should be declared as a boolean. The variable m should be declared as a boolean and the variable n should be declared as a boolean. Option 5

The variable m should be declared as a double and the variable n should be declared as a boolean.

Consider the following code that will assign a letter grade of 'A', 'B', 'C', 'D', or 'F' depending on a student's test score. (grade) This code will work correctly in all cases This code will work correctly only if grade >= 60 This code will work correctly only if grade < 60 This code will work correctly only if grade < 70 This code will not work correctly under any circumstances

This code will work correctly only if grade < 70

A car dealership needs a program to store information about the cars for sale.For each car, they want to keep track of the following information: the number of doors (2 or 4),its average number of miles per gallon, and whether the car has air conditioning. Which of the following is the best design? A. Use one class, Car, which has three attributes: int numDoors, double mpg, and boolean hasAir. B. Use four unrelated classes: Car, Doors, MilesPerGallon, and AirConditioning C. Use a class, Car, which has three subclasses: Doors, MilesPerGallon, and AirConditioning D. Use a class Car, which has a subclass Doors, with a subclass AC, with a subclass MPG. E. Use three classes: Doors, AirConditioning, and MilesPerGallon, each with a subclass Car.

Use one class, Car, which has three attributes: int numDoors, double mpg, and boolean hasAir.

Consider the following code segment. int a; int b; a = 3; b = /* missing expression */; a = 1 + 2 * b; System.out.print(a); System.out.println(b); Which of the following can be used as a replacement for /* missing expression */ so that the code segment prints 94 ? 3 a a - 1 a + 1 1 - 2 * a

a+1

Consider the following class. public class WindTurb { double efficiencyRating; public WindTurb( ) { efficiencyRating = 0.0; } public WindTurb(double e) { efficiencyRating = e; } } Which of the following code segments, when placed in a method in a class other than WindTurb, will construct a WindTurb object wt with an efficiencyRating of 0.73 ? WindTurb wt = new WindTurb(0.73); WindTurb wt = 0.73; new WindTurb wt = 0.73; WindTurb wt = new WindTurb(); wt = 0.73; WindTurb wt = new WindTurb(); wt.efficiencyRating = 0.73;

WindTurb wt = new WindTurb(0.73);

What is the output of the following code? (woo) Woo Hoo Hoo Woo Hoo Woo Hoo Woo Hoo Woo Hoo Woo Woo Hoo Hoo

Woo Hoo Woo Hoo

What will be the output of the following program? (win) You win You lose You win the prize. You lose the prize.

You lose the prize.

What is the output of the following: int a = 0; int b = 10; a = --b ; System.out.println("a: " + a + " b: " + b ); a: 9 b:11 a: 10 b: 9 a: 9 b:9 a: 0 b:9

a: 9 b:9

Assume that the int variables a, b, c, and low have been properly declared and initialized. The code segment below is intended to print the sum of the greatest two of the three values but does not work in some cases. if (a > b && b > c) { low = c; } if (a > b && c > b) { low = b; } else { low = a; } System.out.println(a + b + c - low); For which of the following values of a, b, and c does the code segment NOT print the correct value? a = 1, b = 1, c = 2 a = 1, b = 2, c = 1 a = 1, b = 2, c = 3 a = 2, b = 2, c = 2 a = 3, b = 2, c = 1

a = 3, b = 2, c = 1

Given the following code segment, what is the value of num when it finishes executing? Math.random() returns a random decimal number between 0 and up to 1, for example 0.4. double value = Math.random(); int num = (int) (value * 11) - 5; a random number from 0 to 10 a random number from 0 to 9 a random number from -5 to 4 a random number from -5 to 5

a random number from -5 to 5

Given the following code segment, what is the value of num when it finishes executing? Math.random() returns a random decimal number between 0 and up to 1, for example 0.4. double value = Math.random(); int num = (int) (value * 5) + 5; a random number from 0 to 4 a random number from 1 to 5 a random number from 5 to 9 a random number from 5 to 10

a random number from 5 to 9

A student has created a Cat class. The class contains variables to represent the following. - A String variable called color to represent the color of the cat - A String variable called breed to represent the breed of the cat - An int variable called age to represent the age of the cat The object myCat will be declared as type Cat. Which of the following descriptions is accurate? An attribute of breed is String. color, breed, and age are instances of the Cat class. Cat is an instance of the myCat class. age is an attribute of the myCat object. An attribute of Cat is myCat

age is an attribute of the myCat object

What of the following is not a legal reserved word for a java data type? boolean short character double float

character

Of the following if statements, which one correctly executes three instructions only if the condition is true? (conditions) a b c d e

d

Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x is negative but leave x alone if x is 0? (statments) a b c d e

d

Given the following method definition from a real class, which of the following invocations would be valid from client code? Assume an object named xyz has been properly instantiated in the client code. public double product(double a, int b) { /* body intentionally blank */ } double answer = xyz.product( 4.2, 5 ); double answer = product.xyz (4.2, 5); int answer = xyz.product( 4.2, 5 ); int answer = xyz.product( 5, 4.2 ); double answer = product.xyz( 5, 4.2 );

double answer = xyz.product( 4.2, 5 );

Which statement correctly declares a variable that can store a temperature rounded to the nearest tenth of a degree? boolean patientTemp; double patientTemp; int patientTemp; patientTemp = 0; patientTemp = 0.0;

double patientTemp;

What would the following print? int x = 3; int y = 2; if (x > 2) x++; if (y > 1) y++; if (x > 2) System.out.print("first "); if (y < 3) System.out.print("second "); System.out.print("third"); first first second first second third first third third

first third

Which for loop is equivalent to this while loop? int y = 7; while (y >= 0) { System.out.println(y); y--; } for ( int y = 0; y < 7; y ++) System.out.println(y); for ( int y = 7; y > 0; y --) System.out.println(y); for ( int y = 0; y > 7; y ++) System.out.println(y); for ( int y = 0; y > 7; y --) System.out.println(y); for ( int y = 7; y >= 0; y --) System.out.println(y);

for ( int y = 7; y >= 0; y --) System.out.println(y);

What does the following code fragment write to the monitor? (temp) Hot Moderate Chilly Frigid

frigid

A student has created a Song class. The class contains the following variables. A String variable called artist to represent the artist name A String variable called title to represent the song title A String variable called album to represent the album title The object happyBirthday will be declared as type Song. Which of the following statements is true? artist, title, and album are instances of the Song class. happyBirthday is an instance of three String objects. happyBirthday is an instance of the Song class. Song is an instance of the happyBirthday object. Song is an instance of three String objects.

happyBirthday is an instance of the Song class.

Which of the following is equivalent to the code segment below? if (x > 2) x = x * 2; if (x > 4) x = 0; x = 0; if (x > 2) x *= 2; if (x > 2) x = 0; if (x > 2) x = 0; else x *= 2;

if (x > 2) x = 0;

Which of the following is a syntactically correct import statement for the Scannerclass? Scanner scan = new Scanner (System.in); import java.lang.scanner; int value = scan.nextDouble( ); import java.util.Scanner; int value = scan.nextInt;

import java.util.Scanner;

What is printed as a result of executing the code segment? String myString = new String("my string"); String yourString = new String(); yourString = "my string"; boolean dotEquals = myString.equals(yourString); boolean equalsEquals = (myString == yourString); System.out.print(dotEquals + " " + equalsEquals); true true true false false true false false my string my string

true false

Which of the following is NOT a valid invocation of a Scanner method? String city = scan.next( ); int value1 = scan.nextDouble; double total = scan.nextDouble( ); int value1 = scan.nextInt( ); String state = scan.nextLine( );

int value1 = scan.nextDouble;

Given that s is a String, what does the following loop do? for(int j = s.length( )- 2; j >= 0; j--) { System.out.print(s.charAt(j)); } it yields a run-time error because there is no character at s.charAt(j-1) for j = 0 it prints s out backwards but does not print the original 0th character it prints s out backwards it prints s out backwards after skipping the original last character it prints s out forwards

it prints s out backwards after skipping the original last character

What does the following code do when it is executed? System.out.println(5 / 0); It will print 0 It will give a run-time error It will give a compile-time error (won't compile) It will print 5

it will give a run-time error

Which of the following is a valid identifier in Java? 123java main java_1234 ab ce whoot}

java_1234

Which of the following Scanner methods would allow the user to enter the string data "Yellow Jackets" ? next() nextString() nextLn() nextLine() nextInt()

nextLine()

What is output from the following code? String s = "Georgia Tech"; String s1 = s.substring(0,7); String s2 = s1.substring(2); String s3 = s2.substring(0,3); System.out.println(s3); org eor eorg orgi You will get an index out of bounds exception

org

Consider this code snippet that uses a class called Rectangle. int roomHeight = 15; int roomWidth = roomHeight * 3; Rectangle room = new Rectangle(roomHeight, roomWidth); Which of the following is a reference variable? room roomHeight roomWidth Rectangle

room

A student has created a Movie class. The class contains variables to represent the following. - A String variable called title to represent the title of the movie - A String variable called director to represent the director of the movie - A double variable called rating to represent the rating of the movie The object scaryMovie will be declared as type Movie. Which of the following descriptions is accurate? An attribute of the scaryMovie class is title. scaryMovie is an instance of the Movie class. Title, director, and rating are instances of the scaryMovie object. An attribute of the Movie instance is scaryMovie Movie is an instance of scaryMovie.

scaryMovie is an instance of the Movie class.

Consider the following outline of a nested if-else structure which has more if clauses than else clauses. Which of the statements below is true regarding this structure? if (condition1) { if (condition2) statement1; } else statement2; syntactically it is invalid to have more if clauses than else clauses statement2 will only execute if condition1 is false and condition2 is false statement2 will only execute if condition1 is true and condition2 is false statement2 will only execute if condition1 is false, it does not matter what condition2 is statement2 will never execute

statement2 will only execute if condition1 is false, it does not matter what condition2 is

Consider the following expression. (3 + 4 == 5) != (3 + 4 >= 5) What value, if any, does the expression evaluate to? true false 5 7 No value; relational operators cannot be used on arithmetic expressions.

true

What output is produced when this code segment is executed? boolean a = true; boolean b = true; System.out.print( ( b || ( !a || b ) ) + " " ); System.out.print( ( ( !b || !a ) && a ) + " " ); System.out.println( !( a && b) && b ); true true true true false true true false false false true false false false false

true false false

What are the values of var1 and var2 after the following code segment is executed and the while loop finishes? int var1 = 0; int var2 = 2; while ((var2 != 0) && ((var1 / var2) >= 0)) { var1 = var1 + 1; var2 = var2 - 1; } var1 = 0, var2 = 2 var1 = 1, var2 = 1 var1 = 3, var2 = -1 var1 = 2, var2 = 0 The loop won't finish executing because of a division by zero.

var1 = 2, var2 = 0

Consider the following incomplete code segment, which is intended to print the sum of the digits in num. For example, when num is 12345, the code segment should print 15, which represents the sum 1 + 2 + 3 + 4 + 5. int num = 12345; int sum = 0; /* missing loop header */ { sum += num % 10; num /= 10; } System.out.println(sum); Which of the following should replace /* missing loop header */ so that the code segment will work as intended? while (num > 2) while (num > 1) while (num >= 0) while (num > 0) while (num > sum)

while (num > 0)

Consider the following code segment, which is intended to store the sum of all multiples of 10 between 10 and 100, inclusive (10 + 20 + ... + 100), in the variable total. int x = 100; int total = 0; while( /* missing code */ ) { total = total + x; x = x - 10; } Which of the following can be used as a replacement for /* missing code */ so that the code segment works as intended? x < 100 x <= 100 x > 10 x >= 10 x != 10

x >= 10

What does the following code print when x has been set to 187? (x) x is negative x is zero x is positive

x is positive

What are the values of x, y, and z after the following code executes? int x = 3; int y = x; int z = x * y; x++; x = 3, y = 3, z = 9 x = 4, y = 3, z = 9 x = 0, y = 3, z = 0 x = 4, y = 4, z = 9

x=4, y=3, z=9


Ensembles d'études connexes

Organization of political parties

View Set

CH 3: Medication Administration and the Nursing Process of Drug Therapy

View Set

Environmental Studies Chapters 13-19

View Set

CFP-101 Unit 8: Professional Conduct and Fiduciary Responsibility

View Set

Weathering, Erosion, and Geologic Time

View Set

A&P Lab: Set 5: Body Orientation and Direction

View Set