Basic Data Structures Test

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is printed by the following program? function printNumbers(two, one, zero){ println(two); println(one); println(zero); } function start(){ var zero = 0; var one = 1; var two = 2; printNumbers(zero, one, two); }

0 1 2

What is the output of the following program: var groceries = ["bread", "bananas", "apples", "cheese", "peanuts"]; println(groceries.indexOf("bananas"));

1

Which of the following statements are true about simulation models? I - Models often omit unnecessary features of the phenomena being modeled. II - The time it takes for a simulation to run increases as the underlying model becomes more complex.

1 and 2

Which of the following functions will correctly return true if it is passed an odd integer value for x? I. function IsOdd (x) { return (x % 2 == 1); } II. function IsOdd (x) { return (x / 2 == 1); } III. function IsOdd (x) { if (x % 2 == 1) return true; else return false; }

1 and 3 only

Consider the following code segment. The indices on the exam are different. They do not start at 0, but rather at 1. aList ← ["cat", true, "hello", 78, "Karel"] Which of the following will throw an error? I. aList[0] II. aList[2] III. aList[3] IV. aList[6]

1 and 4

Which of the following is true for an API? I. Allows different devices to connect with each other II. Allows for Interactivity between devices and software III. Messenger program that takes requests and delivers the response back to the user IV. Allows the creation of applications that access the features or data of an operating system, application, or other services V. Specifies how software components should interact VI. Used when programming graphical user interface (GUI) components

1, 2, 3, 4, 5, and 6

Why do we write functions? I. Make our code easier to understand by giving a readable name to a group of instructions II. Avoid writing repeated code III. Make our code reusable

1, 2, and 3

Consider the following code snippet. What is returned and printed for the value of a? function start(){ var a = mystery(10, 14); var b = mystery(82, 28); println(a); println(b); } function mystery(x, y) { if (x < y){ return x; }else{ return y; } }

10

Consider the following code snippet: function mystery1(x){ var result = x + 1; return result; } function mystery2(x, y){ var result = x + y; return result; } function mystery3(x){ x = mystery1(x); var result = x * x; return result; } What would the following print? var y = mystery3(11); println(y);

144

What is the output of the following program? function start(){ var x = 5; sumTo(x); println(x); } function sumTo(num){ var sum = 0; for(var i = 0; i <= num; i++){ sum += i; } println(sum); }

15 5

Consider the following code snippet: function start(){ drawCircle(40, Color.red, 100, 300); drawCircle(50, Color.green, 50, 100); drawCircle(70, Color.blue, getWidth()/2, getHeight()/2); drawCircle(25, Color.yellow, getWidth()/2, 300); } function drawCircle(radius, color, x, y){ var circle = new Circle(radius); circle.setColor(color); circle.setPosition(x, y); add(circle); } In the third call to drawCircle(), what values are passed to the coordinates of the center of the circle, assuming the screen width is 400 and the height is 300?

200, 150

How many parameters go into the function sum, and how many return values come out of the function sum? function sum(first, second, third){ var result = first + second + third; println(first); println(second); println(third); return result; }

3 parameters go in, 1 return value comes out

Consider the code segment. What will be the output? function start(){ var arr = []; arr.push(5); arr[1] = 12; println(arr[0]); arr.push("hello"); arr.push(true); arr.push(1000); arr.push(3.2); println(arr[3]); var last = arr.pop(); println(last); println(arr[4]); }

5 true 3.2 1000

What is printed by the following program? function product(x, y){ return x * y; } function difference(x, y){ return x - y; } function start(){ var x = 2; var y = 5; var value1 = product(x, y); var value2 = difference(y, x); var result = difference(value1, value2); println(result); }

7

What is an array (or list)?

An ordered collection of items

What kinds of elements can you store in data structures?

Any kind of objects (anything you can store in a variable)

What does API stand for in computer science?

Application Programming Interface

True or false: Functions must have parameters.

False

The AP exam uses the notation [index1, index2, index3...] to create a list with those values as the first, second, third, and so on elements. The indices on the exam are different. They do not start at 0, but rather at 1. Consider the following code segment. arrList ← [17, "Karel", false, true, "hello"] Which element can be found at index 2?

Karel

