Challenge - 5 Problems
Class Syntax Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of method call in a class
What is the output of this code when calling
obj.greet()?Javascript
class Person { constructor(name) { this.name = name; } greet() { return `Hello, ${this.name}!`; } } const obj = new Person('Alice'); console.log(obj.greet());
Attempts:
2 left
💡 Hint
Remember how the constructor sets the name property and how methods access it with this.
✗ Incorrect
The constructor sets this.name to 'Alice'. The greet method returns a string using this.name, so the output is 'Hello, Alice!'.
❓ Predict Output
intermediate2:00remaining
Output of static method call
What will this code output when calling
MathHelper.square(4)?Javascript
class MathHelper { static square(x) { return x * x; } } console.log(MathHelper.square(4));
Attempts:
2 left
💡 Hint
Static methods are called on the class itself, not on instances.
✗ Incorrect
The static method square multiplies the input by itself. Calling MathHelper.square(4) returns 16.
❓ Predict Output
advanced2:00remaining
Output of inheritance and method override
What is the output of this code?
Javascript
class Animal { speak() { return 'Animal sound'; } } class Dog extends Animal { speak() { return 'Woof!'; } } const pet = new Dog(); console.log(pet.speak());
Attempts:
2 left
💡 Hint
Child classes can replace methods from parent classes.
✗ Incorrect
Dog overrides the speak method from Animal. Calling pet.speak() returns 'Woof!'.
❓ Predict Output
advanced2:00remaining
Output of private field access
What happens when this code runs?
Javascript
class Counter { #count = 0; increment() { this.#count++; } getCount() { return this.#count; } } const c = new Counter(); c.increment(); c.increment(); console.log(c.getCount());
Attempts:
2 left
💡 Hint
Private fields start with # and are only accessible inside the class.
✗ Incorrect
The private field #count starts at 0. Calling increment twice adds 2. getCount returns 2.
❓ Predict Output
expert3:00remaining
Output of method using super in subclass
What is the output of this code?
Javascript
class Vehicle { start() { return 'Starting vehicle'; } } class Car extends Vehicle { start() { return super.start() + ' and car engine'; } } const myCar = new Car(); console.log(myCar.start());
Attempts:
2 left
💡 Hint
super calls the method from the parent class.
✗ Incorrect
Car's start method calls Vehicle's start using super, then adds text. The output is 'Starting vehicle and car engine'.