Test Preperation (Part 2)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Typescript

Exam Preperation
(Part 02)
26. What keyword is used to declare a variable in TypeScript?

a) var

b) let

c) const

d) Both b) and c)

Answer: d) Both b) and c)

27. Which of the following variable declarations allows reassignment in TypeScript?

a) var

b) let

c) const

d) Both a) and b)

Answer: b) let

28. What is the key difference between var, let, and const in JavaScript/TypeScript?

a) They are all identical and can be used interchangeably.

b) var has block scope, while let and const have function scope.

c) var has function scope, while let and const have block scope.

d) let and const are used for declaring functions, while var is used for variables.

Answer: c) var has function scope, while let and const have block scope.

29. Which of the following is true about TypeScript's type inference for variables?

a) TypeScript cannot infer variable types.


b) TypeScript infers variable types based on the initial value assigned.

c) TypeScript requires explicit type annotations for all variables.

d) TypeScript infers types only for primitive data types.

Answer: b) TypeScript infers variable types based on the initial value assigned.

30. What happens if you declare a variable without initializing it in TypeScript?

a) It throws a compile-time error.

b) It assigns the variable a default value of undefined.

c) It assigns the variable a default value based on its type.

d) It automatically initializes the variable with a random value.

Answer: b) It assigns the variable a default value of undefined.

31. In TypeScript, what is the scope of a variable declared using the var keyword?

a) Global scope

b) Function scope

c) Block scope

d) Module scope

Answer: b) Function scope

32. What is the purpose of declaring a variable as const in TypeScript?

a) To indicate that the variable's value will never change.

b) To indicate that the variable's value will change frequently.

c) To allow the variable to be reassigned multiple times.

d) To specify that the variable should be hoisted.

Answer: a) To indicate that the variable's value will never change.


33 . Which TypeScript feature allows you to enforce stricter variable typing to catch
common errors during development?

a) Type assertions

b) Type inference

c) Type narrowing

d) Type annotations

Answer: d) Type annotations

34. What happens if you try to redeclare a variable using let or const in the same scope
in JavaScript/TypeScript?

a) It throws a syntax error.

b) It ignores the redeclaration and continues executing the code.

c) It automatically converts the variable to a global variable.

d) It updates the value of the variable without any error.

Answer: a) It throws a syntax error.

35. Which of the following statements is true regarding let and const in
JavaScript/TypeScript?

a) Both let and const can be reassigned.

b) let can be reassigned, but const cannot.

c) const can be reassigned, but let cannot.

d) Neither let nor const can be reassigned.

Answer: b) let can be reassigned, but const cannot.

36. Which of the following characters is commonly allowed in variable names in


Typescript?

a) $
b) @

c) #

d) &

Answer: a) $

37. What is the rule regarding starting variable names in Typescript?

a) They must start with a letter.

b) They can start with a letter or a digit.

c) They must start with a special character.

d) They cannot start with a digit.

Answer: a) They must start with a letter.

38. Which of the following is a valid variable name inTypescript?

a) my-variable

b) 1stVariable

c) var#

d) _myVariable

Answer: d) _myVariable

39. In Typescript, which of the following is a common convention for naming variables
with multiple words?

a) Using hyphens between words (e.g., my-variable)

b) Using underscores between words (e.g., my_variable)

c) Using spaces between words (e.g., my variable)

d) Both b) and c)

Answer: b) Using underscores between words (e.g., my_variable)


40. Which of the following variable names is considered to be more readable according
to common coding conventions?

a) numofstudents

b) numOfStudents

c) num_of_students

d) NumOfStudents

Answer: b) numOfStudents

41. Which operator is commonly used for string concatenation in Typescript?

a) +

b) -

c) *

d) /

Answer: a) +

42. In Typescript, what is the result of the following expression: "Hello" + " " +
"World"?

a) HelloWorld

b) Hello World

c) “HelloWorld”

d) SyntaxError

Answer: b) Hello World

43.In Typescript, which symbol is commonly used to terminate a statement?

a) .

b) :
c) ;

d) ,

Answer: c) ;

44. Which of the following is the correct syntax for declaring a variable in Typescript?

a) var myVar = 10;

b) myVar = 10;

c) int myVar = 10;

d) let myVar = 10;

Answer: d) let myVar = 10;

45. What are template literals in TypeScript primarily used for?

a) Declaring variables with constant values.

b) Concatenating strings.

c) Defining multi-line strings.

d) Declaring object literals.

Answer: c) Defining multi-line strings.

46. In TypeScript, what syntax is used to create template literals?

a) Single quotes (' ')

b) Double quotes (" ")

c) Backticks (` `)

d) Square brackets ([ ])

Answer: c) Backticks (` `)

47. Which of the following expressions correctly uses template literals in TypeScript?

a) `Hello, ${name}!`
b) "Hello, ${name}!"

c) 'Hello, ${name}!'

d) [Hello, ${name}!]

Answer: a) `Hello, ${name}!`

48. What advantage do template literals offer over traditional string concatenation in
TypeScript?

a) They provide a simpler syntax.

b) They allow for the interpolation of variables directly into the string.

