Sample Coding Questions Flashcards
When logical OR, if one of the sides is TRUE it will return…
True!
Example:
true || false // it will evaluate to true!
When logical AND, if one of the sides is false it will return…
False!
Example:
true && false // will evaluate to false
Syntax of the IF statement
if (condition) { // block of code to be executed if the condition is true }
Make a Good Day greeting if the hour is less then 18:00
if (hour < 18) {
greeting = “Good day”;
}
If…else syntax
if (condition) { // block of code to be executed if the condition is true } else { // block of code to be executed if the condition is false }
Else if syntax
if (condition1) { // block of code to be executed if condition1 is true } else if (condition2) { // block of code to be executed if the condition1 is false and condition2 is true } else { // block of code to be executed if the condition1 is false and condition2 is false }
Why use the Switch Statement?
Use the switch statement to select one of many code blocks to be executed.
Switch statement syntax
switch(expression) { case x: // code block break; case y: // code block break; default: // code block }
Always have a DEFAULT!
How does the switch statement work?
The switch expression is evaluated once.
The value of the expression is compared with the values of each case.
If there is a match, the associated block of code is executed.
If there is no match, the default code block is executed.
Example of Switch statement
The getDay() method returns the weekday as a number between 0 and 6.
(Sunday=0, Monday=1, Tuesday=2 ..)
switch (new Date().getDay()) { case 0: day = "Sunday"; break; case 1: day = "Monday"; break; case 2: day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; }
What is the break keyword meant for in a switch statement?
When JavaScript reaches a break keyword, it breaks out of the switch block.
This will stop the execution inside the switch block.
It is not necessary to break the last case in a switch block. The block breaks (ends) there anyway.
Possible Exam Question….
Does the default case has to be the last case in a switch block?
No
IF default is not the last case in the switch block, remember to END the default case with a break.
Example: switch (new Date().getDay()) { default: text = "Looking forward to the Weekend"; break; case 6: text = "Today is Saturday"; break; case 0: text = "Today is Sunday"; }
What happends if multiple cases matches a case value (Switch statement)
If multiple cases matches a case value, the first case is selected.
If no matching cases are found, the program continues to the default label.
If no default label is found, the program continues to the statement(s) after the switch.
Switch cases use strict comparison (===).
The values must be of the same type to match.
A strict comparison can only be true if the operands are of the same type.
What will happen if the break statements in the following code block are replaced with the continue statements? (Assume that the value of test is ‘C’.)
switch(test) {
case ‘A’: alert(“Excellent.”); break;
case ‘B’: alert(“Good.”); break;
case ‘C’: alert(“Average.”); break;
case ‘D’: alert(“Below average.”); break;
default: (“Disqualified.”); break;
}
The given code block will display the SyntaxError: Illegal continue statement: no surrounding iteration statement message in the console window, because of the continue statement, which can be used in loops (or iteration statements).
Consider X and Z to be true and Y to be false, which of the following conditions will return false?
Y || Z
Y && Z
X && Z
X || Y
The condition Y && Z will return false, because the AND operator returns true only if both the operands are true. And, in this case, the operand Y is false
Which of the following statements will execute only if the conditions A and B are true? A. if (A) && if (B) B. if (A || B) C. if (A) & if (B) D. if (A && B)
The && logical operator is used to combine expressions so that the entire expression returns true only if all the individual conditions in the expression are true.
What is is true of control structures?
They make decisions based on Boolean values.
What can be a substitute for the nested if-else statements?
The switch statements can be used to substitute a nested if-else block. The conditions that are being tested in the if-else block can be specified as a case in the switch block
What is the purpose of the switch statement?
To compare a value against other values, to search for a match.
The switch statement compares a value against other values, to search for a match. If a match is found, then the code associated with the match will execute. The break statement is then used to exit the switch block of code. Essentially, the switch statement functions the same as multiple if-else clauses within an if statement. However, the switch statement is more readable and allows you to specify a default set of statements to execute if no match is foun
What is true about the do-while statement?
The do-while statement operates like the while statement, with one key difference. The do-while statement does not check the conditional expression until after the first time through the loop, guaranteeing that the code within the curly braces will execute at least once. A do-while loop can accomplish some tasks that the while loop cannot. The difference between the two control structures is that the do-while loop will execute its code at least one time, regardless of the Boolean value returned by the test condition.
Consider the following code:
for (X; Y; Z)
What does Y represent in this statement?
In the given statement for (X; Y; Z), Y is the condition under which the loop will execute. X is the loop counter variable initialization expression. Z is the expression that increments or decrements the loop counter
Consider the following code snippet:
var x = 100; while (x > 5) { x--; }
What will be the value of x after the execution of the given code snippet?
The value of x after the execution of this code will be 5. The condition under which the while statement executes is x > 5, so the last condition of the while loop will be executed when x has a value of 6. Now, the code inside the while statement will execute for x = 6, which is greater than 5, and the value of x will be decremented by 1 one more time making its value 5
Consider the following code:
var myPet= "rabbit"; switch (myPet) { case "Rabbit": document.write("My pet is a Rabbit."); break; case "Frog": document.write("My pet is a Frog."); break; default: document.write("I do not have a pet."); }
JavaScript is a case-sensitive language. Thus, the default output (I do not have a pet.) will be the result because the variable rabbit did not match with the variable Rabbit due to the case of the letter ‘R’