What does the following function mystery do? function mystery(list){ var result = []; while(list.length > 0){ var elem = list.pop(); result.push(elem); } return result; }

Returns a new list containing the elements of the list list in reverse order, and leaves list empty

What is the purpose of a return statement in a function?

Stops executing the function and returns the value

In the following code: var size = 20; var x = 100; var y = 200; var ball = new Circle(size); ball.setPosition(x, y); JavaScript What is the meaning of the x variable?

The x coordinate of the center of the circle

The AP Exam uses the following format to iterate through a list. FOR EACH item IN aList { <block of statements> } Given the following code segment, how can you best describe its behavior? result ← 0 FOR EACH n IN list { IF (n MOD 2 = 1) { result ← result + n } } DISPLAY result

This code displays the sum of the odd numbers in list

The AP Exam uses the following format to add an item to list and to find the length of a list. APPEND(aList, value) LENGTH(aList) Given the following code segment, how can you best describe its behavior? You can assume number is a positive integer and isComposite(x) returns true if x is composite (not prime) and false otherwise. list ← [] i ← 1 REPEAT number TIMES { IF (isComposite(i)) { APPEND(list, i) } i ← i + 1 } DISPLAY LENGTH(list)

This code displays the total number of composite numbers between 1 and number

The AP Exam uses the following format to insert an item to a list and to generate a random integer from a to b, including a and b. INSERT(aList, i, value) RANDOM(a, b) Given the following code segment, how can you best describe its behavior? i ← 1 FOR EACH x IN list { REMOVE(list, i) random ← RANDOM(1, LENGTH(list)) INSERT(list, random, x) i ← i + 1 }

This code shuffles the order of the numbers in the list by removing them and inserting them back in random places.

The AP Exam uses the following format to remove an item from a list and to access the element at a specific index in a list. REMOVE(aList, i) aList[i] Given the following Mystery procedure, how can you best describe the result it returns? PROCEDURE Mystery (list, target) { length ← LENGTH(list) i ← 1 REPEAT length TIMES { IF (list[i] = target) { REMOVE(list, i) } ELSE { i ← i + 1 } } RETURN(list) }

This procedure returns the list without any instance of the given target

The following program should draw a circle on the screen 1 function start(){ 2 var circle = new Circle(40); 3 circle.setPosition(getWidth() / 2, getHeight() / 2); 4 circle.setColor(Color.blue); 5 } JavaScript But when we run this code, we don't see the circle. What is missing from our start function?

We create the circle and position it correctly on the screen, but we never add it to the screen. We need to add the line add(circle); after line 4

A group of scientists has developed a simulation that simulates traffic in a city based on several factors. Which of the following statements is most likely something that can be learned from this simulation?

Which roads are most likely to need maintenance in the near future

What is the output of the following program? var arr = [1, 2, 3, 4, 5]; var removed = arr.remove(2); println(arr); println(removed);

[1, 2, 4, 5] 3

What is the output of the following program? function start(){ var arr = [12, 17, 65, 7, 30, 88]; var elem = arr.pop(); println(arr); println(elem); }

[12, 17, 65, 7, 30] 88

Consider the following code segment. bList ← [10, 30, 50] aList ← [20, 40, 60] bList ← [15, 45, 90] aList ← bList What is stored in aList?

[15, 45, 90]

Suppose we have a list groceryList shown below: var groceryList = ["milk", "bread", "eggs", "sugar", "carrots", "apples"]; We want to write a function addGrocery that will take a grocery list and an item as a parameter and add the item on to the end of the grocery list. For example: println(groceryList); addGrocery(groceryList, "potatoes"); println(groceryList); Should print out ["milk", "bread", "eggs", "sugar", "carrots", "apples"] ["milk", "bread", "eggs", "sugar", "carrots", "apples", "potatoes"] Which of the following is the correct implementation of addGrocery?

function addGrocery(groceryList, item){ groceryList.push(item); }

"It's a bird! It's a plane! No, it's Superman!" We want to write a function is Superman that takes in two parameters is Bird and is Plane and returns true if it is in fact Superman, and false otherwise. If it's not a bird and it's not a plane, it must be Superman.Which of the following functions is the correct implementation of is Superman?

function isSuperman(isBird, isPlane){ return !isBird && !isPlane; }

Which of the following is the correct way to create a function in JavaScript?