c) They enforce strict typing of string values.

d) They offer better performance in runtime.

Answer: b) They allow for the interpolation of variables directly into the string.

49. What is the purpose of data types in TypeScript?

a) To restrict the values that a variable can hold.

b) To specify the size of a variable in memory.

c) To define the operations that can be performed on a variable.

d) To specify the order of execution of statements.

Answer: a) To restrict the values that a variable can hold.

50. Which of the following is not a built-in data type in TypeScript?

a) number

b) boolean

c) array

d) string

Answer: c) array
51. What is the data type of the following variable declaration in TypeScript: let age =
25;?

a) string

b) number

c) boolean

d) array

Answer: b) number

52. Which operator is used to perform type assertion in TypeScript?

a) :

b) =

c) <>

d) !

Answer: a) :

53. What is the purpose of type inference in TypeScript?

a) To explicitly specify the data type of a variable.

b) To automatically determine the data type of a variable based on its value.

c) To convert one data type to another data type.

d) To perform type checking at runtime.

Answer: b) To automatically determine the data type of a variable based on its value.

54. What is a type error in programming?

a) An error caused by incorrect syntax.

b) An error that occurs during runtime.

c) An error caused by using an incorrect data type.


d) An error caused by infinite loops.

Answer: c) An error caused by using an incorrect data type.

55. In statically typed languages like TypeScript, when are type errors typically
detected?

a) During compilation.

b) During runtime.

c) After the program has finished executing.

d) Type errors are not detected in statically typed languages.

Answer: a) During compilation.

56. What is the purpose of comments in TypeScript?

a) To execute code.

b) To document code and improve readability.

c) To declare variables.

d) To define functions.

Answer: b) To document code and improve readability.

57. Which symbol is used to start a single-line comment in TypeScript?

a) //

b) #

c) ;

d) /*

Answer: a) //

58. How do you write a multi-line comment in TypeScript?

a) /* This is a multi-line comment /


b) // This is a multi-line comment //

c) /* This is a multi-line comment */

d) // This is a multi-line comment

Answer: c) /* This is a multi-line comment */

59. Which of the following statements about comments in TypeScript is true?

a) Comments are executed by the compiler.

b) Comments are ignored by the compiler.

c) Comments are used to declare variables.

d) Comments are only used for debugging purposes.

Answer: b) Comments are ignored by the compiler.

60. What are operators used for in TypeScript?

a) To declare variables.

b) To perform arithmetic, logical, and other operations on variables and values.

c) To define functions.

d) To document code.

Answer: b) To perform arithmetic, logical, and other operations on variables and


values.

61. Which of the following is an arithmetic operator in TypeScript?

a) &&

b) ||

c) +

d) !

Answer: c) +
62. What does the === operator do in TypeScript?

a) It performs strict equality comparison.

b) It performs logical AND operation.

c) It performs addition.

d) It performs assignment.

Answer: a) It performs strict equality comparison.

63. What does the !== operator do in TypeScript?

a) It performs strict inequality comparison.

b) It performs logical OR operation.

c) It performs subtraction.

d) It performs assignment.

Answer: a) It performs strict inequality comparison.

64. What are logical operators used for in TypeScript?

a) To perform arithmetic operations.

b) To compare values.

c) To combine or manipulate boolean values.

d) To declare variables.

Answer: c) To combine or manipulate boolean values.

65. Which of the following is a logical operator in TypeScript?

a) +

b) &&

c) +

d) *
Answer: b) &&

66. What is the result of the logical AND (&&) operator when both operands are true?

a) true

b) false

c) null

d) undefined

Answer: a) true

67. What is the result of the logical OR (||) operator when one operand is true and the
other is false?

a) true

b) false

c) null

d) undefined

Answer: a) true

68. Which of the following is an example of a logical operator in TypeScript?

a) +

b) +=

c) ||

d) /

Answer: c) ||

69. What is the purpose of the "if" statement in programming?

a) To execute a block of code only if a specified condition is true.

b) To execute a block of code regardless of any condition.


c) To loop through a block of code until a condition becomes false.

d) To define a function.

Answer: a) To execute a block of code only if a specified condition is true.

70. When is the "else" statement used in conjunction with the "if" statement?

a) To execute a block of code only if the specified condition is false.

b) To execute a block of code regardless of any condition.

c) To loop through a block of code until a condition becomes false.

d) To define a function.

Answer: a) To execute a block of code only if the specified condition is false.

71. What is the purpose of the "else if" statement in programming?

a) To execute a block of code only if a specified condition is true, and to execute another
block of code if that condition is false.

b) To execute a block of code only if a specified condition is true.

c) To execute a block of code only if the preceding "if" condition is false and a new condition
is true.

d) To define a function.

Answer: c) To execute a block of code only if the preceding "if" condition is false and a
new condition is true.

72. What is the purpose of the tsc -w command in TypeScript?

a) To transpile TypeScript files into JavaScript files.

b) To watch for changes in TypeScript files and automatically recompile them.

c) To generate TypeScript documentation.

d) To run TypeScript files in watch mode.

Answer: b) To watch for changes in TypeScript files and automatically recompile them.

You might also like