Sample Coding Questions Flashcards

1
Q

When logical OR, if one of the sides is TRUE it will return…

A

True!
Example:

true || false 
// it will evaluate to true!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When logical AND, if one of the sides is false it will return…

A

False!
Example:

true && false
// will evaluate to false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Syntax of the IF statement

A
if (condition) {
  //  block of code to be executed if the condition is true
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Make a Good Day greeting if the hour is less then 18:00

A

if (hour < 18) {
greeting = “Good day”;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If…else syntax

A
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
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Else if syntax

A
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
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why use the Switch Statement?

A

Use the switch statement to select one of many code blocks to be executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Switch statement syntax

A
switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

Always have a DEFAULT!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does the switch statement work?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Example of Switch statement

A

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";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the break keyword meant for in a switch statement?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Possible Exam Question….

Does the default case has to be the last case in a switch block?

A

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";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happends if multiple cases matches a case value (Switch statement)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Switch cases use strict comparison (===).

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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;
}

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
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)
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is is true of control structures?

A

They make decisions based on Boolean values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What can be a substitute for the nested if-else statements?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the purpose of the switch statement?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is true about the do-while statement?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Consider the following code:

for (X; Y; Z)

What does Y represent in this statement?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

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?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

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.");
}
A

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’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is a way to exit a loop that would otherwise continue to execute?

A

A way to exit a loop that would otherwise continue to execute is to use the break statement. Usually, you will find the break keyword inside an if clause. If a certain condition is reached, the program will break out of the loop. If not, the loop will continue. You can use the break statement within the for, while, and do…while loops, as well as with the switch statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Which JavaScript statement is used to force the flow of control back to the top of a loop?

A

continue

Feedback:
The continue statement is used to force the flow of control back to the top of a loop. The continue statement can be considered as a “skip” or bypass command. When execution hits the continue statement, all statements between it and the end of the loop block are skipped, and execution returns to the top of the loop. The test condition for the loop is then evaluated to determine whether the loop should execute again. The continue statement can be used only within the for or the while loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Consider the following code:

              var j = 0,
                  i;
              for (i = 0; i < 10; i++) {
                  document.write(i + " + ");
                  j += i;
              }
              document.write("=" + j);

What is the output when this script is run in the browser?

A

The loop executes 10 times and sums the numbers using j. An extra addition operator (+) appears because the document.write() method includes it after every number. It performs addition rather than concatenation because it recognizes the numbers are not string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Is the number of iterations in a do-while loop always one more than the while loop?

A

Not always.

Here both iterate 1 time:

// while loop
             var x = 1;
             while (x > 0) {
             document.write("a");
             x--;
             }
