1.
Do functions in JavaScript necessarily return a value?
Correct Answer
C. Few functions return values by default
Explanation
In JavaScript, functions do not necessarily return a value. While it is possible to explicitly specify a return value using the return statement, not all functions have a return statement. In such cases, the function will return undefined by default. Therefore, only a few functions in JavaScript actually return values by default.
2.
Which of the following is an example of a javascript function?
Correct Answer
A. Alert()
Explanation
The correct answer is "alert()". This is because "alert()" is a built-in JavaScript function that displays a dialog box with a message. It is commonly used to provide information or to prompt the user for input. The other options, "if()" and "script()", are not examples of JavaScript functions. "if()" is a conditional statement used for decision-making, and "script()" is not a valid JavaScript function. Therefore, the correct answer is "alert()".
3.
It is a group of reusable code which can be called anywhere in your
program.
Correct Answer
B. Functions
Explanation
Functions are a group of reusable code that can be called anywhere in a program. They allow for the organization and modularization of code, making it easier to manage and maintain. By encapsulating a set of instructions within a function, it can be called multiple times from different parts of the program, reducing redundancy and improving code efficiency. Additionally, functions can accept inputs (parameters) and return outputs, making them versatile and adaptable to different scenarios. Therefore, functions are the correct answer in this case.
4.
Consider the following code snippet:
var tensquared = (function(x) {return x*x;}(10));
Will the given code work?
Correct Answer
A. Yes, perfectly
Explanation
The given code will work perfectly. It defines a function called tensquared that takes an argument x and returns the square of x. The function is immediately invoked with the argument 10, resulting in the variable tensquared being assigned the value 100.
5.
Consider the following code snippet.
Correct Answer
B. 123xyz
Explanation
The correct answer is "123xyz" because it is the only value in the code snippet that is not a valid number. The other values, "123", "Exception", and "NaN", can all be interpreted as numbers in some context. However, "123xyz" contains non-numeric characters and cannot be parsed as a number.
6.
For the below-mentioned code snippet:
var o = new Object();
The equivalent statement is
Correct Answer
C. Var o= new Object;
Explanation
The correct answer is "var o = new Object;" because it correctly initializes a new object using the Object constructor. The other options are incorrect because they either do not use the "new" keyword or have incorrect syntax.
7.
The most common way to define
a function in JavaScript is by using the________ keyword.
Correct Answer
C. Function
Explanation
The most common way to define a function in JavaScript is by using the "function" keyword. This keyword is used to declare a new function and specify its name and parameters. It is followed by a block of code that defines the behavior of the function. By using the "function" keyword, developers can create reusable blocks of code that can be executed whenever needed.
8.
What is the output of the following snippet:
<html>
<head>
<script type="text/javascript">
function sayHello()
{
document.write ("Hello there!");
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type="button" onclick="sayHello()" value="Say Hello">
</form>
<p>Use different text in write method and then try...</p>
</body>
</html>
Correct Answer
D. When the button is clicked an output "Hello there!" will be displayed
Explanation
When the button is clicked, the function "sayHello()" is called. Inside the function, the "document.write()" method is used to write the text "Hello there!" to the document. Therefore, when the button is clicked, the output "Hello there!" will be displayed.
9.
Which of the following statement about the function is true?
Correct Answer
B. A function can take multiple parameters separated by comma.
Explanation
The given correct answer states that a function can take multiple parameters separated by a comma. This means that when defining a function, it is possible to specify multiple variables that can be passed as arguments when calling the function. This allows for flexibility and reusability of code, as different values can be passed to the function each time it is called.
10.
The follwing syntax is used to?<script type="text/javascript"><!--var variablename = new Function(Arg1, Arg2..., "Function Body");//--></script>
Correct Answer
A. To create a function using Function() constructor
Explanation
The given syntax is used to create a function using the Function() constructor. This syntax allows for the creation of a new function object by specifying the arguments and the function body within the constructor.
11.
Consider the following code snippet:
function constfuncs()
{
var funcs = [];
for(var i = 0; i < 10; i++)
funcs[i] = function() { return i; };
return funcs;
}
var funcs = constfuncs();
funcs[5]()
What does the last statement return?
Correct Answer
C. 10
Explanation
The last statement returns 10. This is because the variable "funcs" is assigned the value returned by the "constfuncs()" function, which is an array of functions. Each function in the array returns the value of the variable "i" when it was created. In the for loop, "i" is incremented from 0 to 9. So when the function at index 5 is called, it returns the value of "i" at that time, which is 10.
12.
The behavior of the instances present of a class inside a method is defined by:
Correct Answer
B. Classes
Explanation
The behavior of the instances present of a class inside a method is defined by classes. Classes are the blueprint for creating objects, and they define the properties (attributes) and behaviors (methods) of those objects. In this case, the behavior of the instances of a class is determined by the class itself, as methods are defined within the class and operate on its instances. Interfaces, on the other hand, define contracts for implementing classes but do not directly define the behavior of instances. Therefore, classes are the correct answer in this context.
13.
The keyword or the property that you use to refer to an object through which they were invoked is:
Correct Answer
C. This
Explanation
The keyword "this" is used to refer to the object through which a method or property was invoked. It allows access to the current instance of the object and is commonly used within the object's methods to refer to its own properties or invoke other methods.
14.
JavaScript's interaction with HTML is handled through ___ that occurs when the user or the browser manipulates a page.
Correct Answer
D. Event
Explanation
JavaScript's interaction with HTML is handled through events that occur when the user or the browser manipulates a page. Events are triggered by actions such as clicking a button, submitting a form, or hovering over an element. JavaScript can listen for these events and respond accordingly by executing functions or modifying the HTML content. This allows for dynamic and interactive web pages.
15.
This is the most frequently used event type that occurs when a user clicks the left button of his mouse.
Correct Answer
C. OnClick
Explanation
The correct answer is onClick. This event type is commonly used when a user clicks the left button of their mouse. The onSubmit event type is typically used in forms when the user submits the form, while the onmouseover event type is triggered when the user moves their mouse over an element. Therefore, the most appropriate event type for the given scenario is onClick.
16.
It is is an important keyword in JavaScript which can be used as a unary
operator that appears before its single operand, which may be of any type.
Correct Answer
B. Void
Explanation
The keyword "void" in JavaScript is used as a unary operator before an operand. It is commonly used to specify that a function does not return a value. When "void" is used before a function call, it ensures that the function is executed but the result is discarded. This can be useful in situations where the function is used for its side effects rather than its return value.
17.
It is the capability to store related information, whether data
or methods, together in an object.
Correct Answer
A. Encapsulation
Explanation
Encapsulation refers to the ability to store related information, such as data and methods, together in an object. This allows for better organization and modularity in programming, as the object can encapsulate its own data and methods, making it easier to manage and manipulate. Encapsulation also helps to ensure data integrity and security, as access to the object's internal data can be controlled through encapsulation.
18.
It is the capability to store one object inside another object.
Correct Answer
D. Aggregation
Explanation
Aggregation refers to the capability of storing one object inside another object. In this concept, one object is considered as the whole, while the other object is considered as a part. The whole object can exist without the part object, and the part object can also exist independently. This relationship allows for code reusability and flexibility in designing complex systems.
19.
It is the capability to write one function or method that works
in a variety of different ways.
Correct Answer
A. PolymorpHism
Explanation
Polymorphism refers to the ability of an object to take on many forms. In programming, it specifically refers to the capability of a function or method to behave differently based on the type of object it is called on. This allows for code reusability and flexibility, as the same function can be used with different types of objects, providing different behaviors based on the specific object being used.
20.
It is the capability of a class to rely upon another class (or
number of classes) for some of its properties and methods.
Correct Answer
B. Inheritance
Explanation
Inheritance is the capability of a class to rely upon another class (or number of classes) for some of its properties and methods. Inheritance allows a class to inherit the attributes and behaviors of another class, known as the parent or base class. This enables code reusability and promotes the concept of hierarchical relationships between classes. Subclasses can extend and specialize the functionality inherited from the parent class, while also adding their own unique attributes and methods.
21.
The syntax for adding a property to an object is:
Correct Answer
C. ObjectName.objectProperty = propertyValue;
Explanation
The correct answer is "objectName.objectProperty = propertyValue;". This syntax is used to add a property to an object. It assigns the value of "propertyValue" to the "objectProperty" of "objectName". The other options either include unnecessary "new" keyword or parentheses, which are not required in this context.
22.
Which of the following is an example to show how to use the
write() method of the document object to write any content on the document?
Correct Answer
B. Document.write ("Hello world");
Explanation
The correct answer is "document.write ("Hello world");". This is because the write() method is used to write content directly to the HTML document. In this example, the write() method is used to write the string "Hello world" onto the document. The other options are not valid examples of using the write() method.
23.
The____ operator is used to create an instance of an object.
Correct Answer
A. New
Explanation
The "new" operator is used to create an instance of an object. It allocates memory for the object and initializes its properties and methods. This allows us to create multiple instances of the same object type, each with its own set of values and behaviors.
24.
A _______is a function that creates and initializes an object.
Correct Answer
A. Constructor
Explanation
A constructor is a special type of function that is used to create and initialize objects. It is called when an object is created from a class and is responsible for setting initial values and performing any necessary setup tasks. Constructors are commonly used to ensure that objects are properly initialized before they are used, and they can also accept parameters to customize the initialization process. In this context, a constructor is the correct answer as it specifically refers to a function that creates and initializes an object.
25.
Which of the following syntax is an example of properly declaring a function?
Correct Answer
C. Book name is : Perl
Book name is : Perl
function sayHello()