Unit 5 Programming

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the output to the console after the following code segment is executed? 1 var x = 10; 2 increase(); 3 x = x+3; 4 console.log(x); 5 function increase(){ 6 var x = 5; 7 }

13

Assuming that the list data below is given as the parameter to the procedure, what is returned by the following procedure? Index: 1 2 3 4 5 data 3 10 4 16 9 PROCEDURE mystery(data) { val = 0 FOR EACH item IN data { IF (item > val) { val = item } } RETURN (val) }

16

The following program processes the list data shown below. What is the output of the following program? Index: 1 2 3 4 5 data 3 10 4 9 16 val = 0 i = 1 REPEAT 3 TIMES { val = val + data[i] i = i + 1 } DISPLAY (val)

17

The counter variable in the code below increments by 1 each time through the loop. What will the value of counter be after the following loop executes? var counter = 0; var n = 6; while(n > 0){ n = n - 2; counter = counter + 1; } console.log(counter)

3

After running the following code segment, what is contained in the array data? Index: 1 2 3 4 5 data: 2 3 1 8 4 i <- 1 REPEAT UNTIL (i = LENGTH(data)) { data [i] <- data [i+1] i <- i+1; }

3 1 8 4 4

What will be displayed as a result of the code below executing? var a = 5; while (a < 5) { a = a - 1; } console.log(a);

5

Which of the following is FALSE about element IDs?

An element with a unique ID must always have an event handler associated with it.

A robot starts in a grid facing north (B3). What is the robot's location and direction after the following program is executed? data = ["F", "F", "R", "F", "L", "F", "R", "R", "F"] FOR EACH move IN data { IF ( move = "F" AND CAN_MOVE(forward) ) { MOVE_FORWARD() } ELSE IF (move = "R"){ rotate_right() } ELSE IF ( move = "L") { rotate_left() } } PICTURE A. PICTURE B. PICTURE C. PICTURE D. PICTURE E. PICTURE

B

A programmer wrote an essay for his history class, and realized he has confused the names of Benjamin Franklin and Alexander Graham Bell. Instead of going through the whole paper and changing the names, he used the following incorrect algorithm in an attempt replace every occurrence of "Benjamin Franklin" with "Alexander Graham Bell" and vise versa: - First, change all occurrences of "Benjamin Franklin" to "apple" - Then, change all occurrences of "apple" to "Alexander Graham Bell". - Then, change all occurrences of "Alexander Graham Bell" to "Benjamin Franklin". Here is an example of one of the sentences from the paper: Alexander Graham Bell was born 141 years before Benjamin Franklin, so he was never able to telephone his neighbors. Which of the following is the result of running the described incorrect algorithm on the sentence above?

Benjamin Franklin was born 141 years before Benjamin Franklin, so he was never able to telephone his neighbors.

Two students, Kaleb and Hunter, are arguing in class about an App Lab project. Kaleb states, "Huh, a button and an image are basically the same thing!". Hunter replies, "That doesn't make any sense at all!". What Kaleb may have meant by that statement.

Both are UI elements

Consider the code segment below: 1 var dist = 100; 2 var radius = dist/20; 3 penDown(); 4 for(var i=0; i < 4; i++){ 5 moveForward(dist); 6 dot(radius); 7 turnRight(90); 8 } Which of the following images is the most likely outcome of the drawing? A PICTURE B PICTURE C PICTURE D PICTURE

C

What is a possible output when the following code segment executes? The ending position of the turtle is shown in each diagram. The starting position is shown as a white triangle in cases where the turtle starts and ends in different locations. 1 var turn = 0; 2 for (var i = 0; i++) { 3 moveForward(); 4 if(turn == 0) { 5 turnLeft(); 6 turn = 1; 7 } 8 else{ 9 turnRight(); 10 trun = 0; 11 } 12 } A. PICTURE B. PICTURE C. PICTURE D. PICTURE E. PICTURE

C

