CPE Final Study Assignment - Spring 2025 Review

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

1/46

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.

47 Terms

1
New cards

Which of the following uses proper syntax to declare and initialize a memory location to store the average miles per hour to the nearest tenth?

- mph = 35.4;

- double mph;

- int mph = 14.1;

- double mph = 25.3;

double mph = 25.3;

2
New cards

Evaluate these two expressions: (int)(9 / 2 3) and (double)(9 / 2 3)

- 13 and 13.0

- 12 and 13.5

- 12 and 12.0

- 13 and 13.5

12 and 12.0

3
New cards

What will the following code print?

int num = 7, amt = 3;

num *= 2;

num++;

amt = num;

amt += num / amt;

System.out.println("num is " + num + "; amt is " + amt);

- num is 15; amt is 2

- num is 3; amt is 2

- num is 3; amt is 4

- num is 15; amt is 16

num is 15; amt is 16

4
New cards

What is the output for the following code?

System.out.println("LM");

System.out.print(245);

System.out.println("CB");

System.out.print(789);

- LM245CB789

- LM

245CB

789

- LM

245CB789

- LM245

CB789

LM

245CB

789

5
New cards

Which value can be stored in a boolean variable?

- False

- false

- "false"

- "False"

- false

6
New cards

After execution of the following line of code, what will be the value of num3 ?

double num3 = 15 / 3;

- 5

-0

-5.0

-error: incompatible types

5.0

7
New cards

Which line of code will result in an error message if:

num1 is an int

num2 is a double

num3 is an int

num4 is a double

- num3 = num1 + num2;

- num2 = num3 / num4;

- num2 = num3 + num1;

- num1 = num1 / num3;

- num3 = num1 + num2;

8
New cards

What will be assigned to num3 given the following declarations?

int tot = 94;

int amt = 5;

int num3 = tot % amt;

- 18.8

- 4

- 18

- 0.8

4

9
New cards

Which line of code below is used to obtain the user's age given the following two lines of code and assuming the variable, age, has already been declared?

Scanner scan = new Scanner(System.in);System.out.println("Enter your age.");

- scan.nextInt();

- age = scan.nextInt();

- age = scan.nextLine();

- age = scan.next();

age = scan.nextInt();

10
New cards

Which line of code has an error that will be caught by the compiler?

- String str = Sea World;

- System.out.print("How are You?");

- public static void main(String[] args)

- double cost = 4.23;

- public class TaxCalculation

String str = Sea World;

11
New cards

Trace the code snippet below and indicate which line produces the first error from the compiler.

1. String name = "Jane";

2. int numGames = 12;

3. double numPoints;

4. double ptsPerGame = numGames / numPoints;

5. System.out.println(name + " scored " + ptsPerGame + " points per game.");

line 4

line 2

line 3

line 1

line 5

line 4

12
New cards

Which of the following is not a primitive data type?

- double

- String

- int

- boolean

String

13
New cards

Given:

String school = "Xavier Prep";

What is the return value for each method call?

A. _____ school.substring(8)

B. ______ school.length()

C. ______ school.indexOf("e")

D. ______ school.substring(2, 5)

A. "rep"

B. 11

C. 4

D. "vie"

14
New cards

tripleNum(amt); is a(n) ____ that passes one ____ to the tripleNum() method.

- method call; variable

- method call; argument or parameter

- argument or parameter; variable call

- variable call; argument or parameter

method call; argument or parameter

15
New cards

Given the method header:

public void costOrder(int a)

Which of the following statements about the costOrder() method is accurate?

- The data type of its return value is a double.

- The data type of its return value is an int.

- It has no return value.

- Its method call will pass a double.

It has no return value.

16
New cards

What is the output?

public void yourMethod(int a)

{

int amt = a + 2; System.out.println(amt + " apples");

}

public void myMethod(int x)

{

int num4 = x * 2;

System.out.println(num4 + " birds");

}

public void main(String[] args)

{

int num1 = 8, num2 = 3, num3 = 5; myMethod(num2);

System.out.println(num1 + " cats"); yourMethod(num3);

}

8 cats

7 apples

6 birds

8 cats

6 birds

7 apples

6 birds

7 apples

8 cats

6 birds

8 cats

7 apples

6 birds

8 cats

7 apples

17
New cards

What is the output?

public int calculate(int a, int b)

{

int num1 = a - 4;

int num2 = b - 8;

int num3 = num1 * num2;

return num3;

}

public void main(String[] args)

{

int amt = 7, num = 10;

int x = calculate(amt, num); System.out.println(amt + " fries"); System.out.println(x + " shakes");

}

6 fries

7 shakes

7 fries

10 shakes

10 fries

7 shakes

