Computer Knowledge
Programming Fundamentals
2,381 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 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.
Which of the following is a statement that allows you to execute different blocks of code based on the value of a variable?
D
Correct answer
Explanation
The 'switch' statement is a statement that allows you to execute different blocks of code based on the value of a variable.
What is the syntax of a 'switch' statement in JavaScript?
-
switch (variable) { case value1: statement1; break; case value2: statement2; break; default: statement3; }
-
switch variable { case value1: statement1; break; case value2: statement2; break; default: statement3; }
-
switch (variable) case value1: statement1; break; case value2: statement2; break; default: statement3;
-
switch variable case value1: statement1; break; case value2: statement2; break; default: statement3;
A
Correct answer
Explanation
The syntax of a 'switch' statement in JavaScript is 'switch (variable) { case value1: statement1; break; case value2: statement2; break; default: statement3; }', where 'variable' is the variable whose value is being checked, 'value1' and 'value2' are the values being compared to the variable, 'statement1' and 'statement2' are the blocks of code to be executed if the variable matches 'value1' or 'value2', respectively, and 'statement3' is the block of code to be executed if the variable does not match any of the specified values.
Which of the following is a statement that allows you to terminate the execution of a loop or a switch statement?
-
break
-
continue
-
return
-
throw
A
Correct answer
Explanation
The 'break' statement is a statement that allows you to terminate the execution of a loop or a switch statement.
What is the syntax of a 'break' statement in JavaScript?
-
break;
-
break statement;
-
break condition;
-
break variable;
A
Correct answer
Explanation
The syntax of a 'break' statement in JavaScript is 'break;', where ';' is the statement terminator.
Which of the following is a statement that allows you to skip the current iteration of a loop and continue with the next iteration?
-
break
-
continue
-
return
-
throw
B
Correct answer
Explanation
The 'continue' statement is a statement that allows you to skip the current iteration of a loop and continue with the next iteration.
What is the syntax of a 'continue' statement in JavaScript?
-
continue;
-
continue statement;
-
continue condition;
-
continue variable;
A
Correct answer
Explanation
The syntax of a 'continue' statement in JavaScript is 'continue;', where ';' is the statement terminator.