Challenge - 5 Problems
Function and Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a class method call
What is the output of this TypeScript code?
Typescript
class Greeter { greeting: string; constructor(message: string) { this.greeting = message; } greet() { return `Hello, ${this.greeting}`; } } const greeter = new Greeter("world"); console.log(greeter.greet());
Attempts:
2 left
💡 Hint
Look at how the constructor sets the greeting property and how greet() uses it.
✗ Incorrect
The constructor sets the greeting property to 'world'. The greet() method returns 'Hello, ' plus the greeting property, so the output is 'Hello, world'.
❓ Predict Output
intermediate1:30remaining
Function return type and output
What will this TypeScript function return when called with argument 5?
Typescript
function multiplyByTwo(x: number): number { return x * 2; } console.log(multiplyByTwo(5));
Attempts:
2 left
💡 Hint
Check the function's return statement and the argument passed.
✗ Incorrect
The function multiplies the input number by 2 and returns it. For input 5, it returns 10.
🔧 Debug
advanced2:30remaining
Identify the error in this class declaration
What error does this TypeScript code produce?
Typescript
class Person { name: string; constructor(name: string) { this.name = name; } greet(): void { console.log('Hello, ' + this.names); } } const p = new Person('Alice'); p.greet();
Attempts:
2 left
💡 Hint
Check the property name used inside the greet method.
✗ Incorrect
The class has a property 'name', but the greet method tries to access 'this.names', which is undefined. This causes a TypeError at runtime.
📝 Syntax
advanced2:00remaining
Which option declares a valid function with default parameter?
Which of the following TypeScript function declarations is syntactically correct and sets a default value for parameter 'count'?
Attempts:
2 left
💡 Hint
Default parameters are assigned with '=' without '?' after the parameter name.
✗ Incorrect
Option B correctly assigns a default value to 'count'. Option B mixes optional and default syntax incorrectly. Options C and D have syntax errors with misplaced '?'.
🚀 Application
expert3:00remaining
Class inheritance and method overriding output
What is the output of this TypeScript code?
Typescript
class Animal { speak(): string { return 'Generic sound'; } } class Dog extends Animal { speak(): string { return 'Woof'; } } const pet: Animal = new Dog(); console.log(pet.speak());
Attempts:
2 left
💡 Hint
Think about which speak method runs when a Dog instance is assigned to an Animal type variable.
✗ Incorrect
Even though the variable type is Animal, the actual object is Dog. The speak method in Dog overrides Animal's method, so 'Woof' is printed.