CodeHS - AP CSP JavaScript Midterm (Units 3-7)

AP CSP JavaScript Midterm (CodeHS Units 3–7)

This midterm-style practice focuses on loops, conditionals, functions, arrays, strings, and basic algorithms in JavaScript, aligned with a typical CodeHS AP CSP JavaScript course.

Select the best answer for each question, then click Submit Midterm at the bottom.

Question 1.
What does this code print?

for (var i = 0; i < 4; i++) {
    console.log(i + 1);
}
  



Question 2.
What is the value of count after this code runs?

var count = 0;
for (var i = 2; i < 7; i += 2) {
    count += i;
}
  



Question 3.
What does this function call return?

function mystery(x) {
    return x * 2 + 3;
}
mystery(5);
  



Question 4.
What is printed by this loop?

var x = 1;
while (x < 5) {
    x = x + 2;
}
console.log(x);
  



Question 5.
What is the value of nums[2]?

var nums = [10, 20, 30, 40];
  



Question 6.
What does this code print?

var arr = [3, 6, 9];
for (var i = 0; i < arr.length; i++) {
    console.log(arr[i] * 2);
}
  



Question 7.
What is printed?

console.log("hi" + 5);
  



Question 8.
What is printed?

var x = 4;
while (x < 10) {
    x += 3;
}
console.log(x);
  



Question 9.
After this code runs, what is the value of total?

var list = [2, 4, 6, 8];
var total = 0;

for (var i = 1; i < list.length; i++) {
    total += list[i];
}
  



Question 10.
What does check(13) return?

function check(num) {
    if (num % 2 == 0) {
        return "even";
    }
    return "odd";
}
  



Question 11.
What is printed?

var word = "CODE";
console.log(word[1]);
  



Question 12.
Which loop correctly prints every element of list?




Question 13.
What does this print?

var msg = "Hello";
console.log(msg.length);
  



Question 14.
What is the final value of x?

var x = 1;
for (var i = 0; i < 3; i++) {
    x = x + i;
}
  



Question 15.
What is the value of result?

function mult(a, b) {
    return a * b;
}
var result = mult(2, mult(2, 2));
  



Question 16.
Which of the following adds an element to the end of an array in JavaScript?




Question 17.
What does this print?

var str = "abcde";
console.log(str[4]);
  



Question 18.
What does this loop count?

var str = "banana";
var count = 0;

for (var i = 0; i < str.length; i++) {
    if (str[i] == 'a') {
        count++;
    }
}
  



Question 19.
What does this print?

console.log(3 > 5 || 2 < 4);
  



Question 20.
What does this function call return?

function sumFirstTwo(arr) {
    return arr[0] + arr[1];
}
sumFirstTwo([4, 10, 6]);
  



Question 21.
What does this code print?

var total = 1;
for (var i = 1; i <= 3; i++) {
    total = total * i;
}
console.log(total);
  



Question 22.
What is printed?

var x = 10;
for (var i = 0; i < 2; i++) {
    x -= i;
}
console.log(x);
  



Question 23.
What is printed?

var nums = [5, 2, 7];
var biggest = nums[0];

for (var i = 1; i < nums.length; i++) {
    if (nums[i] > biggest) {
        biggest = nums[i];
    }
}
console.log(biggest);
  



Question 24.
What does this code print?

var word = "loop";
for (var i = 0; i < word.length; i += 2) {
    console.log(word[i]);
}
  



Question 25.
What is printed?

var a = 2;
var b = 5;
var c = a + b;
a = c - 1;
console.log(a, b, c);
  



Question 26.
Which change makes this code run correctly and display a greeting?

function greet(name) {
    console.log("Hello " + name);
}
greet();
  



Question 27.
What kind of error is this?

var arr = [1, 2, 3];
console.log(arr[3]);
  



Question 28.
Why does this loop never run?

for (var i = 5; i < 0; i++) {
    console.log(i);
}
  



Question 29.
What does this function return?

function countPos(arr) {
    var count = 0;
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] > 0) {
            count++;
        }
    }
    return count;
}
countPos([-1, 2, 3, 0]);
  



Question 30.
What is printed?

var str = "APCSP";
console.log(str[0] + str[3]);
  



Question 31.
What does this function call return?

function sumEven(arr) {
    var total = 0;
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] % 2 == 0) {
            total += arr[i];
        }
    }
    return total;
}
sumEven([1, 2, 3, 4, 5]);
  



Question 32.
In the array ["a", "b", "c", "d"], what does arr[arr.length - 1] represent?




Question 33.
What is printed?

var nums = [2, 4, 6];
nums.push(8);
console.log(nums.length);
  



Question 34.
What does this function call return?

function isBig(n) {
    return n >= 10;
}
isBig(9);
  



Question 35.
What does this function call return?

function contains(arr, target) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == target) {
            return true;
        }
    }
    return false;
}
contains([3, 5, 7], 5);
  



Question 36.
How many times does this loop execute?

var i = 0;
while (i < 5) {
    i++;
}
  



Question 37.
What does the variable count represent after this code runs?

var count = 0;
for (var i = 0; i < list.length; i++) {
    if (list[i] > 10) {
        count++;
    }
}
  



Question 38.
What is printed?

var s = "hello";
var result = "";

for (var i = 0; i < s.length; i++) {
    if (s[i] != 'l') {
        result += s[i];
    }
}
console.log(result);
  



Question 39.
What is printed?

var scores = [80, 90, 100];
var total = 0;
for (var i = 0; i < scores.length; i++) {
    total += scores[i];
}
console.log(total / scores.length);
  



Question 40.
What is printed?

var age = 16;
if (age < 13) {
    console.log("Child");
} else if (age < 18) {
    console.log("Teen");
} else {
    console.log("Adult");
}
  



Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]