AP COMP SCI UNIT 7

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

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

1

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

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.

I and II

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]

I and IV

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

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

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]

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 ← [ ]

Suppose we have a list groceryList shown below: var groceryList = ["milk", "bread", "eggs", "sugar", "carrots", "apples"]; JavaScript 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); JavaScript Should print out ["milk", "bread", "eggs", "sugar", "carrots", "apples"] ["milk", "bread", "eggs", "sugar", "carrots", "apples", "potatoes"] JavaScript Which of the following is the correct implementation of addGrocery?

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

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); JavaScript Should print out 1. milk 2. bread 3. eggs 4. sugar 5. carrots 6. apples JavaScript 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"]; JavaScript 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); JavaScript 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); } }

In the following array: var groceries = ["milk", "eggs", "cookies", "cake"]; JavaScript 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]; JavaScript How can we add an 8 to the end of numbers?

numbers.push(8);

var numbers = [1, 1, 2, 3, 5]; JavaScript 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();


Kaugnay na mga set ng pag-aaral

Psychopharmacology Quiz 7 (Psychotic Disorders and Antipsychotics)

View Set

Personal Finance Exam 2 Chapter 9

View Set

Pearson Educational Research Mills and Gay Final Exam

View Set

Life Insurance Chapter 3 Life insurance Policies

View Set

Active Learning - Reproductive System

View Set

Oral Care Case Study and Quiz ATI

View Set

Posterior Pituitary Gland Disorders

View Set