Computer Knowledge
Programming Fundamentals
2,611 Questions
Programming fundamentals form the core logic of software development and computer science. This includes variable declarations, pointer assignments, loop iterations, and exception handling. These technical topics are regularly tested in computer knowledge and IT officer competitive examinations.
Variables and arraysPointer assignmentsLoop iterationsException handling blocksCompile time errorsFunction references
Programming Fundamentals Questions
What is the correct syntax for creating a loop in JavaScript?
-
for (let i = 0; i < array.length; i++) { ... }
-
for (const i in array) { ... }
-
for (const i of array) { ... }
-
All of the above
D
Correct answer
Explanation
In JavaScript, there are three types of loops: the for loop, the for...in loop, and the for...of loop. The for loop is used to iterate over a range of numbers, the for...in loop is used to iterate over the properties of an object, and the for...of loop is used to iterate over the elements of an array.
What is the difference between a break statement and a continue statement in JavaScript?
-
A break statement exits the loop immediately, while a continue statement skips the current iteration of the loop and continues with the next iteration.
-
A break statement exits the loop immediately, while a continue statement exits the current iteration of the loop and starts the next iteration.
-
A break statement skips the current iteration of the loop and continues with the next iteration, while a continue statement exits the loop immediately.
-
None of the above
A
Correct answer
Explanation
In JavaScript, a break statement exits the loop immediately, while a continue statement skips the current iteration of the loop and continues with the next iteration.
What is the correct syntax for creating a conditional statement in JavaScript?
-
if (condition) { ... }
-
if (condition) { ... } else { ... }
-
if (condition) { ... } else if (condition) { ... } else { ... }
-
All of the above
D
Correct answer
Explanation
In JavaScript, there are three types of conditional statements: the if statement, the if...else statement, and the if...else if...else statement. The if statement is used to execute a block of code if a condition is true, the if...else statement is used to execute one block of code if a condition is true and another block of code if the condition is false, and the if...else if...else statement is used to execute one block of code if a condition is true, another block of code if a different condition is true, and a third block of code if both conditions are false.
What is the correct syntax for creating a function in JavaScript?
-
function functionName() { ... }
-
const functionName = () => { ... }
-
let functionName = () => { ... }
-
All of the above
D
Correct answer
Explanation
In JavaScript, you can create a function using the function keyword, followed by the function name and parentheses. You can also create a function using an arrow function, which is a concise syntax for writing a function. Arrow functions can be written using the const or let keyword, followed by the arrow (=>) symbol and the function body.
How do you define a function in JavaScript?
-
function functionName() {}
-
functionName() {}
-
var functionName = function() {}
-
var functionName = {}
A
Correct answer
Explanation
To define a function in JavaScript, you use the function keyword followed by the function name and parentheses. Inside the parentheses, you specify the parameters that the function will receive. The function body, where the code that will be executed when the function is called is placed, is enclosed in curly braces.
How do you call a function in JavaScript?
-
functionName();
-
functionName
-
functionName = ();
-
functionName {}
A
Correct answer
Explanation
To call a function in JavaScript, you simply use the function name followed by parentheses. You can pass arguments to the function by placing them inside the parentheses when you call the function.
What is the difference between a function declaration and a function expression?
-
Function declarations are hoisted, while function expressions are not.
-
Function expressions can be used as values, while function declarations cannot.
-
Function declarations can be named, while function expressions cannot.
-
All of the above
D
Correct answer
Explanation
Function declarations are hoisted to the top of their scope, while function expressions are not. Function expressions can be used as values, while function declarations cannot. Function declarations can be named, while function expressions cannot.
What is a callback function?
-
A function that is passed as an argument to another function
-
A function that is called from within another function
-
A function that is defined inside another function
-
All of the above
D
Correct answer
Explanation
A callback function is a function that is passed as an argument to another function. The callback function is then called from within the other function. Callback functions are often used to handle asynchronous operations.
What is a closure in JavaScript?
-
A function that has access to the variables of its outer scope, even after the outer scope has finished executing.
-
A function that is defined inside another function.
-
A function that can be called multiple times.
-
None of the above
A
Correct answer
Explanation
A closure in JavaScript is a function that has access to the variables of its outer scope, even after the outer scope has finished executing. This is because the function retains a reference to the outer scope's variables.
Which of the following is a conditional statement in JavaScript?
A
Correct answer
Explanation
The 'if' statement is a conditional statement in JavaScript. It allows you to execute a block of code only if a certain condition is met.
What is the syntax of an 'if' statement in JavaScript?
-
if (condition) { statement }
-
if condition { statement }
-
if (condition) statement
-
if condition statement
A
Correct answer
Explanation
The syntax of an 'if' statement in JavaScript is 'if (condition) { statement }', where 'condition' is the condition that determines whether the statement will be executed or not.
Which of the following is a loop statement in JavaScript?
B
Correct answer
Explanation
The 'for' statement is a loop statement in JavaScript. It allows you to execute a block of code multiple times, based on a specified condition.
What is the syntax of a 'for' loop in JavaScript?
-
for (initialization; condition; increment) { statement }
-
for initialization; condition; increment { statement }
-
for (initialization; condition) statement
-
for initialization condition increment statement
A
Correct answer
Explanation
The syntax of a 'for' loop in JavaScript is 'for (initialization; condition; increment) { statement }', where 'initialization' is the initial value of the loop variable, 'condition' is the condition that determines whether the loop will continue to execute, and 'increment' is the value by which the loop variable is incremented after each iteration.
Which of the following is a type of loop that executes a block of code at least once, and then continues to execute the block as long as a certain condition is met?
-
for
-
while
-
do-while
-
switch
C
Correct answer
Explanation
The 'do-while' loop is a type of loop that executes a block of code at least once, and then continues to execute the block as long as a certain condition is met.
What is the syntax of a 'do-while' loop in JavaScript?
-
do { statement } while (condition);
-
do statement while (condition);
-
do { statement } while condition;
-
do statement while condition;
A
Correct answer
Explanation
The syntax of a 'do-while' loop in JavaScript is 'do { statement } while (condition);', where 'statement' is the block of code to be executed, and 'condition' is the condition that determines whether the loop will continue to execute.