CodeHS AP CSP Midterm JavaScript Practice Quiz (Units 3–7)
CodeHS AP CSP Midterm JavaScript Practice Quiz (Units 3–7)
50 Multiple-Choice Questions — Variables, Conditionals, Loops, Functions, Lists & Algorithms
println("7" + 3);
var x = 10;
x = x - 4;
println(x);
typeof "hello"?println(10 % 4);
if (5 > 2) {
println("Yes");
} else {
println("No");
}
var x = 10;
if (x < 5) {
println("Small");
} else if (x < 10) {
println("Medium");
} else {
println("Large");
}
var temp = 65;
if (temp > 80) {
println("Hot");
} else if (temp >= 60) {
println("Nice");
} else {
println("Cold");
}
for (var i = 0; i < 4; i++) {
println(i);
}
for (var i = 2; i <= 6; i++) {
println(i);
}
var count = 0;
while (count < 3) {
count++;
}
println(count);
for (var i = 1; i < 10; i += 3) {
println(i);
}
var x = 5;
while (x > 2) {
println(x);
x--;
}
for (var i = 0; i < 5; i++) {
println("Hello");
}
function add(a, b) {
return a + b;
}
function f(x) {
return x * 2;
}
println(f(4));
function hello() {
println("Hi");
}
var y = hello();
println(y);
function mystery(a, b) {
return a - b;
}
println(mystery(10, 6));
function square(n) {
return n * n;
}
println(square(3));
var nums = [2, 4, 6];
println(nums[1]);
var a = [5, 10, 15, 20];
var animals = ["cat", "dog", "bird"];
println(animals[animals.length - 1]);
var values = [1, 2, 3];
values.push(4);
println(values);
var nums = [3, 6, 9];
nums[1] = 100;
println(nums);
var arr = [1, 2, 3, 4];
for (var i = 0; i < arr.length; i++) {
println(arr[i]);
}
var arr = [2, 4, 6];
var sum = 0;
for (var i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
println(sum);
var nums = [3, 7, 2, 9];
var max = nums[0];
for (var i = 1; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
println(max);
var nums = [10, 20, 30, 40];
for (var i = nums.length - 1; i >= 0; i--) {
println(nums[i]);
}
var nums = [1, 2, 3, 4, 5];
var countEven = 0;
for (var i = 0; i < nums.length; i++) {
if (nums[i] % 2 === 0) {
countEven++;
}
}
println(countEven);
var nums = [5, 5, 5];
var product = 1;
for (var i = 0; i < nums.length; i++) {
product = product * nums[i];
}
println(product);
var nums = [2, 4, 6, 8];
var count = 0;
for (var i = 0; i < nums.length; i++) {
if (nums[i] > 5) {
count++;
}
}
println(count);
var nums = [3, 1, 4];
nums[0] = nums[1];
println(nums);
var nums = [1, 2, 3, 4];
for (var i = 0; i < nums.length; i++) {
nums[i] = nums[i] * 2;
}
println(nums);
var nums = [10, 20, 30];
var total = 0;
for (var i = 0; i < nums.length; i++) {
total += nums[i];
}
var avg = total / nums.length;
println(avg);
var nums = [4, 9, 1];
var index = 0;
for (var i = 1; i < nums.length; i++) {
if (nums[i] < nums[index]) {
index = i;
}
}
println(index);
function containsZero(arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] === 0) {
return true;
}
}
return false;
}
var letters = ["a", "b", "c", "d"];
for (var i = 0; i < letters.length; i += 2) {
println(letters[i]);
}
var nums = [3, 6, 9];
var found = false;
for (var i = 0; i < nums.length; i++) {
if (nums[i] === 6) {
found = true;
}
}
println(found);
var nums = [1, 2, 3, 4];
var newNums = [];
for (var i = 0; i < nums.length; i++) {
newNums.push(nums[i] + 1);
}
println(newNums);
var nums = [5, 1, 7, 1];
var countOnes = 0;
for (var i = 0; i < nums.length; i++) {
if (nums[i] === 1) {
countOnes++;
}
}
println(countOnes);
var nums = [2, 4, 6, 8];
var result = [];
for (var i = nums.length - 1; i >= 0; i--) {
result.push(nums[i]);
}
println(result);
var nums = [1, 2, 3];
var s = "";
for (var i = 0; i < nums.length; i++) {
s = s + nums[i];
}
println(s);
var nums = [2, 2, 2, 2];
var allSame = true;
for (var i = 1; i < nums.length; i++) {
if (nums[i] !== nums[0]) {
allSame = false;
}
}
println(allSame);
var nums = [3, 5, 7, 9];
var foundOdd = false;
for (var i = 0; i < nums.length; i++) {
if (nums[i] % 2 === 1) {
foundOdd = true;
}
}
println(foundOdd);
Answer Key
1. B 2. C 3. B 4. C 5. C
6. A 7. A 8. C 9. C 10. C
11. B 12. B 13. B 14. C 15. D
16. C 17. C 18. B 19. C 20. A
21. C 22. D 23. B 24. B 25. C
26. B 27. B 28. C 29. C 30. A
31. B 32. C 33. C 34. B 35. B
36. D 37. A 38. B 39. A 40. A
41. C 42. C 43. B 44. C 45. B
46. B 47. B 48. C 49. B 50. C
Explanations
1.
"7" is a string, so "7" + 3 does string concatenation, resulting in "73".
2.
5 * 4 is a numeric multiplication and returns 20, a number. The string ones are concatenations.
3.
10 - 4 = 6, so it prints 6.
4.
In JavaScript, text in quotes is a string, and typeof "hello" is "string".
5.
myName2 starts with a letter and has letters/numbers only. Others either start with a digit, contain illegal characters, or use a reserved word.
6.
10 % 4 is the remainder when 10 is divided by 4, which is 2.
7.
5 > 2 is true, so the if branch runs and "Yes" is printed.
8.
=== is the strict equality operator in JavaScript, checking both value and type.
9.
x is 10. The first condition (x < 5) is false; second (x < 10) is also false; so "Large" prints.
10.
Only "cat" === "cat" is true; the others are false.
11.
temp is 65. It's not greater than 80, but it is ≥ 60, so "Nice" prints.
12.
In an if/else-if/else chain, at most one branch runs because the chain stops as soon as one condition is true.
13.
i goes 0,1,2,3. The loop stops when i becomes 4. So it prints 0, 1, 2, 3.
14.
i takes 2,3,4,5,6 → 5 times total.
15.
count starts at 0, increments until it reaches 3, then loop ends. It prints 3.
16.
while (true) is always true and never changes, so it will loop forever (unless broken externally).
17.
i starts at 1, then 4, then 7. Next would be 10 which fails condition. So prints 1 4 7.
18.
x = 5, prints 5,4,3. When x becomes 2, condition (x > 2) is false and the loop stops.
19.
The loop runs i = 0,1,2,3,4 → 5 times, printing "Hello" each time.
20.
The function returns a + b, the sum of its parameters.
21.
Passing 4 gives 4 * 2 = 8.
22.
hello() prints "Hi" and returns undefined, so output is "Hi" then "undefined".
23.
JavaScript functions are defined with function f(x) { ... }.
24.
10 - 6 = 4, so it prints 4.
25.
3 * 3 = 9, so square(3) returns 9.
26.
Array indexing is zero-based. nums[1] is 4.
27.
The array [5,10,15,20] has 4 elements.
28.
animals.length is 3, so index 2 is "bird", the last element.
29.
push(4) adds 4 to the end, giving [1,2,3,4].
30.
Index 1 changes from 6 to 100, so [3,100,9].
31.
The for loop goes through each index and prints arr[i], printing every element.
32.
2 + 4 + 6 = 12, so it prints 12.
33.
This is the standard "find max" algorithm. It prints 9, the highest value in [3,7,2,9].
34.
Loop starts at last index and goes backwards, so prints 40,30,20,10.
35.
It counts how many array elements are even (2 and 4), so it prints 2.
36.
product = 1 * 5 * 5 * 5 = 125.
37.
Values greater than 5 are 6 and 8 → count = 2, the number of values > 5.
38.
nums[0] = nums[1] changes first element to 1 → [1,1,4].
39.
Each element is multiplied by 2, so [1,2,3,4] becomes [2,4,6,8].
40.
Sum is 10+20+30 = 60. Dividing by 3 gives average 20.
41.
The smallest element is 1 at index 2, so index printed is 2.
42.
It returns true as soon as it finds any element equal to 0.
43.
i = 0,2. So it prints letters[0] "a" and letters[2] "c".
44.
If 6 is present in nums, found becomes true. For [3,6,9], it prints true.
45.
Each element is increased by 1, so [1,2,3,4] → [2,3,4,5].
46.
It counts how many elements equal 1. Here there are two 1's, so it prints 2.
47.
It pushes elements in reverse order, so [2,4,6,8] becomes [8,6,4,2].
48.
s starts as "", then becomes "1", then "12", then "123". So it prints "123".
49.
It compares each element to nums[0]. If any difference is found, allSame is false. For [2,2,2,2], all are the same, so it prints true.
50.
All numbers in [3,5,7,9] are odd, so foundOdd becomes true and it prints true.