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

Multiple choice

What is the correct syntax for creating a loop in JavaScript?

  1. for (let i = 0; i < array.length; i++) { ... }

  2. for (const i in array) { ... }

  3. for (const i of array) { ... }

  4. All of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

What is the difference between a break statement and a continue statement in JavaScript?

  1. A break statement exits the loop immediately, while a continue statement skips the current iteration of the loop and continues with the next iteration.

  2. A break statement exits the loop immediately, while a continue statement exits the current iteration of the loop and starts the next iteration.

  3. A break statement skips the current iteration of the loop and continues with the next iteration, while a continue statement exits the loop immediately.

  4. None of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

What is the correct syntax for creating a conditional statement in JavaScript?

  1. if (condition) { ... }

  2. if (condition) { ... } else { ... }

  3. if (condition) { ... } else if (condition) { ... } else { ... }

  4. All of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

What is the correct syntax for creating a function in JavaScript?

  1. function functionName() { ... }

  2. const functionName = () => { ... }

  3. let functionName = () => { ... }

  4. All of the above

Reveal answer Fill a bubble to check yourself
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 (=&gt;) symbol and the function body.

Multiple choice

How do you define a function in JavaScript?

  1. function functionName() {}

  2. functionName() {}

  3. var functionName = function() {}

  4. var functionName = {}

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

How do you call a function in JavaScript?

  1. functionName();

  2. functionName

  3. functionName = ();

  4. functionName {}

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

What is the difference between a function declaration and a function expression?

  1. Function declarations are hoisted, while function expressions are not.

  2. Function expressions can be used as values, while function declarations cannot.

  3. Function declarations can be named, while function expressions cannot.

  4. All of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

What is a callback function?

  1. A function that is passed as an argument to another function

  2. A function that is called from within another function

  3. A function that is defined inside another function

  4. All of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

What is a closure in JavaScript?

  1. A function that has access to the variables of its outer scope, even after the outer scope has finished executing.

  2. A function that is defined inside another function.

  3. A function that can be called multiple times.

  4. None of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

Which of the following is a conditional statement in JavaScript?

  1. if

  2. for

  3. while

  4. do-while

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

What is the syntax of an 'if' statement in JavaScript?

  1. if (condition) { statement }

  2. if condition { statement }

  3. if (condition) statement

  4. if condition statement

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

Which of the following is a loop statement in JavaScript?

  1. if

  2. for

  3. while

  4. do-while

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

What is the syntax of a 'for' loop in JavaScript?

  1. for (initialization; condition; increment) { statement }

  2. for initialization; condition; increment { statement }

  3. for (initialization; condition) statement

  4. for initialization condition increment statement

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

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?

  1. for

  2. while

  3. do-while

  4. switch

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

What is the syntax of a 'do-while' loop in JavaScript?

  1. do { statement } while (condition);

  2. do statement while (condition);

  3. do { statement } while condition;

  4. do statement while condition;

Reveal answer Fill a bubble to check yourself
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.