Consider the code: onEvent("button1", "click", function() { moveForward(25); } ) ; what is the name of "button1"?

UI Element id

var word = prompt("Enter a word"); if( word.length > 10){ console.log("Wow, big word."); } else{ if( word.length < 3 ){ console.log("Did you say something? That was so short."); } else{ if( word.length > 6){ console.log("Use that word on the SAT."); } else{ console.log("I have never heard that before."); } } } If the above code segment is run, and "goodbye" is entered into the prompt, what will be displayed in the console?

Use that word on the SAT.

Which of the following is the COMMON MISTAKE in insert your images?

Using the URL of an image search web site rather than a URL of the image itself.

Which of the following is true about while loops in JavaScript?

While loops run as long as a given boolean condition is true.

What is the expected output of the given pseudo code segment? A reference for the pseudo code can be found immediately below the question age <- 35 IF (age < 35) { DISPLAY ("You are not old enough to be President.") } ELSE { DISPLAY ("You are old enough to be President!") } PICTURE

You are old enough to be President!

Why use global variables?

are useful for keeping track of data over the lifetime of the program that's running.

Why use local variable?

are useful temporary placeholders for data that you need to perform a computation of some kind.

All of the following are definition of local variables except

can be use outside of the function

In making descriptive and meaningful IDs there are some rules about IDs you need to know. All of the following is true except which one?

can contain spaces

All of the following are definition of global variables except

is not accessible by a function

What is the definition of Debugging?

is to check for bugs in your code to make sure the code run without error.

PICTURE A statistician would like to simulate rolling a single die until she has rolled the die a total of 100 times, or she rolls a six a total of 25 times, whichever comes first. Which boolean expression should she use for the <missing code> to accomplish this? var rolls = 0; var sixes = 0; while ( <missing code> ){ var nextRoll = randomNumber(1, 6); if(nextRoll == 6) { sixes = sixes + 1; } rolls = rolls + 1; } console.log(rolls); console.log(sixes);

rolls < 100 && sixes < 25

What is displayed by the console.log statement after the following code segment executes? 1 var a = 3; 2 var b = 6; 3 var c = 10; 4 a = b / a; 5 b = c - a; 6 c = b / a; 7 console.log("value is: "+c);

value is: 4

Jose is writing a reply function for a text messaging app. He'd like to swap the sender and receiver so that the value currently in variable From ends up as the value in To and To ends up in From From James Bond From Bruce Wayue To Bruce Wayne To James Bond Which of the following code segments will correctly swap the values as described?

var temp = from; from = to; to = temp;

A Boolean expression is an expression that evaluates to which of the of the following?

True/False

The following are describing Syntax errors except

Fix by retracing your steps, being humble, and trying to understand why the computer is interpreting what you wrote the way it is.

The following are Logic errors except

Fix these with careful reading of the lines that have errors

What is the output of this code segment? var phrase = "I am so"; var emotion = "excited"; var sentence = phrase + " " + emotion.toUpperCase() + "!"; console.log(sentence);

I am so EXCITED!

Using what you know about variables and re-assignment you should be able to trace this code and keep track of the values of a, b, and c as they change. You should also have a sense of any possible errors that could occur. Trace the code, and choose the option that correctly shows what will be displayed. 1 var a = 3; 2 var b = 7; 3 var c = 10; 4 a = a + b; 5 b = a + b; 6 c = a + b; 7 console.log("a:"+a+" b:"+b+" c:"+c);

a: 10 b: 17 c: 27

Consider the code segment below: var a = 0; var b = 3; var c = 4; a = a+c; b = a+c; c = a+c; What are the values of a, b, and c after this code segment has been run?

a=4 b=8 c=8

Consider the JavaScript code segment below. Which statement should be used in place of <missing code> such that the alarm is set to 9:00 am on weekends, and 6:30 am on weekdays. var day = prompt("What day is it tomorrow?"); if ( <missing code> ){ setAlarm = "9:00am"; } else { setAlarm = "6:30am"; }