function myFunction()

We want to be able to read our grocery list in a readable format while we're shopping. We decide to write a function printGroceries such that the following code: var groceryList = ["milk", "bread", "eggs", "sugar", "carrots", "apples"]; printGroceries(groceryList); Should print out 1. milk 2. bread 3. eggs 4. sugar 5. carrots 6. apples printGroceries should not modify the groceryList array at all, the array should be in the same state after being printed as it was before it was printed. Which of the following is the best implementation of printGroceries?

function printGroceries(groceryList){ for(var i = 0; i < groceryList.length; i++){ println((i + 1) + ". " + groceryList[i]); }

Suppose we have a list groceryList shown below: var groceryList = ["milk", "bread", "eggs", "sugar", "carrots", "apples"]; Once we buy a certain item, we should remove it from the list to show that we bought it. We want to write a function removeGrocery that will take an item as a parameter and remove it from our grocery list. For example: println(groceryList); removeGrocery(groceryList, "bread"); println(groceryList); Should print out ["milk", "bread", "eggs", "sugar", "carrots", "apples"] ["milk", "eggs", "sugar", "carrots", "apples"] Which of the following is the best implementation of removeGrocery?

function removeGrocery(groceryList, item){ var index = groceryList.indexOf(item); if(index != -1){ groceryList.remove(index); } }

Which of the following type of variable is visible everywhere in your code?

global

In the following array: var groceries = ["milk", "eggs", "cookies", "cake"]; Which of the following statements will change "cookies" to "bread"?

groceries[2] = "bread";

What method can we use to find the index of a particular element in an array?

indexOf

var numbers = [1, 1, 2, 3, 5]; How can we remove the last item from the end of the numbers array and store it in a variable called value?

var value = numbers.pop();

We want to position a Circle circle on our canvas to be at a random position, but we want to keep the entire shape on the screen. Which of the following will accomplish this?

var x = Randomizer.nextInt(circle.getRadius(), getWidth() - circle.getRadius()); var y = Randomizer.nextInt(circle.getRadius(), getHeight() - circle.getRadius()); circle.setPosition(x, y);

In the following function printThreeTimes: function printThreeTimes(word){ println(word); println(word); println(word); } What is the parameter of the function?

word

Consider the code snippet: function drawCircle(radius, color, x, y){ var circle = new Circle(radius); circle.setColor(color); circle.setPosition(x, y); add(circle); } What is the return value for this function?

this function has no return value

How can we get the length of an array arr?

arr.length

Which of the following will create an empty list and assign it to arrList?

arrList ← [ ]

Consider the code snippet: function start(){ printNumbers(12, 17, 65); printNumbers(1, 2, 3); printNumbers(3, 3, 20); } function printNumbers(first, second, third){ println(first); println(second); println(third); } What are the parameters of the printNumbers method?

first, second, third

var numbers = [1, 1, 2, 3, 5]; How can we add an 8 to the end of numbers?

numbers.push(8);

In the following code: var MAX_NUMBER = 10; function sum(x, y){ var result = x + y; //Point A return result; } function start(){ var num1 = Randomizer.nextInt(0, MAX_NUMBER); var num2 = Randomizer.nextInt(0, MAX_NUMBER); println( sum(num1, num2) ); } Which variables exist at point A? In other words, which variables are in scope at point A? Which variables could you type at point A and the program would still work?

result, x, y, and MAX_NUMBER

Which statement allows us to return values from functions?

return

What does the mystery function do in the code snippet below? function start(){ var a = mystery(10, 14); var b = mystery(82, 28); println(a); println(b); } function mystery(x, y) { if (x < y){ return x; }else{ return y; } }

returns the smaller of two values passed to it


Kaugnay na mga set ng pag-aaral

Perry/Hockenberry chapter 12. High Risk Perinatal Care: Gestational Conditions

View Set

Appendicitis NCLEX, Postoperative Care, NCLEX pre op/post op

View Set

Psychological Diorders/ Therapy Study Guide

View Set

fundamentals of life insurance and annuities

View Set

Chapter 9 - Cellular Respiration and Fermentation

View Set

ELA 2 Honors; How It Feels to Be Colored Me

View Set

LES 305: Week 14: Intellectual Property (Exam 4)

View Set