1Which HTML tag is used to embed JavaScript code inside a webpage?
A.<js>
B.<javascript>
C.<script>
D.<code>
Correct Answer: <script>
Explanation:
The <script> tag is the standard HTML element used to define client-side scripts, such as JavaScript.
Incorrect! Try again.
2Which attribute of the <script> tag is used to include an external JavaScript file?
A.link
B.rel
C.href
D.src
Correct Answer: src
Explanation:
The 'src' attribute specifies the URL of an external script file.
Incorrect! Try again.
3Where is the correct place to insert a JavaScript in an HTML document?
A.The <head> section
B.Both the <head> section and the <body> section
C.The <body> section
D.The <footer> section
Correct Answer: Both the <head> section and the <body> section
Explanation:
JavaScript can be placed in both the <head> and <body> sections of an HTML page, depending on when the script needs to load.
Incorrect! Try again.
4What is the correct file extension for an external JavaScript file?
A..html
B..js
C..java
D..script
Correct Answer: .js
Explanation:
External JavaScript files are saved with the .js extension.
Incorrect! Try again.
5How do you write 'Hello World' in an alert box?
A.alert('Hello World');
B.alertBox('Hello World');
C.msgBox('Hello World');
D.msg('Hello World');
Correct Answer: alert('Hello World');
Explanation:
The alert() function is used to display a popup box with a specified message.
Incorrect! Try again.
6Which keyword is used to declare a variable that cannot be reassigned?
A.var
B.const
C.let
D.static
Correct Answer: const
Explanation:
The 'const' keyword defines a variable as a constant, meaning its value cannot be reassigned after initialization.
Incorrect! Try again.
7What is the scope of a variable declared with 'let' inside a block?
A.Block scope
B.Module scope
C.Function scope
D.Global scope
Correct Answer: Block scope
Explanation:
Variables declared with 'let' are limited to the block, statement, or expression on which they are used.
Incorrect! Try again.
8Which operator is used to check both value and type equality?
A.!=
B.==
C.=
D.===
Correct Answer: ===
Explanation:
The strict equality operator (===) checks if two values are equal in both value and data type.
Incorrect! Try again.
9What is the result of the expression: '5' + 3?
A.8
B.NaN
C.53
D.Error
Correct Answer: 53
Explanation:
When a number is added to a string, JavaScript converts the number to a string and concatenates them.
Incorrect! Try again.
10Which function is used to get input from the user?
A.prompt()
B.alert()
C.input()
D.confirm()
Correct Answer: prompt()
Explanation:
The prompt() method displays a dialog box that prompts the visitor for input.
Incorrect! Try again.
11What does the confirm() method return?
A.Object
B.String
C.Integer
D.Boolean
Correct Answer: Boolean
Explanation:
The confirm() method returns true if the user clicks OK, and false if the user clicks Cancel.
Incorrect! Try again.
12How does a 'while' loop start?
A.while (i <= 10; i++)
B.while (i <= 10)
C.until (i <= 10)
D.while i = 1 to 10
Correct Answer: while (i <= 10)
Explanation:
The syntax for a while loop is 'while (condition)'. It continues as long as the condition is true.
Incorrect! Try again.
13Which statement is used to stop a loop completely?
A.break
B.return
C.continue
D.stop
Correct Answer: break
Explanation:
The 'break' statement terminates the current loop or switch statement and transfers control to the statement following the terminated statement.
Incorrect! Try again.
14What is the purpose of the 'continue' statement?
A.Restarts the loop
B.Stops the loop execution
C.Exits the function
D.Skips the current iteration
Correct Answer: Skips the current iteration
Explanation:
The 'continue' statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration.
Incorrect! Try again.
15Which loop is guaranteed to execute at least once?
A.foreach
B.do...while
C.for
D.while
Correct Answer: do...while
Explanation:
A do...while loop executes the code block once before checking if the condition is true, then it will repeat the loop as long as the condition is true.
Incorrect! Try again.
16How do you write an IF statement in JavaScript?
A.if i = 5
B.if i = 5 then
C.if (i == 5)
D.if i == 5 then
Correct Answer: if (i == 5)
Explanation:
The correct syntax for an if statement requires the condition to be enclosed in parentheses: if (condition).
Incorrect! Try again.
17How do you write an IF statement for executing some code if 'i' is NOT equal to 5?
A.if i not 5
B.if (i <> 5)
C.if i =! 5 then
D.if (i != 5)
Correct Answer: if (i != 5)
Explanation:
The operator != represents 'not equal to' in JavaScript.
Incorrect! Try again.
18Which symbol is used for single-line comments in JavaScript?
A.**
B.//
C.//
D.<!-- -->
Correct Answer: //
Explanation:
Double slashes (//) are used for single-line comments in JavaScript.
Incorrect! Try again.
19What is the correct syntax for referring to an external script called 'app.js'?
A.<script href='app.js'>
B.<script file='app.js'>
C.<script src='app.js'>
D.<script name='app.js'>
Correct Answer: <script src='app.js'>
Explanation:
The 'src' attribute is strictly used to link external JavaScript files.
Incorrect! Try again.
20How do you create a JavaScript object?
A.var obj = ();
B.var obj = [];
C.var obj = {};
D.var obj = <>;
Correct Answer: var obj = {};
Explanation:
Curly braces {} are used to define an object literal in JavaScript.
Incorrect! Try again.
21Given: var person = {firstName:'John', lastName:'Doe'}; How do you access the lastName?
A.person[lastName]
B.person->lastName
C.person.lastName
D.person::lastName
Correct Answer: person.lastName
Explanation:
Object properties are accessed using dot notation (object.property) or bracket notation with quotes (object['property']).
Incorrect! Try again.
22Which keyword is used to define a variable in JavaScript (ES5 legacy)?
A.string
B.dim
C.int
D.var
Correct Answer: var
Explanation:
The 'var' keyword was the original way to declare variables in JavaScript before ES6 introduced 'let' and 'const'.
Incorrect! Try again.
23What is the correct way to write a JavaScript array?
Correct Answer: var colors = ['red', 'green', 'blue']
Explanation:
Arrays are defined using square brackets [].
Incorrect! Try again.
24Inside which HTML element do we put the JavaScript?
A.<script>
B.<scripting>
C.<js>
D.<javascript>
Correct Answer: <script>
Explanation:
The standard HTML tag for embedding scripts is <script>.
Incorrect! Try again.
25What will be the output of: typeof 'Hello'?
A.String
B.text
C.string
D.object
Correct Answer: string
Explanation:
The typeof operator returns the type of a variable. For string literals, it returns 'string'.
Incorrect! Try again.
26Which statement serves as the fall-back in a 'switch' statement?
A.else
B.default
C.break
D.case
Correct Answer: default
Explanation:
The 'default' keyword specifies the code to run if there is no case match in a switch statement.
Incorrect! Try again.
27What is the correct syntax for a 'for' loop?
A.for (i = 0; i <= 5; i++)
B.for (i = 0; i <= 5)
C.for i = 1 to 5
D.for (i <= 5; i++)
Correct Answer: for (i = 0; i <= 5; i++)
Explanation:
A for loop requires three statements: initialization, condition, and increment/decrement.
Incorrect! Try again.
28How can you add a comment that has more than one line?
A.<!-- This is a comment -->
B.// This is a comment //
C./ This is a comment /
D. This is a comment
Correct Answer: / This is a comment /
Explanation:
Multi-line comments in JavaScript start with / and end with /.
Incorrect! Try again.
29Which operator is used to assign a value to a variable?
A.*
B.==
C.-
D.=
Correct Answer: =
Explanation:
The = operator is the assignment operator.
Incorrect! Try again.
30What is the value of 'x' after: var x = 10; x += 5;
A.5
B.15
C.10
D.Error
Correct Answer: 15
Explanation:
The += operator adds the value on the right to the variable on the left and assigns the result to the variable (x = x + 5).
Incorrect! Try again.
31Which keyword identifies a block of code to be executed if a specified condition is false?
A.break
B.then
C.stop
D.else
Correct Answer: else
Explanation:
The 'else' statement specifies a block of code to be executed if the condition in the 'if' statement is false.
Incorrect! Try again.
32In an object method, what does 'this' refer to?
A.The function itself
B.The global object
C.The owner object
D.The variable name
Correct Answer: The owner object
Explanation:
In a method, 'this' refers to the object that 'owns' the method.
Incorrect! Try again.
33Which logical operator represents 'OR'?
A.&
B.!
C.||
D.&&
Correct Answer: ||
Explanation:
The || operator represents logical OR.
Incorrect! Try again.
34What is the correct way to check if a variable 'car' is undefined?
A.if (car is undefined)
B.if (car == null)
C.if (car === undefined)
D.if (typeof car == 'undefined')
Correct Answer: if (typeof car == 'undefined')
Explanation:
Using typeof allows checking for undefined without throwing an error if the variable hasn't been declared.
Incorrect! Try again.
35Which of the following is NOT a valid JavaScript variable name?
A._myVar
B.2myVar
C.myVar2
D.$myVar
Correct Answer: 2myVar
Explanation:
Variable names cannot start with a number.
Incorrect! Try again.
36What syntax is used to access a method 'start' of an object 'engine'?
A.call engine.start
B.engine.start()
C.engine->start()
D.engine['start']
Correct Answer: engine.start()
Explanation:
Methods are accessed using dot notation followed by parentheses to invoke the function.
Incorrect! Try again.
37What happens if you redeclare a variable using 'let' in the same scope?
A.It creates a new variable
B.It overwrites the previous value
C.It causes a SyntaxError
D.Nothing happens
Correct Answer: It causes a SyntaxError
Explanation:
Variables declared with 'let' (and 'const') cannot be redeclared in the same scope.
Incorrect! Try again.
38Which property is used to find the length of a string?
A.length
B.len
C.count
D.size
Correct Answer: length
Explanation:
The .length property returns the number of characters in a string.
Incorrect! Try again.
39What is the output of Boolean(10 > 9)?
A.false
B.undefined
C.NaN
D.true
Correct Answer: true
Explanation:
The expression 10 > 9 is true, so Boolean() returns true.
Incorrect! Try again.
40Which is the ternary operator?
A.||
B.?:
C.??
D.&&
Correct Answer: ?:
Explanation:
The conditional (ternary) operator is the only JavaScript operator that takes three operands: condition ? exprIfTrue : exprIfFalse.
Incorrect! Try again.
41What keyword defines a switch case block?
A.if
B.when
C.option
D.case
Correct Answer: case
Explanation:
The 'case' keyword marks a block of code to be executed in a switch statement if the match is found.
Incorrect! Try again.
42How do you delete a property from a JavaScript object?
A.remove object.property
B.delete object.property
C.object.property = null
D.clear object.property
Correct Answer: delete object.property
Explanation:
The 'delete' operator removes a property from an object.
Incorrect! Try again.
43Which popup box returns null if the user clicks 'Cancel'?
A.confirm
B.prompt
C.alert
D.message
Correct Answer: prompt
Explanation:
If the user clicks Cancel in a prompt box, the function returns null.
Incorrect! Try again.
44What is Hoisting in JavaScript?
A.Moving declarations to the top
B.Looping efficiently
C.Moving initializations to the top
D.Deleting unused variables
Correct Answer: Moving declarations to the top
Explanation:
Hoisting is JavaScript's default behavior of moving declarations (specifically var and function declarations) to the top of the current scope.
Incorrect! Try again.
45Which of the following is a valid const declaration?
A.const PI;
B.const PI = 3.14;
C.const 3.14 = PI;
D.const PI = 3.14; PI = 3.14159;
Correct Answer: const PI = 3.14;
Explanation:
Const variables must be assigned a value when they are declared.
Incorrect! Try again.
46What loop is best suited for iterating over the properties of an object?
A.for...in
B.while
C.do...while
D.for
Correct Answer: for...in
Explanation:
The for...in loop iterates over the enumerable properties of an object.
Incorrect! Try again.
47Which symbol is used for the modulus (remainder) operator?
A.&
B.*
C./
D.%
Correct Answer: %
Explanation:
The % operator returns the division remainder.
Incorrect! Try again.
48If x = 10 and y = '10', what is the result of x == y?
A.true
B.undefined
C.false
D.NaN
Correct Answer: true
Explanation:
The == operator performs type coercion, so the number 10 is equal to the string '10'.
Incorrect! Try again.
49If x = 10 and y = '10', what is the result of x === y?
A.undefined
B.false
C.true
D.NaN
Correct Answer: false
Explanation:
The === operator does not perform type coercion. A number is not strictly equal to a string.
Incorrect! Try again.
50Which global object do alert(), confirm(), and prompt() belong to?
A.console
B.window
C.html
D.document
Correct Answer: window
Explanation:
These popup methods are part of the browser's Window object.
Incorrect! Try again.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill.
The rest comes out of a student's own pocket: the domain, the storage,
and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it.
What it pays for →