(day == "Saturday") || (day == "Sunday")

Assume that a variable temperature is assigned like this: var temperature = 30; Choose the two (2) Boolean expressions that evaluate to FALSE.

- (temperature == 0) || (temperature > 32) - (temperature < 0) || (temperature > 32)

Study the code segment below to see what it will output. Values of x and y will be displayed multiple times during the running of the program, and you might recognize a pattern. From the list of possible outputs listed two (2) are IMPOSSIBLE. Choose the two (2) outputs that are impossible for this code to produce. x <- 0 REPEAT_UNTIL( x = 3 ){ y <- 0 x <- x+1 REPEAT_UNTIL ( y = 3 ){ y <- y+1 DISPLAY( x + ", " + y) } } NOTE: if the condition is TRUE the loop does not execute. For example if x is currently 3 then REPEAT_UNTIL (x=3) will not execute.

- 0,0 - 2,4

A programmer designed a program for an airline to determine whether there is an extra fee on a checked bag. The logic is shown in the flow chart at right. The code they wrote (see below) runs without error, but unfortunately it _does not_ work as intended. What is the problem? Consider the list of values below and choose two that if assigned to weight will result in the incorrect message being displayed as the response (choose 2)? PICTURE 1. var weight = promptNum("How much does your luggage weigh?"); 2. if (weight > 50){ 3. setText("response", "There is a 25 dollar fee to take this luggage"); 4. } else if (weight > 120){ 5. setText("response", "Your luggage is too heavy for this flight"); 6. } else { 7. setText("response", "Your luggage is accepted as is"); 8. }

- 125 - 200

Which of the following are actions a programmer could take when debugging a segment of code that would most likely lead to finding a problem and fixing it? (choose two)

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

Consider the following flow chart showing a process for program execution. Given that a = 5, and b = 10, what will be displayed by the program? PICTURE

10

Several questions below refer to arrays in JavaScript and pseudocode. The pseudocode syntax is very similar to JavaScript with the notable exception that in pseudocode the first index in an array is 1. In Javascript the first index is 0. PICTURE Given the following array, what will the code segment below display after being run? The initial state of the array is shown for you to use as a reference. Index: 1 2 3 4 5 6 data: 5 8 3 4 2 1 data = [5, 8, 3, 4, 2, 1] a <- data[5] b <- data[2] DISPLAY(a + b)

10

What is contained in the list data after the following code segment is run? Index: 1 2 3 4 5 data: 10 45 38 16 23 data <- [10, 45, 38, 16, 23] REMOVE(data, 2) REMOVE(data, 3)

10 38 23

What does the following code segment display? Index: 1 2 3 4 5 data: 3 5 8 2 1 data <- [3, 5, 8, 2, 1] REMOVE(data, 3) DISPLAY(data[3])

2

What value will be displayed after the loop has executed? var a = 5; while (a >= 3){ a = a - 1; } console.log(a);

2

The following program processes the list data shown below. After running the following program, what is contained in the list? Index: 1 2 3 4 data: 2 3 1 2 i = 1 val = 0 n = LENGTH(data) REPEAT n TIMES { val = val + data[i] data[i] = val i = i + 1 }

2 5 6 8

The following questions are about performing operations on lists using INSERT, REMOVE, and APPEND. The pseudocode documentation is here for your reference. PICTURE What is stored in list data after the following code segment is run? Index: 1 2 3 data: 0 1 2 data <- [0,1,2] INSERT(data, 1, 3) INSERT(data, 2, 4) INSERT(data, 3, 5) DISPLAY(data)

3 4 5 0 1 2

What is the output of the following JavaScript code segment? var str1 = "5"; var str2 = "5"; var result = str1 + str2; console.log(result);

55

What is the output of the following JavaScript code segment? var num = 5; var str = "hello"; var result = num + str; console.log(result);

5hello

What is the output of the code segment below? The initial state of the array is shown for you to use as a reference. Index: 1 2 3 4 5 6 data: 5 8 3 4 2 1 i <- 4 a <- data[i] b <- data[i + 1] DISPLAY(a + b)

