Semester 1 Unit 2

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/59

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

60 Terms

1
New cards

Which of the following can replace / missing code / so that the method works as intended?

Math.sqrt(x ^ 2, y ^ 2, a - b)

Math.sqrt((x + y) ^ 2) / Math.abs(a, b)

Math.sqrt((x + y) ^ 2 / Math.abs(a - b))

Math.sqrt(Math.pow(x + y, 2) / Math.abs(a - b))

Math.sqrt(Math.pow(x + y, 2) / Math.abs(a, b))

Math.sqrt(Math.pow(x + y, 2) / Math.abs(a - b))

2
New cards

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?

Song is an instance of the happyBirthday object.

Song is an instance of three String objects.

happyBirthday is an instance of three String objects.

artist, title, and album are instances of the Song class.

happyBirthday is an instance of the Song class.

happyBirthday is an instance of the Song class.

3
New cards

Consider the following method, which is intended to return true if at least one of the three strings s1, s2, or s3 contains the substring "art". Otherwise, the method should return false.

public static Boolean containsArt(String s1, String s2, String s3)

{

String all = s1 + s2 + s3;

return (all.indexOf("art") != -1);

}

Which of the following method calls demonstrates that the method does not work as intended?

containsArt ("matriculate", "carat", "arbitrary")

containsArt ("harm", "chortle", "crowbar")

containsArt ("rattrap", "similar", "today")

containsArt ("start", "article", "Bart")

containsArt ("darkroom", "cartoon", "articulate")

containsArt ("rattrap", "similar", "today")

4
New cards

