Font size
WorksheetsJavaScript and TypeScript Quiz
Total questions: 107
Worksheet time: 54mins
What is the purpose of `let` in JavaScript?
It creates a global variable
It creates a block-scoped variable
It creates a constant variable
It creates an undefined variable
How does JavaScript handle asynchronous code?
Using event loops
Using synchronous execution
Using promises only
Using immediate callbacks
What is the difference between `null` and `undefined` in JavaScript?
Both represent no value
`null` is assigned, `undefined` means no assignment
`undefined` is an object, `null` is a number
What is a closure in JavaScript?
A function inside another function that remembers the environment
An immediate function call
A function that modifies its own scope
A class-based function
What is `this` keyword in JavaScript?
Refers to the global object
Refers to the object in the current execution context
Always refers to the DOM element
Refers to the window object in strict mode
How does `bind()` work in JavaScript?
It binds a function to a context object
It creates a copy of a function
It modifies the value of `this`
It permanently changes the prototype of an object
What is the purpose of `Object.assign()`?
To assign values to variables
To merge objects into one
To create a copy of an array
To delete properties from an object
What is the event delegation in JavaScript?
Directly attaching events to child elements
Attaching events to a parent element to capture events on child elements
Blocking event propagation
Handling form submission with callbacks
How does `async` and `await` work in JavaScript?
`async` turns a function into a promise, `await` pauses execution until the promise resolves
`async` forces synchronous behavior, `await` is for handling errors
`await` works only with synchronous functions
`async` returns only errors
What is the difference between `forEach` and `map` in JavaScript?
`forEach` returns a new array, `map` modifies the original
`forEach` modifies the original, `map` returns a new array
`forEach` works on arrays only, `map` works on objects
`forEach` can stop early, `map` cannot
How does prototypal inheritance work in JavaScript?
Objects inherit directly from other objects via prototypes
Classes extend other classes through prototypes
Functions can only inherit through prototypes
Prototypes exist only on arrays and strings
What is hoisting in JavaScript?
Function and variable declarations are moved to the top of their scope
Only variables are moved to the top of their scope
Functions are executed at the end
Objects are declared before variables
What are the benefits of using arrow functions over traditional functions?
Arrow functions are more performant
Arrow functions do not have their own `this` context
Arrow functions have global scope
Arrow functions prevent memory leaks
What is the purpose of the `spread` operator in JavaScript?
To spread out the elements of an array
To combine objects into one
To perform destructuring
To change the scope of a function
What are JavaScript promises?
A way to handle synchronous code
A way to execute a function immediately
A proxy for a value that will be available in the future
A method to bind events
How does `setTimeout` differ from `setInterval`?
`setTimeout` runs code after a delay, `setInterval` runs code repeatedly
`setTimeout` stops after one call, `setInterval` runs until stopped
`setTimeout` modifies scope, `setInterval` forces global execution
`setTimeout` returns a callback, `setInterval` doesn't
What is the `call()` method used for in JavaScript?
To invoke a function with a specific `this` context
To define a new function
To modify object properties
To access an object's prototype chain
How does JavaScript handle errors in promises?
With try-catch blocks
With promise chaining
With `catch()` for rejected promises
By returning undefined for failed promises
What is `strict mode` in JavaScript?
It prevents variable hoisting
It enforces stricter parsing and error handling
It disallows function calls within loops
It modifies event propagation
What is destructuring in JavaScript?
Breaking down complex objects into simpler ones
Copying objects without references
Assigning object properties to variables directly
Removing properties from objects
What are template literals in JavaScript?
String literals that support multi-line strings and expressions
Special functions to handle arrays
Functions that deal with string interpolation
Methods for creating promises
What is the purpose of the `reduce` method in JavaScript?
To sum array values
To return a new array
To accumulate a single value based on array elements
To break an array into chunks
How does JavaScript's event loop work?
It processes events asynchronously using a queue
It blocks execution until events complete
It handles synchronous execution only
It executes events in parallel
What is the purpose of TypeScript's `interface`?
To define the structure of an object
To define a function's signature
To handle class inheritance
To enforce private variables
What is the difference between `interface` and `type` in TypeScript?
`interface` is more flexible than `type`
`type` can represent unions and intersections
`type` is used for methods only
`interface` handles object literals only
What is TypeScript's `any` type?
A type that can hold any value
A type that enforces type checking
A type that disables the compiler
A type for arrays and objects
How does `readonly` work in TypeScript?
It allows variables to be assigned only once
It prevents variables from being mutated
It forces shallow comparison of objects
It handles class inheritance
What is the purpose of TypeScript's `enum`?
To define a set of named constants
To create variables with restricted values
To ensure type safety in functions
To manage state in classes
How does TypeScript handle optional properties?
By using `?` after the property name
By using `null` values
By enforcing default values
By using the `readonly` keyword
What is the difference between `unknown` and `any` in TypeScript?
`unknown` enforces type checking, `any` does not
`unknown` is for functions, `any` is for objects
`unknown` is for errors, `any` is for types
`unknown` disables type safety
How do generics work in TypeScript?
They allow functions and classes to operate on multiple types without specifying them
They enforce strict typing
They create multiple instances of a class
They define constant values for objects
How do you enforce function argument types in TypeScript?
By specifying types in the function signature
By using `any` for all arguments
By using optional parameters
By adding type assertions
What is the `never` type used for in TypeScript?
For functions that never return
For variables that can hold any value
For default type parameters
For methods that have optional arguments
What is the difference between `null` and `undefined` in TypeScript?
`null` must be explicitly assigned, `undefined` means a variable has not been initialized
`null` and `undefined` are equivalent in TypeScript
`undefined` is an object, `null` is a string
What are modules in TypeScript?
Blocks of code to split functionalities into files
Templates for functions
Reserved words for type definitions
Methods to handle async code
How does `type assertion` work in TypeScript?
By overriding the inferred type of a variable
By creating a union of types
By adding an interface to the type
By forcing a specific return type
What is the purpose of `namespace` in TypeScript?
To group logically related code and avoid global scope pollution
To define private methods
To handle errors in classes
To manage memory efficiently
What is the difference between `var` and `let` in JavaScript?
`var` is function-scoped, `let` is block-scoped
`let` allows hoisting, `var` does not
`var` is block-scoped, `let` is function-scoped
`var` creates constants, `let` does not
How does `export` and `import` work in TypeScript?
They allow functions and variables to be shared across files
They handle object destructuring
They manage promises in async functions
They manage object lifecycles
How do decorators work in TypeScript?
They add behavior to classes or methods
They define type assertions
They enforce immutability
They handle object inheritance
What is the purpose of `type guards` in TypeScript?
To check if a value conforms to a specific type
To handle errors in functions
To prevent variable reassignment
To enforce immutability
What does TypeScript add to JavaScript that makes it more robust?
Strongly-typed variables
Dynamic typing
Automatic memory management
Built-in debugging tools
What is the purpose of TypeScript's "Type Declaration"?
To define the types of data a variable can hold
To declare a new variable
To perform type casting
To add comments to the code
Typescript is
interpreted language
compiled language
What does TypeScript add to JavaScript to help catch errors at compile time?
Static typing
Dynamic typing
Runtime checks
Enhanced performance
In TypeScript, which keyword is used to declare a variable?
let
const
var
type
variable
Which of the following is used to indicate the type of a variable in TypeScript?
typeOf
isType
:
#
What tool can you use to transpile TypeScript code into JavaScript code?
TypeScript Compiler (tsc)
JavaScript Transformer (jst)
TypeScript Formatter (tsf)
JavaScript Linter (eslint)
Javascript is a subset of Typescript
True
False
JSON is
Used to store data
Used to transfer Data
File format
Used to maintain the version of packages
Used for Configuration setting
Our project is dependent on some packages (called as dependencies) and these packages are dependent on some other dependencies and those dependencies are further dependent on some other dependencies and so on these are called as
(a)
What is node module in our project?
store your all packages
all the dependencies are inside the node module
it manage the version of our project
it lock all the packages
None of above
What is the shortest program of javascript?
(a)
What is an output of this code?
const a = "Quiz 1"
a = "Quiz cancel"
a = "Quiz postponed"
console.log(a)
Quiz 1
Quiz cancel
Quiz postponed
Error
What is an output of this code?
let var1 = "Typescript Cheety"
var1 = "23"
console.log(var1)
Type Number is not assignable to Type string
23
Typescript Cheety
Error
Below code is in Typescript file and is an example of ?
let var1 = "Typescript Cheety"
var1 = "Python Cheety"
console.log(var1)
Type Inferance
Explicit Typing
Dynamic Typing
None of Above
The JSON object is written inside curly braces { }
True
False
JSON object is only used in javascript and typescript language.
True
False
JSON is similar to Javascript object.
True
False
As JSON is similar to javscript object so we can access it using dot notation
True
False
What is an output of this code?
let var1 : string = "Pakistani Developers "
let var2 : string = "are number "
let var3 : string = "1"
console.log(var1 + var2 + var3)
Number is not assignable to type string
Error: Strings cannot be added together
Pakistani Developers are number 1
None of above
What is an output of this code?
let var1 : string = "Welcome to programming"
var1 = 101
console.log(var1)
Welcome to programming
101
Welcome to programming 101
Type number is not assignable to type string
We can reassign value to a variable that initialize with const
True
False
We can reassign value to a variable that initialize with let
True
False
What is an output of this code in typescript file?
let var1;
var1 = "Welcome to typescript class"
var1 = 23
var1 = false
console.log(var1)
Welcome to typescript class
23
false
Error
What typescript compiler infer the datatype in below code
let value = null;
Unknown
void
any
undefined
null
What is an output of below code?
let value : null = 101;
console.log(value)
101
null
undefined
Error
How would you describe Typescript as a programming language?
Strongly Typed
Weakly Typed
Dynamically Typed
Statistically Typed
Which file extension is commonly used for TypeScript files?
.js
.tx
.ts
.tys
TypeScript code is ultimately compiled into which language?
C++
Java
Python
JavaScript
What command correctly represents build/compiling Typescript to JavaScript
build
tsc
node tsc
build tsc
const student = 'Usman';
The implicit type here is a?
Number
String
Boolean
Object
Which of the following correctly define an explicit type?
let a = true
let a: boolean = true
let a = true:bolean
let boolan: a = true
At what time does checks in Typescripts occur?
Run time
Compile time
Code time
Dev Time
Rasheed wants to create an array of only strings, which syntax should he use?
names = [ ]
names: any = [ ]
names: string = [ ]
names: string[] = [ ]
Using this function:
const addNum = (num1: number, num2: number, num4?:number, num3: number = 10) => {}
num3 represents?
Optional parameters
Default parameters
Return type
None of the options
Using this function:
const addNum = (num1: number, num2: number, num4?:number, num3: number = 10) => {}
num4 represents?
Optional parameters
Default parameters
Return type
None of the options
Ella is creating a function to integrate data service providers(mtn, glo, etc). Which of the following concepts can help her define the different providers in her function
Type alias
Switch parameters
Optional parameters
Generics
Which of the following is used to create a union type in TypeScript?
&
|
~
^
What is the purpose of the "unknown" data type in TypeScript?
Represents a variable that can have any type
Represents a function that never returns
Represents an object with an unknown structure
There is no unkonwn
How can you declare a generic type in TypeScript?
Using the "generic" keyword
Using the "type" keyword
Using the "<>" syntax
Using the "any" keyword
A class can implement an interface
True
False
How na??
None of the options
const createPerson = <T>(person: T) => ...
Which of the following is a correct way to use the above function
createPerson('sam')
createPerson(true)
createPerson({
name: 'sam'
})
All of the options
enum Color {
Red,
Green,
Blue,
}
What is the value of blue?
0
1
2
3
let x: [string, number];
What does this represent?
Any Type
Union Type
Tuples
Array Type
Which of the following is not an access modifier
Public
Private
Protected
Readonly
Static
What is the purpose of the "typeof" operator in TypeScript?
To check the type of a variable
To define a new variable
To create an instance of an object
To import external modules