// dowhile loop
             var x = 1;
             do {
             document.write("a");
             x--;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Which JavaScript statement should you use to test for a single condition and branch to a process?

A

The if and if…else statements provide the ability to branch to one of two or more processes, depending on the result of some test condition that you have scripted. The if statement is used to test for a single condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Consider the following code block:

if (test == 10) {
alert(“Disqualified.”);
} else if (test == 25) {
alert(“Passed with grace marks.”);
} else if (test == 40) {
alert(“Passed.”);
} else if (test == 50) {
alert(“Passed with honors.”);
} else {
alert(“Inconclusive score.”);
}

A

The switch statement is best suited to replace the given code of segment. while and for are looping statements, therefore they will not be of much help. The problem with if statements without the else block is the last alert “inconclusive score.” If all other conditions fail, the else block must execute. Realizing this logic through if statements only will be inefficien

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Consider the following code snippet:

if (example = 25) {
document.write(“Correct answer.”);
} else {
document.write(“Wrong answer.”);
}

Assuming the value of example is 10, what will be the output of the code snippet?

A

In the if block, the test expression example = 25 will always return 25, because instead of the equality comparison operator (==), the assignment operator (=) is used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Will this run? Look at the missing curly braces….

if (timerMinutes == 10) alert(“Time Expired!”);

A

Yes, it will!

If the code associated with a test condition consists of just one line, you can omit the curly braces. There should be no quotation marks around the variable named (timerMinutes)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

How does JS evaluate the day’s placement in a week?

A

It doesn’t. It compares the values of the strings letter-by-letter and returns the value in the Boolean form.

Example:

             var day = "Thursday";
             if(day < "Wednesday"){
               window.alert("It's before today");
             }
             else{
               window.alert("It's after today")
             }

In this case, the string Thursday has the first letter as ‘T’ which is less than the letter ‘W’ of the string Wednesday, so the value Thursday is less than Wednesday. So, the operation evaluates to true and the script displays the output as It’s before today

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Consider the following code:

             var test = 100;
             if(test <= 6 && test >= 200 || test != 2) {
               window.alert("Good");
             }
             else{
               window.alert("Bad");
             }
A

The code is well-formed, so the window alert box will appear and display Good. Because this script uses an if/else statement, only one alert box will appear. The value of test (100) is not equal to 2 (see the last condition after the || operator), so the entire expression evaluates to true. Thus, the Boolean evaluates to true so the value Good is displayed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What will be the output of the following code block?

for (a = 10; a > 0; a–) {
document.write(a + “, “);
if (a == 5) break;
}

A

The for block will execute as usual, displaying the value of a at each iteration. But at the sixth iteration, the condition inside the if statement will result true, and the break; statement will execute, causing the loop to end. (

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Different Kinds of Loops

JavaScript supports different kinds of loops:

A

for - loops through a block of code a number of times
for/in - loops through the properties of an object
for/of - loops through the values of an iterable object
while - loops through a block of code while a specified condition is true
do/while - also loops through a block of code while a specified condition is true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What does the forEach method?

A
The forEach() method calls a function for each element in an array.
The forEach() method is not executed for empty elements.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Example of forEach method

A
let sum = 0;
const numbers = [65, 44, 12, 4];
numbers.forEach(myFunction);

document.getElementById(“demo”).innerHTML = sum;

function myFunction(item) {
  sum += item;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Using an array literal is the easiest way to create a JavaScript Array.

A
const cars = [
  "Saab",
  "Volvo",
  "BMW"
];
40
Q

It is considered good practice to name constructor functions with an upper-case first letter.

A

True

41
Q

Example of the constructor (with the keyword new)

A
// Constructor function for Person objects
function Person(first, last, age, eye) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
  this.eyeColor = eye;
}
// Create a Person object
const myFather = new Person("John", "Doe", 50, "blue");
42
Q

Add property to an Object

A

myFather.nationality = “English”;

43
Q

Add Method to an Object

A

myFather.name = function () {
return this.firstName + “ “ + this.lastName;
};

44
Q

Can you add a property to a constructor?

A

No. To add a new property to a constructor, you must add it to the constructor function:

function Person(first, last, age, eyecolor) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
  this.eyeColor = eyecolor;
  this.nationality = "English";
}
45
Q

Consider the following code block:

function sample(a, b) {
                 a++;
                 b++;
                 return a + b;
              }

Which of the following expressions is an equivalent of the sample() function?

A

The following expression represents an arrow function:

var sample = (m, n) => ++m + ++n;

Other expressions are either syntactically incorrect or will return incorrect values of variables. In the given code block, the value of the variables a and b is incremented before returning. The pre-increment operators modify the value of the variables before using them. The following expression will not increment the value before returning:

var sample = function (m, n) { return m++ + n++; }

46
Q

What is an argument in JavaScript?

A

A value passed into a function from outside the function. Also called parameters

47
Q

When does Event capturing start?

A

Event capturing starts at the highest level in the DOM hierarchy tree (generally, the Window object

48
Q

Consider the following code:

function dateTodayTomorrow(a, b){
  a.concat(b);
  return a.valueOf();
}
alert(dateTodayTomorrow('Wednesday','Thursday'));

What will the alert box display when this script is run in a browser?

A

The alert box will display the value of a only i.e. Wednesday, even though in the previous line there was a concatenation. But the concatenated value is not assigned to the variable a, so the initial value of the variable a will not be changed. Thus, Wednesday will be displayed in the alert box when the given script is run in the browser

49
Q

Are functions hoisted?

A

Yes. When a page is rendered, the function definitions are hoisted and all functions are created at runtime.

50
Q

The parseInt() Method

A

parseInt() parses a string and returns a whole number (integer)

51
Q

The parseFloat() Method

A

parseFloat() parses a string and returns a number.

parseFloat(“10”)
—> 10

52
Q

Which JavaScript property is used to add new properties and methods to objects?

A

The prototype property is used to add new properties and methods to objects. New methods and functions are attached to the prototype property of the object. These methods and functions are then automatically added to the object itself.

53
Q

All JavaScript objects inherit properties and methods from a prototype:

A

Date objects inherit from Date.prototype
Array objects inherit from Array.prototype
Person objects inherit from Person.prototype
The Object.prototype is on the top of the prototype inheritance chain:

54
Q

Prototype property Example ( add a property)

A
function Person(first, last, age, eyecolor) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
  this.eyeColor = eyecolor;
}

Person.prototype.nationality = “English”;

55
Q

Attention:

A

Only modify your own prototypes. Never modify the prototypes of standard JavaScript objects.

56
Q

How does a global variable differ from a local variable?

A

You can access a global variable value from any function or block that you define on the HTML page.

57
Q

To instantiate and then populate the properties of each new instance with actual data, you must _________ _________.

A

declare variables

58
Q

Target a frame, you need to know..

A

the frame’s number in the frames array OR the frame’s name

59
Q

Which of the following is the correct way to test for the presence of a cookie?

A

alert(document.cookie);

60
Q

Frames are arranged in a parent child hierarchy. With this in mind, in JavaScript, which keyword targets the parent of all parents in a frameset?

A

top

61
Q

A (an) __________ is a value or expression containing data or code that is passed on to a function or procedure.

A

argument

62
Q

You must declare variables that will become object references to a newly instantiated objects to _____________________

A

instantiate and then populate the properties of each new instance with actual data

63
Q

Jennie wants to retrieve the MIME information about her Web form. Which property of the form object retrieves this information?

A

encoding

64
Q

In JavaScript, what is the purpose of the form object?

A

The form object represents an HTML form in JavaScript. This object is used to access all properties of the form, most importantly, the elements that compose the form.

65
Q

How can you check if a user’s browser has Java enabled or not?

A

The javaEnabled() method of the navigator object can be used to check if a user’s browser has Java enabled or not. This method returns a Boolean value in return (true or false)

66
Q

Which line of code will allow you to redirect the browser to the page which you were previously viewing?

A

history.back();

67
Q

What is the limitation of a Virtual DOM?

A

Changes made to the Virtual DOM are not directly reflected on the webpage.

68
Q

What is the one property associated with the history object?

A

length

69
Q

Which object can be used to access cookies?

A

The documetn object has the cookie property

70
Q

What is the purpose of the location object?

A

To specify URLs in the script.

71
Q

Which code is the JavaScript equivalent of clicking a browser’s back button?

A

history.go(-1);

72
Q

What will protocol display?

A

The alert will display http:. The properties of the location object relate to the various pieces of information that form a complete URL. In many applications, accessing this information is extremely important. These properties allow the developer easy access to this data without having to perform rather complex string manipulation. The elements of the URL are identified as follows:

protocol://hostname:port/pathname/search#hash

where, protocol represents http, hostname:port represents www.myweb.com, pathname represents faq/answers, and search#hash represents protocol=email in the given URL

73
Q

What is the container of the form object?

A

document

74
Q

Which JavaScript object represents the frame of the browser and the mechanisms associated with it?

A

window

75
Q

How to bring a window object to the top?

A

A new browser window can be brought to the forefront by using the window.focus() method,

76
Q

Which method of the history object is used to create a new history entry?

A

The pushState() method of the history object is used to create new history entries

77
Q

What is diffing?

A

The process of comparing the new Virtual DOM with its previous version for identifying the elements (or objects) of the Virtual DOM that have changed (or updated) is called diffing. In React, after diffing, only those objects in the real DOM that have changed are updated

78
Q

How to check if image has loaded?

A

The complete property of the image object can be used to check if the referenced image has been loaded completely in the browser window.

79
Q

How many arguments does the generic syntax for the window.open() method use?

A

The generic syntax for the window.open() method uses three arguments, namely, a URL, a window name, and a list of window features

80
Q

Reg Exp

A

A regular expression is a sequence of characters that forms a search pattern.
The search pattern can be used for text search and text replace operations.

81
Q

Why use i in a string search (Reg Exp)

A

To do a case insensitve search

82
Q

Reg Exp modifiers

A

i Perform case-insensitive matching
g Perform a global match (find all matches rather than stopping after the first match)
m Perform multiline matching

83
Q

test() in Reg Ex

A

The test() method is a RegExp expression method.

It searches a string for a pattern, and returns true or false, depending on the result.

84
Q

You need to create a comma-delimited list from an array of values. Which function will accomplish this?

A

The correct function is myArray.join(“,”);. The join() method creates a string of an array’s values. The join() method requires a single argument: The character that will act as the separator for the element’s values.

85
Q

Consider the following code:

var firstArray = [“Sabrina”, “Kelly”, “Jill”]
var secondArray = firstArray;
secondArray.push(“Kris”);
alert(secondArray);

What output will be displayed in the alert box when this script is run in the browser?

A

The alert box will display Sabrina, Kelly, Jill, Kris. The push method adds the element to the end of the array.

86
Q

How do the shift() and pop() methods differ?

A

The shift() method works at the first element in the array. It removes and returns the first element in the array and then shifts the contents (index) of the array accordingly. The pop() method works on the last element in the array. It removes the last element from the array and returns it to the calling statement

87
Q

How can you extract the username portion of the email address given in the following line of code?

A

The correct code is email.substring(0, email.indexOf(“@”));. The substring() method will grab the characters starting with the first one (in index position 0) up to the character before the @ sign, which is located using the indexOf() property. The substring() method extracts the string up to the index position of the second parameter i.e. @, not including that character.

88
Q

Which code snippet can be used to display the current date?

A

You can use date and time information in JavaScript through the Date object. To use any date or time information, you must create a new instance or copy of the Date object using the new keyword. Considering this, the following code snippet is used to display the current date:

var myDate = new Date();
alert(myDate);
89
Q

Consider the following code:

var nurseryRhyme = “Mary had a little lamb”;

How would you find the position of the last letter “a” in the given code?

A

The correct code and syntax is:

nurseryRhyme.lastIndexOf(“a”);

The lastIndexOf method will return the last letter “a” in the string.

90
Q

When you need to run a function after a certain amount of time, which method would you use?

A

The correct statement is setTimeout(function, time);. To have a method or command repeat after a set amount of time, you use the setTimeout() method. It passes the parameters for the function to run and the time to wait before running it

91
Q

What does the map() return?

A

The map() method returns an array with the results of a function called for each element in an array. (

92
Q

An interface that you are developing will display an array in a list format. You want to allow the user to flip the list, making the first item last and the last item first, and reversing the order of all items. Which line of code will help you to achieve the task?

A

The correct statement is arrayList.reverse();. Arrays have a method that can be applied to them to flip or reverse the order. The reverse() method does not take any parameters. It will change the array so that the first entry is now last, the second entry is second to last, the last entry is first, and so forth

93
Q

What will be the output of the following expression: “Hi there!”.split(“e”)?

A

The split() method splits a string using the specified character or regular expression and returns an array of substrings. Here’s the syntax of the split() method:

string.split(split_character, number_of_splits);

where, split_character specifies the character over which the string is split and number_of_splits specifies the number of items (after the split) to be included in the returned array.

In the given expression, the split_character is e, so the expression will get split where the character e occurs. Since, the given expression contains two e’s, therefore, the number_of_splits that takes place will be twice. So, the output will be [“Hi th”, “r”, “!”].

94
Q

What is the purpose of the prototype property of the String objects?

A

It is used to create your own methods and properties of the String object.

95
Q

In JavaScript, the String, Math, Array, Date, and RegExp objects are called

A

In JavaScript, the String, Math, Array, Date, and RegExp objects are called language objects.

96
Q

Consider the following code:

var firstArray = ["Sabrina", "Kelly", "Jill"];
var secondArray = firstArray;
secondArray = secondArray.join("Kris");
alert(secondArray);

What will the alert box display when this script is run in the browser?

A

The alert box will display SabrinaKrisKellyKrisJill. The join method will add all of the elements in the array into a string using a parameter as the separator. Because Kris was the parameter, Kris was used instead of a comma, which is the default.

97
Q

Consider the following code:

var nurseryRhyme = “Mary had a little lamb”;

How would you find the letter at the seventh position of the given string?

A

You can find the letter at the seventh position of the given string using the charAt() method. The charAt() method will find the character at the specified position. For the given code, the correct syntax is nurseryRhyme.charAt(6);. You can count position beginning with 0, not 1.