7 fries

6 shakes

7 fries

6 shakes

18
New cards

Given:

String schMascot = "Gators";

String schCity = "Phoenix";

What is the return value of the following method call?

schCity.substring(schMascot.indexOf("t"), schMascot.length())

oenix

eni

enix

oen

oeni

oeni

19
New cards

Given:

int num = 16;int amt = 3;

What is the return value for each method call?

A. ______ Math.pow(3, amt)

B. ______ Math.sqrt(num)

C. ______ Math.abs(amt - num)

A. 27.0

B. 4.0

C. 13

20
New cards

Which of the following expressions returns a random integer from 1 to 25?

- (int)(Math.random() * 25 + 1)

- Math.random() * 1 + 25

- Math.random(25) * 1)

- (int)(Math.random() * 1 + 25)

- (int)(Math.random(25) * 1)

- Math.random() * 25 + 1

(int)(Math.random() * 25 + 1)

21
New cards

Match the following method call with the appropriate header of the called method.

someMethod(7, 10.4, "Billy");

1. public void someMethod(double b, int d, String g)

2. public void myMethod(int c, double m, String p)

3. public void someMethod(String a, int b, double c)

4.public void someMethod(int k, double m, String p)

4

22
New cards

Match the following method call with the appropriate header of the called method.

- double var = someMethod(7);

- public void someMethod(int a)

- public double someMethod(int a)

- public int someMethod(int a)

- public double someMethod(double a)

public void someMethod(int a)

23
New cards

What are two ways data can be passed from one method to another?

- variable declaration and print statement

- return statement and variable call

- method call and return statement

- method call and variable call

method call and return statement

24
New cards

What will be the output?

int num2 = 20;

if(num2 >= 20)

System.out.println("apple");

else if(num2 > 15)

System.out.println("pear");

else

System.out.println("peach");

apple

pear

apple

peach

apple

pear

peach

apple

apple

25
New cards

What will be the output?

int num1 = 15;

if(num1 <= 10)

System.out.println("bear");

if(num1 < 20)

System.out.println("cat");

if(num1 < 30)

System.out.println("dog");

cat

cat

dog

bear

cat

dog

bear

cat

cat

dog

26
New cards

What will be the output?

int amt = 52;

int max = 15;

int min = 5;

if(amt > min)

{

System.out.println("train");

}

if(amt < max)

{

System.out.println("boat");

}

else

{

System.out.println("plane");

}

System.out.println("bike");

train

plane

train

plane

bike

train

bike

train

boat

bike

train

train

plane

bike

27
New cards

Tracing code without indentation and curly braces.

What will be the output?

int num1 = 15;

int amt = 52;

int max = 15;

int min = 5;

if(max > num1)

System.out.println("car");

System.out.println("boat");

if(min < amt)

System.out.println("truck");

else

System.out.println("motorcycle");

System.out.println("van");

boat

truck

car

boat

truck

car

boat

truck

van

boat

truck

van

boat

truck

van

28
New cards

Given:

boolean A = true;

boolean B = true;

boolean C = false;

boolean D = false;

What are the values of each of the following expressions?

A. _______ A && C

B. ________ B || D

C. ________ A || D && C

D. _________ !B || !(A && C)

A. false

B. true

C. true

D. true

29
New cards

Which three snippets of code produce an error?

if(x != 10)

else if(x > 10)

if(x <= 10)

if(x >= 30);

if(x >= 30);

30
New cards

How should the following line of code be fixed?

if(favPetName = "Fluffy")

1. if(favPetName.equals(Fluffy))

2. if(Fluffy.equals(favPetName))

3. if(favPetName == "Fluffy")

4. if("Fluffy".equals("favPetName"))

5. if(favPetName.equals("Fluffy"))

5. if(favPetName.equals("Fluffy"))

31
New cards

How many times will the following loop be executed?

int x = 1;

while(x < 9)

{

System.out.println("far out");

x++;

}

10

infinite

9

8

0

8

32
New cards

How many times will the following loop be executed?

int x;

for(x = 0; x <= 15; x++)

{

System.out.println("far out ");

}

15

16

0

14

infinite

16

33
New cards

How many times will Hello be printed?

for(x = 1; x <= 30; x++)

{

for(x = 1; x <= 7; x++)

{

System.out.println("Hello");

}

}

37

30

210

7

210

34
New cards

What is the output?

int x;

int a = 2;

for(x = 1; x < 4; x++)

{

a = a + x;

}

System.out.println("a: " + a + ", x: " + x);

a: 3, x: 1

a: 5, x: 2

a: 8, x: 3

a: 8, x: 4

a: 8, x: 3

a: 3, x: 1

a: 5, x: 2

