What does the public access modifier mean in TypeScript?
The public modifier means the property or method can be accessed from anywhere: inside the class, subclasses, and outside the class.
Click to reveal answer
beginner
What is the effect of the private access modifier in TypeScript?
The <strong>private</strong> modifier restricts access to the property or method only within the class where it is declared. It cannot be accessed from outside or subclasses.
Click to reveal answer
beginner
How does the protected access modifier work in TypeScript?
The <strong>protected</strong> modifier allows access to the property or method inside the class and its subclasses, but not from outside these classes.
Click to reveal answer
intermediate
Consider this TypeScript code snippet:<br><pre>class Animal {
protected name: string;
constructor(name: string) {
this.name = name;
}
}
class Dog extends Animal {
public bark() {
console.log(`${this.name} barks`);
}
}</pre><br>Why can <code>this.name</code> be accessed inside <code>bark()</code>?
Because <code>name</code> is <strong>protected</strong>, it can be accessed inside the subclass <code>Dog</code>. Protected members are accessible in the class and its subclasses.
Click to reveal answer
intermediate
What happens if you try to access a <strong>private</strong> property from a subclass in TypeScript?
You get a compile-time error because <strong>private</strong> properties are only accessible within the class they are declared in, not in subclasses.
Click to reveal answer
Which access modifier allows a property to be accessed anywhere, including outside the class?
Aprotected
Bprivate
Cpublic
Dreadonly
✗ Incorrect
The public modifier allows access from anywhere.
If a property is marked as private, where can it be accessed?
AOnly inside the class it is declared
BInside the class and subclasses
CAnywhere in the program
DOnly outside the class
✗ Incorrect
Private properties are only accessible inside their own class.
Which access modifier allows access in the class and its subclasses but not outside?
Aprivate
Bprotected
Cpublic
Dstatic
✗ Incorrect
Protected members are accessible in the class and subclasses only.
What will happen if you try to access a protected property from outside the class hierarchy?
AIt will work fine
BIt will be undefined
CRuntime error
DCompile-time error
✗ Incorrect
Accessing protected members outside the class or subclasses causes a compile-time error.
Which of these is NOT an access modifier in TypeScript?
Aexternal
Bprivate
Cpublic
Dprotected
✗ Incorrect
'external' is not a valid access modifier in TypeScript.
Explain the differences between public, private, and protected access modifiers in TypeScript.
Think about who can see or use the property or method.
You got /3 concepts.
Describe a real-life example where you might use each access modifier in a TypeScript class.
Imagine a company with public services, private offices, and protected areas for managers.
You got /3 concepts.
Practice
(1/5)
1. Which access modifier in TypeScript allows a class member to be accessed from anywhere, including outside the class?
easy
A. private
B. public
C. protected
D. readonly
Solution
Step 1: Understand access modifiers
public means anyone can access the member, private restricts access to the class only, and protected allows access to the class and its subclasses.
Step 2: Identify the modifier allowing access from anywhere
Only public allows access from outside the class, including other classes and code.
Final Answer:
public -> Option B
Quick Check:
public = accessible everywhere [OK]
Hint: Public means open to all, private is class-only [OK]
Common Mistakes:
Confusing private with public access
Thinking protected is accessible outside class
Assuming readonly controls access
2. Which of the following is the correct syntax to declare a private property named age in a TypeScript class?
Access modifiers come before the property name and type, e.g., private age: number;.
Step 2: Identify correct modifier placement
private age: number; correctly places private before the property name and type. age: private number; has incorrect order.
Final Answer:
private age: number; -> Option A
Quick Check:
Modifier before name and type [OK]
Hint: Modifier comes before property name and type [OK]
Common Mistakes:
Placing modifier after property name
Using invalid syntax like 'age: private number;'
Confusing protected and private keywords
3. What will be the output of the following TypeScript code?
class Animal {
protected sound = 'Roar';
}
class Lion extends Animal {
public makeSound() {
return this.sound;
}
}
const lion = new Lion();
console.log(lion.makeSound());
medium
A. Error: Cannot access protected property
B. undefined
C. "Roar"
D. Error: Property 'sound' is private
Solution
Step 1: Understand protected access
The property sound is protected, so it can be accessed inside the class and its subclasses like Lion.
Step 2: Trace method call and output
The makeSound method returns this.sound, which is "Roar". So, console.log prints "Roar".
Final Answer:
"Roar" -> Option C
Quick Check:
Protected allows subclass access [OK]
Hint: Protected allows subclass access, so value is returned [OK]
Common Mistakes:
Thinking protected is inaccessible outside class
Confusing private with protected
Expecting runtime error for protected access
4. Identify the error in the following TypeScript code:
class Person {
private name: string = 'Alice';
}
const p = new Person();
console.log(p.name);
medium
A. Error: Property 'name' is protected, not accessible
B. No error, prints 'Alice'
C. Syntax error: missing constructor
D. Error: Cannot access private property 'name' outside class
Solution
Step 1: Check access modifier usage
The property name is declared private, so it cannot be accessed outside the class Person.
Step 2: Analyze the console.log statement
Trying to access p.name outside the class causes a compile-time error about private access.
Final Answer:
Error: Cannot access private property 'name' outside class -> Option D
Quick Check:
Private blocks outside access [OK]
Hint: Private blocks outside class access, causing error [OK]
Common Mistakes:
Assuming private properties are accessible outside
Confusing private with protected
Expecting code to print property value
5. Given the following TypeScript classes, which line will cause a compilation error?
class Vehicle {
protected speed: number = 0;
private engineOn: boolean = false;
public start() {
this.engineOn = true;
}
}
class Car extends Vehicle {
public accelerate() {
this.speed += 10; // Line 1
this.engineOn = true; // Line 2
}
}
const car = new Car();
car.accelerate();
hard
A. Line 2: Accessing private property 'engineOn' in subclass
B. No error, both lines are valid
C. Line 1: Accessing protected property 'speed' in subclass
D. Error at car.accelerate() call
Solution
Step 1: Understand access levels of properties
speed is protected, so accessible in subclass Car. engineOn is private, accessible only inside Vehicle.
Step 2: Analyze lines in subclass method
Line 1 accesses speed and is valid. Line 2 tries to access engineOn directly, which causes a compilation error.
Final Answer:
Line 2: Accessing private property 'engineOn' in subclass -> Option A
Quick Check:
Private blocks subclass access [OK]
Hint: Private blocks subclass access; protected allows it [OK]
Common Mistakes:
Assuming private is accessible in subclasses
Thinking protected is private
Ignoring error on private property access
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.