FSD Java Script (1)
FSD Java Script (1)
1. What is JavaScript?
a) JavaScript is a scripting language used to make the website interactive
b) JavaScript is an assembly language used to make the website interactive
c) JavaScript is a compiled language used to make the website interactive
d) None of the mentioned
Answer: a
Explanation: JavaScript is a scripting language used along with HTML and CSS to make the website
interactive along. It is used both on the client-side and server-side.
Answer: b
Explanation: The == in JS convert different types of operands to the same type before making the
comparison whereas a strict comparison === results in true value if the operands are of the same
type and the contents match.
Answer: a
Explanation: A === operator in JS is only true if the operands are of the same type and the contents
match. Two strings are strictly equal when they have the same sequence of characters, same length,
and same characters in corresponding positions. In this case, we are comparing an integer and a
string so it will be false.
Answer: d
Explanation: For functions expressed as expressions, the function name is optional in Javascript.
Sometimes function expressions are defined and used right away.
13. Which of the following object is the main entry point to all client-side JavaScript features and
APIs?
a) Position
b) Window
c) Standard
d) Location
Answer: b
Explanation: All client-side JavaScript features and APIs are accessed through the Window object. It
represents a web browser window or frame, and the identifier window can be used to refer to it.
function sanfoundry(javascript){
return (javascript ? “yes” : “no”);}
bool ans=true;
console.log(sanfoundry(ans));
a) Compilation error
b) Runtime error
c) Yes
d) No
Answer: c
Explanation: In javascript, “?” is called the ternary operator which is used for choosing one choice
from the given two choices. It is used instead of if else statement and makes the code shorter.
16. Which of the following can be used to call a JavaScript Code Snippet?
a) Function/Method
b) Preprocessor
c) Triggering Event
d) RMI
Answer: a
Explanation: A function call to the element on which JavaScript is to be run can be used to invoke
JavaScript code. Other techniques include onclick, onload, and onsubmit, among others.
19. Which of the following explains correctly what happens when a JavaScript program is developed
on a Unix Machine?
a) will work perfectly well on a Windows Machine
b) will be displayed as JavaScript text on the browser
c) will throw errors and exceptions
d) must be restricted to a Unix Machine only
Answer: a
Explanation: Because JS can run on a variety of operating systems, an application written for UNIX
will run just as well on Windows.
var num=10;
while(num>=1){
document.writeln(num);
num++;}
a) Code 1
b) Code 2
c) Both Code 1 and Code 2
d) Cannot Compare
Answer: a
Explanation: Code 1 would be more efficient JS code. Infact second code will go into runtime error as
the value of num will never reach less than or equal to one.
function printArray(a) {
var len = a.length, i = 0;
if (len == 0)
console.log("Empty Array");
else
{// do-while loop in javascript
do
{
console.log(a[i]);
} while (++i < len);
}}
a) Prints “Empty Array”
b) Prints 0 to the length of the array
c) Prints the numbers in the array in order
d) Prints the numbers in the array in the reverse order
Answer: c
Explanation: The do/while statement creates a loop that executes a block of javascript code once,
before checking if the condition is true, then it will repeat the loop as long as the condition is true.
Hence the iterator traverses through the array and print them in normal order.
var js = 0;
while (js < 10) {
console.log(js);
js++;}
a) An exception is thrown
b) The values of js are logged or stored in a particular location or storage
c) The value of js from 0 to 9 is displayed in the console
d) An error is displayed
Answer: c
Explanation: In JavaScript, Console.log is a predefined function that accepts the value as an argument.
At the time of code execution, console.log prints this value in the argument to the console.
int a=0;for(a;a<5;a++);
console.log(a);
a) 4
b) 5
c) 0
d) error
Answer: b
Explanation: The value of a will increase until it equals 5, at which point the cursor will exit the loop.
Because there are no statements in the for loop, the value of a will only increase. As a result, the result
will be five.
30. Which of the following methods/operation does javascript use instead of == and !=?
a) JavaScript uses equalto()
b) JavaScript uses equals() and notequals() instead
c) JavaScript uses bitwise checking
d) JavaScript uses === and !== instead
Answer: d
Explanation: The comma operator, bitwise operators, and the ++ and — operators are not included in
the subset. It also forbids the usage of == and!= due to the type conversion they do, instead requiring
the use of === and!==.
31. What will be the result or type of error if p is not defined in the following JavaScript code snippet?
console.log(p)
a) Value not found Error
b) Reference Error
c) Null
d) Zero
Answer: b
Explanation: Console.log() is a javascript predefined function for printing data or messages to the
console. A reference error will occur if the console.log argument is not defined.
32. What is the prototype represents in the following JavaScript code snippet?
Answer: d
Explanation: All object instances have a constructor property that points to the constructor function
that created them. A custom constructor is a constructor which requires no arguments and is created
automatically by the compiler at the time of object creation if not created by the user.
Answer: a
Explanation: JS code can change the behavior of windows, documents, and the elements that make
up those documents via event handlers.
35. Which of the following is the property that is triggered in response to JS errors?
a) onclick
b) onerror
c) onmessage
d) onexception
Answer: b
Explanation: The Window object’s onerror property acts as an event handler, and it is triggered when
JavaScript problems occur. However, because it is called with various arguments, it isn’t a genuine
event handler.
function compare(){
let sanfoundry=1;
let javascript="1";
if(sanfoundry.toString()===javascript)
return true;
else
return false;}
a) runtime error
b) logical error
c) true
d) false
Answer: c
Explanation: The toString() function can be used to convert a non-string (integer) to a string. Only if
the operands are of the same type and the contents match, then only the comparison is true. In this case,
both the operands to === operator are strings and have the same value “1”. So, the result is true.
37. What will be the firstname and surname of the following JavaScript program?
var book = {
"main title": "JavaScript",
'sub-title': "The Definitive Guide",
"for": "all audiences",
author: {
firstname: "David",
surname: "Flanagan"
}
};
a) objects
b) property names
c) properties
d) property values
Answer: b
Explanation: An item is contained within another object in the code sample above. The property names
are firstname and surname. The value of that property is an object in and of itself.
39. Consider the following JavaScript statement containing regular expressions and check if the pattern
matches.