TypeScript Variables and Constants Quiz

TypeScript
0 Passed
0% acceptance

A 35-question TypeScript & JavaScript quiz covering variable declarations, scope rules, hoisting, reassignment behavior, constants, immutability concepts, shadowing, and best practices for naming variables.

35 Questions
~70 minutes
1

Question 1

Which declaration creates a block-scoped variable?

A
let
B
var
C
function
D
define
2

Question 2

What is true about var declarations?

A
They are function-scoped
B
They are block-scoped
C
They prohibit redeclaration
D
They cannot store strings
3

Question 3

What is the result?

javascript
let x = 5;
x = 10;
console.log(x);
A
10
B
5
C
undefined
D
Error
4

Question 4

Which declaration prevents reassignment?

A
const
B
let
C
var
D
assign
5

Question 5

What happens here?

javascript
const count = 1;
count = 2;
A
TypeError
B
2
C
1
D
undefined
6

Question 6

What is hoisting?

A
Moving declarations to the top of their scope at compile time
B
A way to rename variables
C
Automatic optimization
D
A feature for async execution
7

Question 7

What prints?

javascript
console.log(a);
var a = 10;
A
undefined
B
10
C
ReferenceError
D
TypeError
8

Question 8

What happens here?

javascript
console.log(b);
let b = 5;
A
ReferenceError
B
undefined
C
5
D
null
9

Question 9

Which scope type does var use?

A
Function scope
B
Block scope
C
Module scope only
D
Package scope
10

Question 10

What logs?

javascript
{ let x = 1; }
console.log(typeof x);
A
undefined
B
number
C
object
D
string
11

Question 11

What prints?

javascript
function test() {
  var a = 10;
}
console.log(typeof a);
A
undefined
B
10
C
number
D
Error
12

Question 12

What is true about block scope?

A
Variables inside {} cannot be accessed outside with let/const
B
var respects block boundaries
C
Blocks create new scopes for all declarations
D
Functions ignore blocks entirely
13

Question 13

Which declarations allow redeclaration in the same scope?

A
var only
B
let and const
C
const only
D
let only
14

Question 14

What happens?

javascript
let x = 1;
let x = 2;
A
SyntaxError
B
2
C
1
D
undefined
15

Question 15

What does const prevent?

A
Rebinding the variable identifier
B
Mutating objects
C
Named exports
D
Global scoping
16

Question 16

What logs?

javascript
const obj = { n: 1 };
obj.n = 2;
console.log(obj.n);
A
2
B
1
C
Error
D
undefined
17

Question 17

What is required to make an object fully immutable?

A
Using Object.freeze on nested objects too
B
Declaring with const only
C
Using let
D
Switching to another file
18

Question 18

What happens with shadowing here?

javascript
let x = 1;
{
  let x = 2;
  console.log(x);
}
A
2
B
1
C
undefined
D
ReferenceError
19

Question 19

Shadowing occurs when:

A
An inner scope declares a variable with the same name
B
Two variables share a type
C
A function has no return
D
A constant is frozen
20

Question 20

What prints?

javascript
var a = 10;
(function(){ var a = 20; console.log(a); })();
A
20
B
10
C
undefined
D
Error
21

Question 21

Which naming style is recommended for variables?

A
camelCase
B
PascalCase for all variables
C
ALL_CAPS for everything
D
Single-letter names always
22

Question 22

What makes a variable name good?

A
It clearly describes the stored value
B
It is as short as possible
C
It matches another file’s name
D
It uses URL-safe characters only
23

Question 23

Why should overly generic names be avoided?

A
They make it harder to understand intent
B
They slow down execution
C
They break strict mode
D
They reduce hoisting
24

Question 24

What logs?

javascript
console.log(x);
var x = 5;
console.log(x);
A
undefined then 5
B
5 then 5
C
Error
D
null then 5
25

Question 25

Which declaration helps avoid accidental global leakage?

A
let
B
var
C
implicit declarations
D
function
26

Question 26

What happens if you assign a variable without declaring it?

A
It becomes a global variable (non-strict mode)
B
It throws a SyntaxError always
C
It becomes block scoped
D
It becomes const implicitly
27

Question 27

Which is true about const with primitive values?

A
The value itself cannot change
B
It can be reassigned
C
It becomes hoisted with undefined
D
It is automatically deep frozen
28

Question 28

Why is var generally discouraged?

A
It has confusing scoping and hoisting behaviour
B
It cannot store strings
C
It is deprecated
D
It breaks import statements
29

Question 29

What is the main benefit of using const whenever possible?

A
It communicates intent that the binding will not change
B
It guarantees faster execution
C
It eliminates runtime errors
D
It avoids needing semicolons
30

Question 30

What is the recommended default declaration for new variables?

A
const
B
let
C
var
D
auto
31

Question 31

Which is a valid reason to choose let instead of const?

A
The value must change over time
B
It loads faster
C
It hoists more cleanly
D
It allows redeclaration
32

Question 32

What is the effect of block-scoped declarations in loops?

A
Each iteration gets its own binding
B
The loop runs faster
C
The loop becomes immutable
D
It disables closures
33

Question 33

Which variable naming pattern should be avoided?

A
Ambiguous names like tmp or data
B
camelCase for variables
C
Prefixing booleans with is or has
D
Using nouns for constants
34

Question 34

What prints?

javascript
let n = 1;
{
  n = 2;
}
console.log(n);
A
2
B
1
C
undefined
D
Error
35

Question 35

Best practice suggests avoiding:

A
Implicit globals
B
Using const when possible
C
Readable names
D
Block scoping

QUIZZES IN TypeScript