AP Comp Sci Unit 1 Test

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/22

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 5:26 AM on 9/4/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

23 Terms

1
New cards

A code segment will use variables to represent the age of an animal and whether it is considered a house pet. Which of the following variable declarations is most appropriate for the code segment?

int age; 
boolean pet;

B

2
New cards

what’s missing


int x = 5;
int y = 6;
/* missing code */
z = (x + y) / 2;

int z = 0;

C

3
New cards

A program needs to maintain the following information about data captured by a sensor.

  • Whether the sensor is currently active

  • The number of items detected by the sensor

  • The average time per detection, in seconds

     

Which set of variable declarations is most appropriate for this program?

D

boolean active; int items; double avgTime;

4
New cards

In the following code segment,  x  and  y  are properly declared and initialized  double  variables.

 

y = x;
double z = y + x;
System.out.println(z);


Two times the initial value of  x

5
New cards

In the following code segment,  x  and  y  are properly declared and initialized  int  variables.

 

int z = x;
x = y;
y = z;

Which of the following best describes the behavior of the code segment?

D

It swaps the values of  x  and  y.

6
New cards

The following code segment is intended to interchange the values of the  int  variables  x  and  y.  Assume that  x  and  y  have been properly declared and initialized.

int temp = x; 
/* missing code */

Which of the following can be used to replace  /* missing code */  so that the code segment works as intended?

A

x = y; 
y = temp; 


7
New cards

ng code segment.

 

System.out.print(I do not fear computers. );   // Line 1 
System.out.println(I fear the lack of them.);  // Line 2 
System.out.println(--Isaac Asimov);            // Line 3 

 

The code segment is intended to produce the following output but may not work as intended.

 

I do not fear computers. I fear the lack of them. 
--Isaac Asimov 

Which change, if any, can be made so that the code segment produces the intended output?


C

In lines 1, 2, and 3, the text that appears in parentheses should be enclosed in quotation marks.

8
New cards

Consider the following code segment.

 

int a = 5; 
int b = 8; 
int c = 3; 
System.out.println(a + b / c * 2); 

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



D

9


9
New cards

Consider the following code segment, which is intended to print the digits of the two-digit  int  number  num  in reverse order. For example, if  num  has the value  75,  the code segment should print  57.  Assume that  num  has been properly declared and initialized.

 

/* missing code */ 
System.out.print(onesDigit); 
System.out.print(tensDigit);

Which of the following can be used to replace  /* missing code */  so that the code segment works as intended?


A

int onesDigit = num % 10; 
int tensDigit = num / 10;


10
New cards

In the following code segment,  x  and  y  are properly declared and initialized  int  variables whose values are positive. The code segment is intended to print the average of  x  and  y.  For example, if  x  is  10  and  y  is  5,  the code segment is intended to print  7.5.

 

int sum = x + y; 
double average = (double) (sum / 2);
System.out.println(average); 

Which of the following best describes the error, if any, in the code segment?

C

In the expression  (double) (sum / 2),  the cast to  double  is applied too late, so the average will be less than the expected result for odd values of  sum.

11
New cards

Consider the following code segment.

 

double num = 9 / 4;
System.out.print(num);
System.out.print(" ");
System.out.print((int) num);

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

A

2.0 2


12
New cards

The following code segment is intended to round  val  to the nearest integer and print the result.

 

double val = -0.7; 
int roundedVal = (int) (val + 0.5); 
System.out.println(roundedVal); 

Which of the following best describes the behavior of the code segment?

D

The code segment does not work as intended because the expression  (int) (val + 0.5)  rounds to the nearest integer only when  val  is positive.

13
New cards

Consider the following code segment.

 

int a = 5; 
int b = 4; 
int c = 2; 
a *= 3; 
b += a; 
b /= c; 
System.out.print(b); 

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


B

9


14
New cards

Consider the following code segment.

 

int x = 5; 
x += 6 * 2; 
x -= 3 / 2; 

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

D

16


15
New cards

Consider the following code segment.

 

int a = 4; 
int b = 5; 
a++; 
b++; 
int c = a + b; 
a -= 1; 
System.out.println(a + c); 

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

D

15


16
New cards

A PetOwner object is an attribute of a Cat object

B)

17
New cards

The color and size variables are attributes of the poloShirt object.

A)

18
New cards

Attribute:

• title

Behavior:

• play()

A)

19
New cards

Print your name

System.out.println("John Doe");

20
New cards

Print the numbers 1–10

for (int i = 1; i <= 10; i++) {

System.out.println(i)

21
New cards

Find the larger of 15 and 22.

int a = 15; int b = 22;

if (a > b) {

System.out.println(a);

} else {

System.out.println(b); }

22
New cards

Compute the sum of 8 and 17

int a = 8; int b = 17;

2

int sum = a + b;

System.out.println(sum);

23
New cards

Find the area of a rectangle (length × width)

int length = 8;

int width = 5;

int area = length * width;

System.out.println(area);