Practice · Code Reading
What does this print?
Read the code, pick what it prints, then see why. This is the skill behind the new code comprehension rounds: reading code you didn't write, and knowing what it really does. Every program here has been run, and the answers are what it actually prints.
35 JavaScript drills15 Python drills →
1. Use it before you write it
Lesson 3sayHi();console.log(score);var score = 50;function sayHi() { console.log("Hi there!");}2. Same name, which city?
Lesson 4var city = "Delhi";function showHome() { var city = "Pune";}showHome();console.log(city);3. undefined vs not defined
Lesson 6console.log(marks);var marks = 95;console.log(grade);4. Whose variable is it?
Lesson 7function kitchen() { console.log(owner);}var owner = "Asha";kitchen();5. var vs let, before the line
Lesson 8console.log(count);console.log(price);var count = 3;let price = 99;6. What leaks out of the braces?
Lesson 9{ var coins = 10; let lives = 3;}console.log(coins);console.log(lives);7. It remembers
Lesson 10function makeGreeter() { var name = "Riya"; return function () { console.log("Hi " + name); };}var greet = makeGreeter();greet();8. The classic interview loop
Lesson 11for (var i = 1; i <= 3; i++) { setTimeout(function () { console.log(i); }, i * 10);}9. The same loop, with let
Lesson 11for (let i = 1; i <= 3; i++) { setTimeout(function () { console.log(i); }, i * 10);}10. Statement vs expression
Lesson 13greet();wave();function greet() { console.log("Hello!");}var wave = function () { console.log("Wave!");};11. Who prints last?
Lesson 15console.log("start");setTimeout(function cb() { console.log("timer");}, 50);console.log("end");12. Zero milliseconds, still last
Lesson 17console.log("Start");setTimeout(() => console.log("Callback"), 0);console.log("End");13. map, filter, reduce
Lesson 19const prices = [120, 80, 250];console.log(prices.map((p) => p + 20));console.log(prices.filter((p) => p < 100));console.log(prices.reduce((sum, p) => sum + p, 0));14. What's inside the promise?
Lesson 21const booking = new Promise((resolve) => { setTimeout(() => resolve("BK-101"), 100);});console.log(booking);15. Resolved already, printed later
Lesson 22new Promise((resolve) => { resolve("BK-101");}).then((id) => console.log(id));console.log("booking...");16. What does async return?
Lesson 23async function getCourse() { return "Inside JavaScript";}console.log(getCourse());17. One of them fails
Lesson 25const details = new Promise((resolve) => setTimeout(() => resolve("details"), 200));const showtimes = new Promise((_, reject) => setTimeout(() => reject(new Error("Showtimes failed")), 100));Promise.all([details, showtimes]) .then((r) => console.log(r)) .catch((e) => console.log(e.message));18. Who wins the race?
Lesson 26const details = new Promise((resolve) => setTimeout(() => resolve("details"), 200));const showtimes = new Promise((resolve) => setTimeout(() => resolve("showtimes"), 100));Promise.race([details, showtimes]).then((v) => console.log(v));19. Who is this?
Lesson 27"use strict";function show() { console.log(this);}show();20. Where did this go?
Lesson 28"use strict";const movie = { title: "Inception", show() { console.log(this.title); },};movie.show();const show = movie.show;show();21. The local one, too early
Lesson 3var x = 1;function show() { console.log(x); var x = 2;}show();22. Two counters
Lesson 10function makeCounter() { let count = 0; return () => ++count;}const a = makeCounter();const b = makeCounter();a();console.log(a(), b());23. var in a loop, fixed
Lesson 11for (var i = 0; i < 3; i++) { (function (j) { setTimeout(() => console.log(j), 0); })(i);}24. A function that returns a function
Lesson 13const add = (a) => (b) => a + b;const addTen = add(10);console.log(addTen(5), add(2)(3));25. map with parseInt
Lesson 19const nums = ["1", "2", "3"].map(parseInt);console.log(nums);26. Reduce an empty array
Lesson 19const total = [].reduce((sum, x) => sum + x);console.log(total);27. After the catch
Lesson 21Promise.resolve(1) .then((x) => x + 1) .then(() => { throw new Error("oops"); }) .catch(() => 5) .then((x) => console.log(x));28. An async function that throws
Lesson 23async function load() { throw new Error("boom");}load().catch((e) => console.log("caught", e.message));console.log("after");29. Where await pauses
Lesson 23async function run() { console.log("A"); await null; console.log("B");}run();console.log("C");30. Wait for everyone
Lesson 25Promise.allSettled([ Promise.resolve(1), Promise.reject(new Error("no")),]).then((r) => console.log(r.map((x) => x.status).join(",")));31. The first success
Lesson 26Promise.any([ Promise.reject(new Error("a")), new Promise((ok) => setTimeout(() => ok("slow"), 50)), Promise.resolve("fast"),]).then((v) => console.log(v));32. bind, then bind again
Lesson 28function getX() { return this.x;}const once = getX.bind({ x: 1 });const twice = once.bind({ x: 2 });console.log(twice());33. An arrow function as a method
Lesson 28const user = { city: "Pune", where: () => console.log(this.city), whereNow() { console.log(this.city); },};user.where();user.whereNow();34. Loose equality
console.log([] == false, null == 0, null >= 0);35. Two names, one object
const a = { score: 1 };const b = a;b.score = 2;const c = { ...a };c.score = 3;console.log(a.score, b.score, c.score);