A pair of number cubes is used in a game of chance. Each number cube has six sides, numbered from 1 to 6 , inclusive, and there is an equal probability for each of the numbers to appear on the top side (indicating the cube's value) when the number cube is rolled. The following incomplete statement appears in a program that computes the sum of the values produced by rolling two number cubes.

int sum = / missing code / ;

Which of the following replacements for /* missing code */ would best simulate the value produced as a result of rolling two number cubes?

  • (int) (Math.random() * 13)

  • 2 + (int) (Math.random() 6) + (int) (Math.random() 6)

  • (int) (Math.random() 6) + (int) (Math.random() 6)

  • 2 (int) (Math.random() 7)

  • 2 (int) (Math.random() 6)

2 + (int) (Math.random() 6) + (int) (Math.random() 6)

5
New cards

The following statement assigns an integer value to x.

int x = (int)(Math.random() * 5) + 10;

Consider the statement that would result if the positions of 5 and 10 were swapped in the preceding statement and the resulting integer were assigned to y.

int y = (int)(Math.random() * 10) + 5;

Which of the following are true statements about how the possible values assigned to y differ from the possible values assigned to x?

I. There are more possible values of x than there are possible values of y.

II. There are more possible values of y than there are possible values of x.

III. The value assigned to x can sometimes be the same as the value assigned to y.

II and III

III only

I only

II only

I and III

II and III

6
New cards

Consider the following class definition.

public class ExamScore

{

private String studentId;

private double score;

public ExamScore(String sid, double s)

{

studentId = sid;

score = s:

}

public double getScore()

{

return score;

}

public void bonus(int b)

{

score += score * b/100.0;

}

}

Assume that the following code segment appears in a class other than ExamScore.

ExamScore es = new ExamScore("12345", 80.0);

es.bonus(5);

System.out.println(es.getScore());

What is printed as a result of executing the code segment?

  • 4.0

  • 80.0

  • 5.0

  • 84.0

  • 85.0

84.0

7
New cards

Consider the following partial class declaration.

public class SomeClass

{

private int myA;

private int myB;

private int myC;

// constructor(s) not shown

public int getA()

{ return myA; }

public void setB(int value)

{ myB = value; }

}

The following declaration appears in another class.

SomeClass obj = new SomeClass ( );

Which of the following code segments will compile without error?

  • int x = getA(obj);

  • int x;

    obj.getA (x);

  • int x = obj.getA ( );

  • int x = obj.myA;

  • int x = SomeClass.getA ( );

int x = obj.getA ( );

8
New cards

Consider the following class declaration.

public class Person

{

private String myName;

private int myYearOfBirth;

public Person (String name, int yearOfBirth)

{

myName = name;

myYearOfBirth = yearOfBirth;

}

public String getName ()

{ return myName;}

public void setName(String name)

{ myName = name; }

// There may be instance variables, constructors, and methods that are not shown.

}

Assume that the following declaration has been made.

Person student = new Person ("Thomas", 1995);

Which of the following statements is the most appropriate for changing the name of student from "Thomas" to "Tom"?

  • student = new Person ("Tom", 1995);

  • student.setName ("Tom");

  • student.getName ("Tom");

  • student.myName = "Tom";

  • Person.setName ("Tom");

student.setName ("Tom");

9
New cards

Consider the following code segment. Assume that a is greater than zero.

int a = /* value not shown */;

int b = a + (int) (Math.random() * a);

Which of the following best describes the value assigned to b when the code segment is executed?

  • A random integer between a and 2 * a, inclusive

  • 2 * a

  • A random integer between 0 and a - 1, inclusive

  • A random integer between a and 2 * a - 1, inclusive

  • a

A random integer between a and 2 * a - 1, inclusive

10
New cards

Consider the following class.

public class SomeMethods

{

public void one(int first)

{/*implementation not shown*/}

public void one(int first, int second)

{/*implementation not shown*/}

public void one(int first, String second)

{/* implementation not shown*/}

}

Which of the following methods can be added to the SomeMethods class without causing a compile-time error?

I.

public void one(int value)

{/*implementation not shown*/}

II.

public void one (String first, int second)

{/*implementation not shown*/}

III.

public void one (int first, int second, int third)

{/*implementation not shown*/}

I and II only

II and III only

I and III only

I only

I, II, and III

II and III only

11
New cards

Consider the following class.

public class WindTurbine

{

private double efficiencyRating;

public WindTurbine()

{

efficiencyRating = 0.0;

}

public WindTurbine(double e)

{

efficiencyRating = e;

}

}

Which of the following code segments, when placed in a method in a class other than WindTurbine, will construct a WindTurbine object wt with an efficiencyRating of 0.25?

  • new WindTurbine wt = 0.25;

  • WindTurbine wt = 0.25;

  • WindTurbine wt = new WindTurbine();

    wt = 0.25;

  • WindTurbine wt = new WindTurbine (0.25);

WindTurbine wt = new WindTurbine (0.25);

12
New cards

Consider the following method.

public void doSomething()

{

System.out.println("Something has been done");

}

Each of the following statements appears in a method in the same class as doSomething. Which of the following statements are valid uses of the method doSomething ?

I.

doSomething ();

II.

String output = doSomething () ;

III.

System.out.println(doSomething());

I only

I, II, and III

I and III only

II only

I and II only

I only

13
New cards

Assume that the following variable declarations have been made.

double d = Math.random();

double r;

Which of the following assigns a value to r from the uniform distribution over the range 0.5 ≤ r < 5.5 ?

  • r = d * 5.5;

  • r = d * 5.0 + 0.5;

  • r = d + 0.5;

  • r = d + 0.5 * 5.0;

  • r = d * 5.0;

r = d * 5.0 + 0.5;

14
New cards

The Student class has been defined to store and manipulate grades for an individual student. The following methods have been defined for the class.

/* Returns the sum of all of the student's grades */

public double sumOfGrades ()

{/* implementation not shown */ }

/* Returns the total number of grades the student has received */

public int numberOfGrades ()

{/* implementation not shown */ }

/* Returns the lowest grade the student has received */

public double lowestGrade ()

{/* implementation not shown */ }

Which of the following statements, if located in a method in the Student class, will determine the average of all of the student's grades except for the lowest grade and store the result in the double variable newAverage?

  • newAverage = sumOfGrades () / (numberOfGrades () - 1);

  • newAverage = sumOfGrades () / numberOfGrades () - 1;

  • newAverage = sumOfGrades () - lowestGrade () / (numberOfGrades() - 1);

  • newAverage = (sumOfGrades () - lowestGrade ()) / (numberOfGrades () - 1);

  • newAverage = (sumOfGrades () - lowestGrade()) / numberOfgrades() - 1;

newAverage = (sumOfGrades () - lowestGrade ()) / (numberOfGrades () - 1);

15
New cards

Consider the following class declaration.

public class Student

{

private String myName;

private int myAge;

public Student ()

{ /* implementation not shown */ }

public Student (String name, int age)

{ /* implementation not shown */ }

// no other constructors

}

Which of the following declarations will compile without error?

I.

Student a = new Student();

II.

Student b = new Student("Juan", 15);

III.

Student c = new Student("Juan", "15");

I, II, and III

I and II only

I and III only

I only

II only

newAverage = (sumOfGrades () - lowestGrade ()) / (numberOfGrades () - 1);

16
New cards

Consider the following class declaration.

public class Student

{

private String myName;

private int myAge;

public Student ()

{ /* implementation not shown */ }

public Student (String name, int age)

{ /* implementation not shown */ }

// no other constructors

}

Which of the following declarations will compile without error?

I.

Student a = new Student();

II.

Student b = new Student("Juan", 15);

III.

Student c = new Student("Juan", "15");

I, II, and III

I and II only

I and III only

I only

II only

I and II only

17
New cards

Consider the code segment below.

int a = 1988;

int b = 1990;

String claim = " that the world's athletes " +

"competed in Olympic Games in ";

String s = "It is " + true + claim + a +

" but " + false + claim + b + ".";

System.out.println(s);

What, if anything, is printed when the code segment is executed?

  • It is trueclaim1998 but falseclaim1990.

  • It is trueclaima but falseclaimb.

  • It is true that the world's athletes competed in Olympic Games in a but false that the world's athletes competed in Olympic Games in b.

  • Nothing is printed because the code segment does not compile.

  • It is true that the world's athletes competed in Olympic Games in 1988 but false that the world's athletes competed in Olympic Games in 1990.

  • It is true that the world's athletes competed in Olympic Games in 1988 but false that the world's athletes competed in Olympic Games in 1990.

18
New cards

Which of the following statements assigns a random integer between 25 and 60, inclusive, to rn ?

  • int rn = (int) (Math.random() * 25) + 60;

  • int rn = (int) (Math.random() * 26) + 60;

  • int rn = (int) (Math.random() * 36) + 25;

  • int rn = (int) (Math.random() * 60) + 25;

  • int rn = (int) (Math.random() * 25) + 36;

int rn = (int) (Math.random() * 36) + 25;

19
New cards

Consider the following class definition.

public class Bird

{

private String species;

private String color;

private boolean canFly;

public Bird(String str, String col, boolean cf)

{

species = str;

color = col;

canFly = cf:

}

}

Which of the following constructors, if added to the Bird class, will cause a compilation error?

public Bird(String col, String str, boolean cf)

{

species = str;

color = col;

canFly = cf;

}

public Bird(String col, String str)

{

species = str;

color = col;

canFly = false;

}

public Bird()

{

species = "unknown";

color = "unknown";

canFly = false;

}

public Bird(boolean cf)

{

species = "unknown";

color = "unknown";

canFly = cf;

}

public Bird(boolean cf, String str, String col)

{

species = str;

color = col;

canFly = cf;

}

public Bird(String col, String str, boolean cf)

{

species = str;

color = col;

canFly = cf;

}

20
New cards

Consider the following class definition.

public class Student

{

private int studentID;

private int gradeLevel;

private boolean honorRoll;

public Student(int s, int g)

{

studentID = s;

gradeLevel = g;

honorRoll = false;

}

public Student(int s)

{

studentID = s;

gradeLevel = 9;

honorRoll = false;

}

}

Which of the following code segments would successfully create a new Student object?

I.

Student one = new Student(328564, 11);

II.

Student two = new Student(238783);

III.

int id = 392349;

int grade = 11;

Student three = new Student(id, grade);

II only

I, II, and III

I and II only

III only

I only

I, II, and III

21
New cards

Consider the following code segment.

String temp = "comp";

System.out.print(temp.substring(0) + " " +

temp.substring(1) + " " +

temp.substring(2) + " " +

temp.substring(3));

What is printed when the code segment is executed?

  • comp

  • c o m p

  • comp comp comp comp

  • comp com co c

  • comp omp mp p

  • comp omp mp p

22
New cards

Assuming the following declarations:

int x = 5, y = 2, z = 10, temp = 0;

What is the output of the following statement? If it causes an error, just type error. If nothing is output, just type no output.

if ( x + y > z ) {

x = y + z;

}

else {

x = y - z;

}

System.out.println( x + " " + y + " " + z );

-8 2 10

23
New cards

Assuming the following declarations:

int x = 5, y = 2, z = 10, temp = 0;

What is the output of the following statement? If it causes an error, just type error. If nothing is output, just type no output.

if ( x >= 6 ) { System.out.println( x + y); }

System.out.println(x + y);

7

24
New cards

Consider the following code segment.

int num1 = 13;

int num2 = 2;

if(num1 < num2)

{

System.out.print((num1 + num2) % num2);

}

else

{

System.out.print((num1 - num2) % num2);

}

What is printed as a result of executing the code segment?

1

2

11

-1

1

25
New cards

Consider the following code segment.

int num1 = 9;

int num2 = 5;

if(num1 > num2)

{

System.out.print((num1 + num2) % num2);

}

else

{

System.out.print((num1 - num2) % num2);

}

What is printed as a result of executing the code segment?

2

0

14

4

9

4

26
New cards

Consider the following code segment.

String greet1 = "Good morning!";

String greet2 = "Good afternoon!";

String greet3 = "Good evening";

int timeOfDay;

if(timeOfDay >= 1700) {

System.out.println(greet3);

}

else if(timeOfDay >= 1200) {

System.out.println(greet2);

}

else {

System.out.println(greet1);

}

What is printed as a result of executing the code segment if timeOfDay equals 1230?

Good morning!

Good afternoon!

Good evening!

Good evening

Good afternoon!

27
New cards

Consider the following code segment.

String greet1 = "Good morning!";

String greet2 = "Good afternoon!";

String greet3 = "Good evening";

int timeOfDay;

if(timeOfDay >= 1700) {

System.out.println(greet3);

}

else if(timeOfDay >= 1200) {

System.out.println(greet2);

}

else {

System.out.println(greet1);

}

What is printed as a result of executing the code segment if timeOfDay equals 1900?

Good morning!

Good evening!

Good evening

Good afternoon!

Good evening

28
New cards

Consider the following code segment.

int value = initValue;

if(value > 10){

if(value > 15){

value = 0;

}

else{

value = 1;

}

System.out.println(“value = “ + value);

}

Under which of the conditions below will this code segment print value = 1?

initValue = 8;

Never. Code will not compile.

Never. Value = 0 will always be printed.

initValue = 12;

initValue = 20;

initValue = 12;

29
New cards

Consider the following code segment.

int num1 = 10;

int num2 = 45;

int num3 = 7;

if(num1 > num2)

{

System.out.print((num1 + num2) % num3);

}

else

{

System.out.print((num1 - num3) % num2);

}

What is printed as a result of executing the code segment?

33

3

-3

13

3

30
New cards

Consider the following code segment.

int value = initValue;

if(value > 7) {

if(value < 22) {

value = 0;

}

}

else {

value = 1;

}

System.out.println(“value = “ + value);

Under which of the conditions below will this code segment print value = 1?

initValue = 2

initValue = 21

initValue = 12

initValue = 32

initValue = 2

31
New cards

Consider the following code segment.

int num1 = 9;

int num2 = 5;

if(num1 > num2)

{

System.out.print((num1 + num2) % num2);

}

else

{

System.out.print((num1 - num2) % num2);

}

What is printed as a result of executing the code segment?

2

0

4

14

9

4

32
New cards

Consider the following code segment.

int num1 = 10;

int num2 = 45;

if(num1 > num2)

{

System.out.print((num1 + num2) % num2);

}

else

{

System.out.print((num1 - num2) % num2);

}

What is printed as a result of executing the code segment?

35

-35

-3

3

-35

33
New cards

Consider the following code segment.

int value = initValue;

if(value > 10) {

if(value < 15) {

value = 0;

}

}

else {

value = 1;

}

System.out.println(“value = “ + value);

Under which of the conditions below will this code segment print value = 0?

initValue = 8;

Never. Code will not compile.

initValue = 12;

initValue = 20;

Never. Value = 0 will always be printed.

initValue = 12;

34
New cards

&& is evaluated before !.

True

False

false

35
New cards

Given the following statement:

int attendance = 8;

The expression !Attendance = 8; would evaluate to true.

false

36
New cards

Consider the following code segment.

int x = 2;

int y = 1;

int z = 0;

if((y + 1) == x) {

y++;

z += y;

}

if(z == x) {

x--;

y = 5;

}

What are the values of x, y, and z after this code segment has been executed?

x = 2, y = 1, and z = 0

x = 1, y = 5, and z = 2

x = 1, y = 5, and z = 1

x = 2, y = 2, and z = 1

x = 1, y = 5, and z = 2

37
New cards

Assume that the int variables x, y, z, 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(x > y && y > z)

{

low = z;

}

if(x > y && z > y)

{

low = y;

}

else {

low = x;

}

System.out.println(x + y + z - low);

For which of the following values of x, y, and z does the code segment NOT print the correct value?

x = 2, y = 2, z = 2

x = 1, y = 1, z = 1

x = 1, y = 2, z = 1

x = 3, y = 2, z = 1

x = 3, y = 2, z = 1

38
New cards

If x has the value 6, Java will only evaluate the left side of the compound expression in the if condition:

if (x > 1 && x < 50)

True

False

False

39
New cards

Determine if the following evaluates to true or false based on the value of these variables:

int a = 0;
int b = 1;

(a != b);

 

true

false

true

40
New cards

A Boolean expression evaluates to either true or false.

True

False

True

41
New cards

Determine if the following evaluates to true or false based on the value of these variables:

int a = 0;

int b = 1;

!(a 0) && !(b 2);

true

false

false

42
New cards

The following code segment prints one or more characters based on the values of boolean variables b1 and b2. Assume that b1 and b2 have been properly declared and initialized.

if(!b1 || b2)

{

System.out.print("A");

}

else {

System.out.print("B");

}

if(!(b1 || b2))

{

System.out.print("C");

}

else {

System.out.print("D");

}

if(b1 && !b1){

System.out.print("E");

}

If b1 and b2 are both initialized to true, what is printed when the code segment has finished executing?

ABCD

BDE

BD

AD

ABD

AD

43
New cards

Determine if the following evaluates to true or false based on the value of these variables:

 

int a = 0;
int b = 1;

(a < b || b < a);

false

true

true

44
New cards

Determine if the following evaluates to true or false based on the value of these variables:

int a = 0;

int b = 1;

(a < -10 || b == 2);

true

false

false

45
New cards

Given the following statement:

int attendance = 8;

The expression !Attendance = 8; would evaluate to true.

False

46
New cards

&& is evaluated before !.

True 

False

False

47
New cards

Consider the following code segment.

int start = 3;

int end = 6;

boolean keepGoing = true;

if (start < end && keepGoing)

{

if (end > 0)

{

start += 3;

end++;

}

else

{

end += 4;

}

}

if (start < end)

{

if (end == 0)

{

end += 2;

start++;

}

else

{

end += 3;

}

}

What is the value of end after the code segment is executed?

6

5

10

9

10

48
New cards

What is the output of the following code fragment? Assume all the variables are declared.

if(( 5.2 > 3.3 && 6.2 < 9.9) && (6.2 < 3.5 || 4.2 == 3.1 || 3.1 != 3.14)) {

System.out.print("TRUE");

}

else {

System.out.print("FALSE");

}

TRUE

49
New cards

Consider the following statement.

boolean x = (9 < 8) != (5 == 5);

What is the value of x after the statement has been executed?

false

3

5

true

true

50
New cards

Consider the following Boolean expressions.

I. A && B

II. !A && B

Which of the following best describes the relationship between values produced by expression I and expression II?

Expression I and expression II evaluate to the same value only when A and B differ.

Expression I and expression II evaluate to the same value only when A and B are the same.

Expression I and expression II evaluate to different values for all values of A and B.

Expression I and expression II evaluate to the same value whenever B is false.

Expression I and expression II evaluate to the same value whenever B is false.

51
New cards

Consider the following code segment.

if (false && true || false)

{

if (false || true && false)

{

System.out.print("First");

}

else

{

System.out.print("Second");

}

}

if (true || true && false)

{

System.out.print("Third");

}

What is printed as a result of executing the code segment?

FirstThird

Third

Second

First

Third

52
New cards

Consider the following code segment.

if (a < b || c != d)

{

System.out.println("dog");

}

else

{

System.out.println("cat");

}

Assume that the int variables a, b, c, and d have been properly declared and initialized. Which of the following code segments produces the same output as the given code segment for all values of a, b, c, and d ?

if (a >= b || c == d)

{

System.out.println("cat");

}

else

{

System.out.println("dog");

}

if (a < b && c != d)

{

System.out.println("dog");

}

else

{

System.out.println("cat");

}

if (a > b && c == d)

{

System.out.println("cat");

}

else

{

System.out.println("dog");

}

if (a >= b && c == d)

{

System.out.println("cat");

}

else

{

System.out.println("dog");

}

if (a >= b && c == d)

{

System.out.println("cat");

}

else

{

System.out.println("dog");

}

53
New cards

Consider the following statement.

boolean x = (5 < 8) (5 8);

What is the value of x after the statement has been executed?

5

true

false

3

false

54
New cards

What is the output of the following code fragment? Assume all the variables are declared.

if(( 5.2 > 3.3 && 6.2 < 9.9) && (6.2 < 3.5 || 4.2 == 3.1 || 3.1 != 3.14)) {

System.out.print("TRUE");

}

else {

System.out.print("FALSE");

}

TRUE

55
New cards

Java uses short-circuit evaluation for determining the result of a compound Boolean expression that includes && or ||.

True

False

True

56
New cards

Consider the following statement.

boolean x = (9 < 8) != (5 == 5);

What is the value of x after the statement has been executed?

true

3

5

false

true

57
New cards

Consider the following Boolean expressions.

I. A && B

II. !A && B

Which of the following best describes the relationship between values produced by expression I and expression II?

Expression I and expression II evaluate to the same value only when A and B differ.

Expression I and expression II evaluate to the same value only when A and B are the same.

Expression I and expression II evaluate to the same value whenever B is false.

Expression I and expression II evaluate to different values for all values of A and B.

Expression I and expression II evaluate to the same value whenever B is false.

58
New cards

Consider the following code segment.

int x = 7;

int y = 4;

boolean a = false;

boolean b = false;

if (x > y)

{

if (x % y >= 3)

{

a = true;

x -= y;

}

else

{

x += y;

}

}

if (x < y)

{

if (y % x >= 3)

{

b = true;

x -= y;

}

else

{

x += y;

}

}

What are the values of a, b, and x after the code segment has been executed?

a = true, b = false, x = 3

a = true, b = true, x = -1

a = false, b = true, x = 3

a = true, b = false, x = 7

a = true, b = false, x = 7

59
New cards

Consider the following statement, which assigns a value to b1.

boolean b1 = true && (17 % 3 == 1);

Which of the following assigns the same value to b2 as the value stored in b1 ?

  • boolean b2 = false && (17 % 3 == 2);

  • boolean b2 = true || (17 % 3 == 1);

  • boolean b2 = (true && false) || true;

  • boolean b2 = false || (17 % 3 == 2);

boolean b2 = false && (17 % 3 == 2);

60
New cards