a: 8, x: 3

a: 8, x: 4

a: 8, x: 4

35
New cards

What are the three parts of a loop?

Loop control variable declared, assigned, & called

Loop control variable declared, tested, & updated

Loop control variable initialized, updated, & called

Loop control variable initialized, tested, & updated

Loop control variable initialized, tested, & updated

36
New cards

Java automatically converts between objects and primitives in the process of autoboxing and unboxing.

What happens when an int is autoboxed?

A int is autoboxed when it is converted to a Double

A int is autoboxed when it is passed to a method.

A int is autoboxed when it is converted to a primitive value.

A int is autoboxed when it is converted to a Integer

A int is autoboxed when it is converted to a Integer

37
New cards

Java automatically converts between objects and primitives in the process of autoboxing and unboxing.

What happens when a Double is unboxed?

A Double is unboxed when it is converted to a primitive value.

A Double is unboxed when it is passed to a method.

A Double is unboxed when it is converted to a Double value.

A Double is unboxed when it is converted to an Integer

A Double is unboxed when it is converted to a primitive value.

38
New cards

The purpose of a wrapper class is to

"Wrap" a primitive value to convert it to an object

Allow methods to be called on a primitive value

Allow primitive to be passed to methods

"Wrap" an object to convert it to a primitive value

"Wrap" a primitive value to convert it to an object

39
New cards

What would this program print?

double sideLength = Math.sqrt(64);

double height = Math.pow(3, 2); double difference = Math.abs(sideLength - height); System.out.println(difference);

-1.0

1.0

8.0

0.0

1.0

40
New cards

Which of these is an example of calling a static method?

Math.abs(x)

point.setX(x)

student.getName()

square(x)

Math.abs(x)

41
New cards

What is the output of the following code snippet?

String forest = "Amazon Rainforest"; System.out.println(forest.indexOf('a')); System.out.println(forest.indexOf('g')); System.out.println(forest.indexOf('n'));

0

-1

5

2

-1

5

This will throw an error because 'g' is not in the string.

1

0

6

2

-1

5

42
New cards

Consider the following code snippet. What would the output be?

String school = "Rydell High School"; System.out.println(school.substring(8)); System.out.println(school);

i

i

igh School

Rydell High School

gh School

Rydell High Schoo

igh School

igh School

gh School

gh School

igh School

Rydell High School

43
New cards

What would be printed by the following code snippet?

String lastName = "Vu"; String otherLastName = "Lopez";

int comparison = lastName.compareTo(otherLastName); System.out.println(comparison);

A positive number because "Vu" comes after "Lopez" in lexicographical order.

A negative number because "Vu" comes before "Lopez" in lexicographical order.

A positive number because "Vu" comes before "Lopez" in lexicographical order.

Zero because both strings start with capital letters

A negative number because "Vu" comes after "Lopez" in lexicographical order.

A positive number because "Vu" comes after "Lopez" in lexicographical order.

44
New cards

Consider this code snippet that uses a class called Rectangle.

int roomHeight = 40;

int roomWidth = roomHeight * 3;

Rectangle room = new Rectangle(roomHeight, roomWidth);

Java

Which of the following is a reference variable?

roomHeight

roomWidth

Rectangle

room

room

45
New cards

Suppose a program is a client of the Player class. Here is a snippet of code contained in the program

Player firstPlayer = new Player("Karel", "Warrior", "Mote Prime", 90);

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 library

Both the program and the documentation contain formal parameters

In the program

In the documentation

In the documentation

46
New cards

Suppose there is a class called Student. One of the methods is given below. It sets the instance variable isHonors to the parameter value.

public void setHonorStatus(boolean status)

{ isHonors = status;

}

Using the Student object called karel, which of the following is the correct way to set karel's honor status to true?

karel.setHonorStatus(isHonors = true);

karel.setHonorStatus(status=true);

karel.setHonorStatus(true);

karel.isHonors = true;

karel.setHonorStatus(true);

47
New cards

Consider the following class:

public class Rectangle

{

private int width;

private int height;

public Rectangle(int rectWidth, int rectHeight)

{

width = rectWidth;

height = rectHeight;

}

public int getArea()

{

return width * height;

}

public int getHeight()

{

return height;

}

public int getWidth()

{

return width;

}

public String toString()

{

return "Rectangle with width: " + width + " and height: " + height;

}

}

If a new variable Rectangle shape = new Rectangle(10, 20); was initialized, what is the correct syntax for retrieving the area of shape?

Rectangle.getHeight() * Rectangle.getWidth();

int area = shape.getArea();

int area = Rectangle.getArea();

shape.getArea(10,20);

shape.getArea();

int area = shape.getArea();