6

What will be displayed as a result of the javascript code below executing? var a = 0; while( a != 5 ){ a = a+2; } console.log(a);

6

The following question asks you to reason about pseudocode involving loops, boolean operators, and the "robot instructions" from the AP Exam Reference guide. We've replicated the relevant sections below to use as reference. PICTURE PICTURE PICTURE A robot is represented as a triangle in a grid of squares. The initial position and direction of the robot is shown below. The robot can move onto a white square, but cannot move into a black region. PICTURE Consider the following program: REPEAT 4 TIMES { REPEAT UNTIL (NOT CAN_MOVE (forward)) { MOVE_FORWARD () } REPEAT UNTIL ( CAN_MOVE(forward) ) { ROTATE_LEFT () } } After running the code above, what will the ending location and direction of the robot be? A. PICTURE B. PICTURE C. PICTURE D. PICTURE E. PICTURE

A

Tiffany is writing a program to help manage a bake sale. She writes the following code which prompts the user to enter the total number of items a person is buying and then a loop repeatedly prompts for the cost of each item. She wrote the code but there is a problem: it runs in an infinite loop. How can Tiffany change her code so it doesn't loop forever? 0 var numItems = promptNum("How many items?"); 1 var total = 0; 2 while (numItems > 0){ 3 total = total + promptNum("Enter next item price"); 4 } 5 console.log("The total is" + total);

Add after line 3: numItems = numItems - 1;

What is the output to the console after the following code segment is executed? 1 fiveMore(); 2 3 function fiveMore(){ 4 var x = 5; 5 } 6 var y = 3 + x; 7 console.log(y);

Error. Unknown Identifier: x

Consider the code: onEvent("button1", "click", function() { moveForward(25); } ) ; what is the name of "click"?

Event name or type

Which of the following is FALSE about event-driven programs?

Event-driven programs do not implement algorithms.

str.length In JavaScript you can find the number of characters in String by using .length. For example: var name = "Victoria"; console.log(name.length); // will display 8 - the number of characters in "Victoria" For the next two questions, consider the following JavaScript code segment. var word = prompt("Enter a word"); if( word.length > 10){ console.log("Wow, big word."); } else{ if( word.length < 3 ){ console.log("Did you say something? That was so short."); } else{ if( word.length > 6){ console.log("Use that word on the SAT."); } else{ console.log("I have never heard that before."); } } } If the above code segment is run, and "hello" is entered at the prompt, what will be displayed in the console?

I have never heard that before.

A programmer created a list of all of her sibling's ages: 1 2 3 ages: 10 12 17 Unfortunately, she made a typo! Her middle sibling is 13 years old. Which of the following solutions will NOT fix the typo?

INSERT the value 13 at the index of 2

Which of the following best describes the result of running the procedure below? PROCEDURE mystery (a, b, c) { IF ( a >= b AND a >= c) { RETURN (a) } ELSE IF ( b >= a AND b >= c) { RETURN (b) } ELSE { RETURN (c) } }

It returns the largest of the three input values

A student decides to draw a series of three dots (sort of like a snowman) as shown in the diagram. She wants each dot to be half the radius of the previous dot, and for the center to be on the edge of the dot below it. She writes the following code segment to do it: PICTURE 1 var bottom = 100; 2 var middle = 50; 3 var top = 25; 4 5 dot(bottom); 6 moveForward(bottom); 7 8 dot(middle); 9 moveForward(middle); 10 11 dot(top); She is not sure about the size though, and wants to be able to quickly experiment with the drawing by changing only one number - the radius of the bottom dot - and for the rest of the code to size and scale the drawing accordingly. How should she adjust lines 2 and 3 of her code to implement this change?

Line 2: var middle = bottom/2; Line 3: var top = middle/2;

