NEW
Font size
WorksheetsVariables in Computer Science
Total questions: 7
Worksheet time: 11mins
Which of the following is not an action a programmer should take when debugging a segment of code?
Display the value of variables at various points during the program
Ask a friend or collaborator to look over the code segment to see if they are able to find any errors
Delete all the code and re-type it
Check the names of each variable for spelling and capitalization errors
Consider the following code:
var a = 0
var b = 2
var c = 6
a = a+b
b=a+c
c=a+b
What are the values of a, b, and c after this code segment has been run?
a= 0 b=2 c=6
a=2 b=6 c=2
a=2 b=2 c=8
a= 2 b=8 c=10
While writing an app, you create a variable to keep track of the number of items in a shopping cart. Every time someone clicks "addItemButton", the variable would increase by 1.
var cartTotal = 0
// <missing code>
console.log(cartTotal);
What code should you insert where it says <missing code> in order for the app to work?
cart total = 1
cartTotal + 1
cartTotal = cartTotal + 1
var cartTotal = cartTotal + 1
var cartTotal + 1
You are writing a reply function for a text messaging app. You want to swap the sender and receiver so that the value currently in From ends up as the value in To and To ends up in From. Which code segments will correctly swap the values?
to = from
from = to
from = var temp
to = temp
from = to
var temp = from
from = to
to = from
var temp = from
from = to
to = temp
What is displayed by the console.log statement after the following code?
var a = 2
var b = 4
var c = 10
a = b/a
b = c-a
c = b/a
console.log("value is: " + c)
value is: 2
value is: 4
value is: 6
value is: 10
What is the output to the console after the following code is run?
console.log(y);
var x = 10;
var y = 2 + x;
10
2
8
12
Error. Unkown identifier: y
What is the output to the console after the following code segment is run?
var x = 10;
increase();
x = x+3;
console.log(x);
function increase(){
var x =5}
5
8
10
13
Error. Cannot make a new variable x inside function increase()