Choose the best description of what the mystery procedure below does. The procedure accepts two parameters: a list of a values and a number n. PROCEDURE mystery (list, n) { i = 1 REPEAT LENGTH(list) TIMES { IF ( list[i] = n ) { DISPLAY (i) } i = i+1 } }

Mystery displays the index of every occurrence of the value n in the list.

Consider the playGame procedure below which calls on 3 other procedures: countFives, player1Move, and player2Move. PROCEDURE playGame() { cards = [] REPEAT_UNTIL ( countFives(cards) >= 5 ) { card1 = player1Move() APPEND (cards, card1) card2 = player2Move() APPEND (cards, card2) } } The procedure above simulates a certain card game called "fives" - played with two decks of cards - in which each player takes a turn playing a card, until 5 fives have been played in total, at which point it's "Game Over." The procedure uses a list called cards which is initially empty. Each round of play, two cards are appended to the list. Here is the countFives procedure. PROCEDURE countFives(cards) { count = 0 FOR EACH card IN cards { IF( card = 5 ) { count = count+1 } } <MISSING CODE> } Which of the following should replace the <MISSING CODE> at line 12 to make the procedure work as designed?

RETURN (count)

Consider the code: onEvent("button1", "click", function() { moveForward(25); } ) ; what is the name of function()?

Start of callback function

Which of the following statements about strings in JavaScript is FALSE?

Strings with numerical digits in them are invalid.

PICTURE Consider the code segment below given in pseudo code (see reference above). Assuming that variables a, b and c already have numeric values assigned to them, what is the expected output of this code segment? val <- a IF(b >= a AND b >= c){ val <- b; } ELSE { IF (c >= a AND c >= b) { val <- c; } } DISPLAY (val);

The code will display the largest of the three values

Which of the following best describes the value returned by the procedure below? PROCEDURE mystery (data) { count = 0 i = 1 REPEAT UNTIL (i = LENGTH(data)) { IF (data[i] < data[i+1]) { count = count + 1 } i = i+1 } RETURN (count) } The answers below refer to values being in ascending and descending order. Ascending order means increasing, as in: [1, 2, 5, 8]. Descending order means decreasing, as in [20, 15, 7, 3].

The procedure returns the number of times adjacent items are in ascending order

Consider the following JavaScript code segment. var time = promptNum("What hour is it (on a 24 hour clock)?"); var greeting = ""; if (time < 6) { greeting = "It is too early!"; } else if (time < 20) { greeting = "Good Day!"; } else if (time < 10) { greeting = "Good Morning!"; } else { greeting = "Good Evening!"; } console.log(greeting); Something is wrong with the logic in the program above. For which values of time will the greeting "Good Morning!" be displayed?

There is no value of time that will result in "Good Morning!" being displayed

Consider the following segment given in pseudo code (see reference from previous question): IF (grade >= 70) { DISPLAY("You passed!") } ELSE { IF(grade <= 70){ DISPLAY("Time to start studying again!") } } What will be displayed if grade is set to 70?

You passed!

Jasmine is writing a shopping app. She has created a variable to keep track of the number of items in the shopping cart. Every time someone clicks the "addItemButton", she would like the variable to increase by 1. 1 var cartTotal = 0; 2 3 onEvent("addItemButton", "click", function() { 4 5 // <missing code> 6 7 console.log(cartTotal); 8 }); What code should Jasmine insert where it says <missing code> in order for her app to work?

cartTotal = cartTotal + 1;

Which of the following JavaScript statements will result in the following output being displayed to the console? Hello! How are you?

console.log("Hello! \nHow are you?");


संबंधित स्टडी सेट्स

Midterm Review (Only quizzes 1-6)

View Set

fundamentos administrativos final

View Set

Confidence in Introduction (ch.17)

View Set

PHYS, Stars & Galaxies, Chap. 24, Homework & Test Review, Prof. Kaim, DMC

View Set

The Progressive Era - Thompson APUSH Unit 15

View Set

MADM 760 Exam 2 Chapter 3 and Chapter 4

View Set