introduction to computer science final exam...

23
Introduction to Computer Science Final Exam Review 1. Which of the following commands is a valid Karel command? a. move b. move; c. move(); d. move() 2. What makes the following command an invalid Karel command? turnleft(); a. It should end in a colon rather than a semicolon b. The l should be a capital L c. It should start with a capital T d. This command is correct 3. Which of the following is the correct way to define a turnRight function in Karel? a. function turnRight() { turnLeft(); turnLeft(); turnLeft(); } b. function turnRight() { turnRight(); turnRight(); turnRight(); } c. function turnRight { turnLeft(); turnLeft(); turnLeft(); } d. turnRight function() { turnLeft(); turnLeft(); turnLeft(); turnLeft(); } 4. Why do we use functions in Karel programs? a. Break down our program into smaller parts b. Avoid repeating code c. Make our program more readable d. All of the above

Upload: others

Post on 27-Apr-2020

52 views

Category:

Documents


0 download

TRANSCRIPT

Introduction to Computer Science Final Exam Review

1. Which of the following commands is a valid Karel command? a. move b. move; c. move(); d. move()

2. What makes the following command an invalid Karel command?

turnleft(); a. It should end in a colon rather than a semicolon b. The l should be a capital L c. It should start with a capital T d. This command is correct

3. Which of the following is the correct way to define a turnRight function in Karel?

a. function turnRight() { turnLeft(); turnLeft(); turnLeft();

} b. function turnRight() {

turnRight(); turnRight(); turnRight();

} c. function turnRight {

turnLeft(); turnLeft(); turnLeft();

} d. turnRight function() {

turnLeft(); turnLeft();

turnLeft(); turnLeft();

}

4. Why do we use functions in Karel programs? a. Break down our program into smaller parts b. Avoid repeating code c. Make our program more readable d. All of the above

5. In this code, how many times is the dance function called and how many times is it defined? function start() { move(); dance(); move(); move(); turnLeft(); dance(); dance(); } function dance() { turnLeft(); move(); turnLeft(); turnLeft(); move(); turnLeft(); }

a. Called 1 time, defined 1 time b. Called 1 time, defined 3 times c. Called 3 times, defined 1 time d. Called 3 times, defined 3 times

6. What’s wrong with this code?

function start() { move(); go(); go(); } function go() { move(); move(); } function go() { move(); move(); }

a. The go function is called twice b. The go function has a syntax error c. The go function has been defined twice d. go is not a command that Karel understands

7. How many total times will Karel move in this program? function start() { move(); for (var i = 0; i < 5; i++) { move(); putBall(); } }

a. 1 b. 5 c. 6 d. 7

8. What is wrong with this for loop?

for (var i = 0, i < 10, i + 1) { move(); } I. The for loop uses commas instead of semicolons II. It should say i++ instead of i + 1

a. I b. II c. I and II d. The for loop is correct

9. What is the proper format for a single line comment in Karel?

a. This is a comment b. // This is a comment c. /* This is a comment d. This is a comment

10. What does the mystery function do?

function mystery() { while (noBallsPresent()) { move(); }

}

a. Karel moves until it is on a ball b. Karel moves once if there is no ball present c. Karel moves until it puts down a ball d. Karel checks if there is no ball on the current spot and then moves once

11. Which of the following is not a valid condition to go in an if statement for Karel?

a. ballsPresent() b. frontIsClear() c. leftIsBlocked() d. turnLeft()

12. Say Karel is on a location with one tennis ball. After the following code runs, how many tennis balls will there be at that location? for (var i = 0; i < 3; i++) { if (ballsPresent()) { takeBall(); } else { putBall(); putBall(); } }

a. 0 b. 1 c. 2 d. 6

13. What condition should be used in this while loop to get Karel to pick up all the tennis balls on the current

location? while (________) { takeBall(); }

a. noBallsPresent() b. ballsPresent() c. frontIsClear() d. takeBall()

14. Why does a programmer indent their code?

a. Helps show the structure of the code. b. Easier for other people to understand. c. A key part of good programming style! d. All of the above

15. How can we teach Karel new commands?

a. For loop b. While loop c. Define a new function d. The start function

16. Say you want to write a program to have Karel put down 300 tennis balls. Which control structure would you

use? a. If statement b. While loop c. For loop d. Nested while loop

17. Karel starts at Street 1, Avenue 1, facing East in a 5x5 world. What will happen after this code runs? move(); putball(); move(); move(); move(); move(); move();

a. Karel will end on Street 1, Avenue 2 b. Karel will end on Street 1, Avenue 7 c. This code won’t run because of a syntax error d. Karel will crash into a wall

18. What is top down design?

a. Top down design is a way of designing your program by starting with the biggest problem and breaking it down into smaller and smaller pieces that are easier to solve.

b. Top down design is a way that you can create designs on a computer to put on a web page c. Top down design is a way of designing your programs starting with the individual commands first d. Top down design is a way to use loops and if statements to decompose the problem

19. How can we improve the following program?

function start() { move(); move(); move(); move(); move(); move(); move(); move(); move(); }

a. Break down this program into more functions b. Use a for loop to repeat the move command c. Use a while loop to repeat the move command d. Fix the indentation of this program

20. In the following code below from the Cleanup Karel example, what is the purpose of If Statement #2? // This program has Karel walk down the // row and clean up all of the tennis balls // on the way. function start() { while (frontIsClear()) { // If statement #1 if (ballsPresent()) { takeBall(); } move(); } // If statement #2 if (ballsPresent()) { takeBall(); } }

a. To move the last time b. To pick up the ball that is in the last spot c. To pick up the ball that is in the last spot, if there is one d. To take the ball from all of the positions that have a ball on them

21. In the following code, what would be a good Postcondition to write?

/* Precondition: Karel is on a spot with a tennis ball facing East * Postcondition: ... */ function getOnTop() { turnLeft(); move(); turnRight(); }

a. Karel is on a spot with a tennis ball facing north b. Karel ends one spot above a tennis ball facing East. c. Karel is on the same position but on top of a ball. d. Karel is facing East.

22. What is the purpose of using a for loop in code?

a. To do something if a condition is true b. To do something while a condition is true c. To repeat something a fixed number of times d. To make programs run faster

23. A store has 20 apples in its inventory. How can you store this information in a JavaScript variable?

a. var numApples == 20; b. 20 = numApples; c. var numApples = 20; d. var num apples = 20;

24. You want to read input from the user to know how many apples they would like to buy. Which statement should

you use to read in a number from the user? a. var applesToBuy = readLine("How many apples would you like? "); b. var applesToBuy = println("How many apples would you like? "); c. var applesToBuy = readInt("How many apples would you like? "); d. var applesToBuy = nextInt("How many apples would you like? ");

25. You are splitting up all of your apples equally between 3 people. Which statement below will calculate how

many apples will be left over? a. var leftOver = numApples / 3; b. var leftOver = 3 / numApples; c. var leftOver = numApples % 3; d. var leftOver = 3 % numApples;

26. In a graphics canvas, what are the coordinates of the bottom right corner of the window?

a. 0, 0 b. 0, getWidth() c. getHeight(), getWidth() d. getWidth(), getHeight()

27. What are the coordinates of the center of the window?

a. getWidth(), getHeight() b. getWidth() / 2, getHeight() / 2 c. getHeight() - getWidth(), getWidth() - getHeight() d. getHeight() / 2, getWidth() / 2

28. In the following code:

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

a. The x coordinate of the center of the circle b. The x coordinate of the left edge of the circle c. The radius of the circle d. The position of the circle

29. 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 } But when we run this code, we don’t see the circle. What is missing from our start function?

a. We never set the circle’s radius. We need to add the line circle.setRadius(40); After line 4

b. We are setting the position of the ball to be off the canvas. We need to change line 3 to be circle.setPosition(0, 0);

c. We are creating the circle incorrectly. We need to specify the width and height. We need to change line 2 to be var circle = new Circle(40, 40);

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

30. What is the value of the boolean variable canVote at the end of this program?

var age = 17; var isCitizen = true; var canVote = age >= 18 && isCitizen;

a. true b. false c. yes d. none, there is a syntax error in this code

31. What is printed by the following program?

var isRaining = false; var isCloudy = false; var isSunny = !isRaining && !isCloudy; var isSummer = false; var isWarm = isSunny || isSummer; println("Is it warm: " + isWarm);

a. Is it warm: true b. Is it warm: false c. Is it warm: yes d. Is it warm: no

32. What is printed by the following program? var numApples = 10; var numOranges = 5; if(numApples < 20 || numOranges == numApples){ println("Hello, we are open!"); } else { println("Sorry, we are closed!"); } println("Sincerely, the grocery store");

a. Hello, we are open! Sincerely, the grocery store

b. Sorry, we are closed! Sincerely, the grocery store

c. Hello, we are open! d. Sorry, we are closed! e. Sincerely, the grocery store

33. We want to print the phrase “CodeHS is the best” exactly 25 times. What kind of control structure should we

use? a. if statement b. while loop c. break statement d. for loop

34. What is the output of the following program?

var result = 0; var max = 5; for(var i = 0; i < max; i++){ result += i; } println(result);

a. 15 b. 10 c. 0 d. 12345

35. What is the last thing printed by the following program? var start = 30; var stop = 10; for(var i = start; i >= stop; i-=5){ if(i % 2 == 0){ println(i * 2); } else { println(i); } }

a. 10 b. 20 c. 30 d. 60

36. 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? a. var x = Randomizer.nextInt(0, getWidth() - circle.getRadius());

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

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

c. var x = Randomizer.nextInt(0, getWidth()); var y = Randomizer.nextInt(0, getHeight()); circle.setPosition(x, y);

d. var x = Randomizer.nextX(); var y = Randomizer.nextY(); circle.setPosition(x, y);

37. We want to simulate constantly flipping a coin until we get 3 heads in a row. What kind of loop should we use?

a. for loop b. while loop c. loop and a half d. infinite loop

38. How many times will the following program print "hello"?

var i = 0; while(i < 10){ println("hello"); }

a. 10 b. 11 c. 9 d. This code will loop infinitely

39. The following code continually asks the user for a password until they guess the correct password, then ends. But there is one problem. 1 var SECRET_PASSWORD = "karel"; 2 3 function start(){ 4 while(true){ 5 var password = readLine("Enter your password: "); 6 if(password == SECRET_PASSWORD){ 7 println("You got it!"); 8 } 9 println("Incorrect password, please try again."); 10 } 11 } Which of the following will fix this program?

a. Change the true in line 4 to be password != SECRET_PASSWORD so that the program doesn’t loop infinitely

b. Change the true in line 4 to be password == SECRET_PASSWORD so that the program doesn’t loop infinitely

c. Add a break; statement after line 7 so that the program doesn’t loop infinitely d. Put line 9 inside of an else statement

40. Why do we write functions?

a. Make our code easier to understand by giving a readable name to a group of instructions b. Avoid writing repeated code c. Make our code reusable d. All of the above e. None of the above

41. In the following function printThreeTimes:

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

a. printThreeTimes b. function c. println d. word

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

a. 5 15

b. 15 5

c. 5 d. 15 e. none, there is a syntax error

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

a. 2 1 0

b. 0 1 2

c. zero one two

d. two one zero

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

a. 3 parameters go in, 1 return value comes out b. 3 parameters go in, 3 return values come out c. 1 parameter goes in, 4 return values come out d. 1 parameter goes in, 1 return value comes out

45. “It’s a bird! It’s a plane! No, it’s Superman!”

We want to write a function isSuperman that takes in two parameters isBird and isPlane 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 isSuperman?

a. function isSuperman(isBird, isPlane){

return isBird || isPlane; }

b. function isSuperman(isBird, isPlane){ return !isBird || !isPlane;

} c. function isSuperman(isBird, isPlane){

return !isBird && !isPlane; }

d. function isSuperman(isBird, isPlane){ return isBird && isPlane;

}

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

a. 7 b. -7 c. 13 d. -13

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

a. result only b. result, x, and y c. result, x, y, and MAX_NUMBER d. result, num1, num2, and MAX_NUMBER e. result, x, y, num1, num2, and MAX_NUMBER

48. What does this program do? var ball; function start(){ ball = new Circle(40); add(ball); setTimer(draw, 20); } function draw(){ ball.move(2, 2); }

a. Animates a ball by moving it down and to the right once every 20 milliseconds. b. Animates a ball by moving it up and to the right once every 20 milliseconds c. Animates a ball by moving it down and to the right every 20 seconds d. Animates a ball by moving it up and to the right once every 20 seconds

49. In the following code:

var ball; function start(){ ball = new Circle(40); add(ball); setTimer(draw, 20); } function draw(){ ball.move(2, 2); } Which of the following statements are true about ball?

I – ball is a local variable II – the ball variable in draw is different from the ball variable in start III – ball is a global variable IV – ball’s scope includes both start and draw

a. III and IV b. I and II c. III only d. I, II, III, and IV

50. The following program animates a ball by setting a timer to move the ball across the screen. We want the animation to stop whenever the user clicks the mouse: var ball; function start(){ ball = new Circle(40); add(ball); setTimer(draw, 20); mouseClickMethod(stopAnimation); } function draw(){ ball.move(2, 2); } function stopAnimation(e){ //your code here ... } Which statement would stop the animation in this program?

a. stopTimer(ball.move(2, 2)); b. stopTimer(draw); c. stopTimer(draw()); d. stopTimer(draw, 20);

51. We extend our draw function to include:

1 var ball; 2 var moveCounter = 0; 3 function draw(){ 4 5 ball.move(2, 2); 6 moveCounter++; 7 8 if(moveCounter == 25){ 9 stopTimer(draw); 10 } 11 } 12 13 function start(){ 14 ball = new Circle(40); 15 add(ball); 14 setTimer(draw, 20); 15 } How many times will the ball be moved before the animation stops?

a. 20 b. 40 c. 2 d. 25

52. How many circles of radius 10 will fit vertically stacked in a window that has height 100? The circles should not overlap, the bottom of one circle should touch the top of the next circle.

a. 10 b. 20 c. 5 d. 100

53. How can we determine if ball’s left edge is hitting a different shape on our canvas named box?

a. if(ball == box){

//ball's left edge is hitting box }

b. var elem = getElementAt(ball.getX() - ball.getRadius() - 1, ball.getY()); if(elem == box){ //ball's left edge is hitting box

}

c. var elem = getElementAt(ball.getX() - ball.getRadius() - 1, ball.getY()); if(elem != null){

//ball's left edge is hitting box }

d. if(ball.getX() - ball.getRadius() - 1 == box){

//ball's left edge is hitting box }

54. Which of the following statements will call the function paint every time the mouse is moved?

a. mouseMoveMethod(paint, e); b. mouseMoveMethod(paint); c. mouseMoveMethod(paint()); d. mouseMoveMethod(paint(e));

55. What is a callback function?

a. A function called at the bottom of the start function b. A function passed to an event handler that is called every time a certain event happens c. A function named callback d. A function that is called every DELAY milliseconds

56. In the following statement:

mouseMoveMethod(draw); Which part is the event handler?

a. mouseMoveMethod b. draw c. e d. none

57. In the following program: var STEP = 5; var SQUARE_SIZE = 20; var square; function start(){ square = new Rectangle(SQUARE_SIZE, SQUARE_SIZE); square.setPosition(getWidth() / 2, getHeight() / 2); keyDownMethod(down); } function down(e){ if(e.keyCode == Keyboard.LEFT){ square.move(-STEP, 0); } else if(e.keyCode == Keyboard.RIGHT){ square.move(STEP, 0); } else if(e.keyCode == Keyboard.letter('R')){ square.move(0, -STEP); } else if(e.keyCode == Keyboard.letter('F')){ square.move(0, STEP); } else{ square.setColor(Randomizer.nextColor()); } } How can a user move the square up on the screen?

a. Pressing down the ‘F’ key b. Pressing down the UP arrow key c. Pressing down the ‘R’ key d. Pressing down the LEFT arrow key

58. Which of the following are good examples of things that should be stored as global variables?

I – The parameter e to a callback function II – The ball in the game the user is playing III – A counter keeping track of how many times the user has pressed the spacebar IV – A for loop counter V – The color of a rectangle that is only used in one function

a. II, III, and V b. I, IV, and V c. II and III d. I, II, and III

59. What function do you need to call to ask the user of the program to enter text? a. readLine b. readln c. text d. println

60. To ask the user of the program for a number, which function should you use?

a. readLine b. readInt c. readFloat d. Both readInt and readFloat are for numbers.

61. What will the following code print to the screen?

println(2 + 2);

a. 2 + 2 b. 4 c. 22 d. Nothing

62. What symbol do you use to do multiplication in JavaScript?

a. x b. X c. * d. #

63. What function do you need to call to get the width of the screen?

a. width() b. getWidth() c. screenWidth() d. getScreenWidth()

64. In the following code, we create a circle and add it to the screen. What is the meaning of the x variable?

var x = 20; var circle = new Circle(x); add(circle);

a. The color of the circle b. The diameter of the circle c. The radius of the circle d. The position of the circle

65. What are the coordinates of the top right corner of the screen?

a. 0, 0 b. 0, getWidth() c. getWidth(), 0 d. getWidth(), getHeight()

66. Which of the following is not a valid value for a boolean? a. true b. false c. yes d. The above are all valid

67. What symbol represents the and operator in JavaScript?

a. AND b. and c. & d. &&

68. What symbol represents the or operator in JavaScript?

a. OR b. or c. | d. ||

69. After the following code runs, what is the value of isWeekend?

var isSaturday = true; var isSunday = false; var isWeekend = isSaturday || isSunday;

a. true b. false c. "isWeekend" d. "isSaturday || isSunday"

70. What is the proper way to compare if two values are equal in a boolean expression in JavaScript?

a. = b. == c. EQUALS d. is

71. What is the proper operator for “not equals” in JavaScript?

a. =! b. != c. ~= d. NOT EQUALS

72. What kind of statement allows us to run a block code only if a certain condition is true?

a. if statement b. break statement c. else statement d. for loop

73. What kind of statement allows us to run one block of code if one condition is true, and a separate block of code

otherwise? a. if statement b. break statement c. if/else statement d. for loop

74. How can we check if a variable num is even? a. num.isEven() will be true b. (num % 2 == 0) will be true c. (num % 2 == 0) will be false d. (num / 2 == 0) will be true

75. In the following code, what will be the last number to print to the screen before the program finishes? for (var i = 0; i < 10; i++) { if (i % 2 == 0) { println(i); } else { println(2 * i); } }

a. 10 b. 20 c. 9 d. 18

76. How many times will the following code print "hello"? for (var i = 3; i < 8; i++) { println("hello"); }

a. 3 b. 5 c. 6 d. 8

77. How many times will the following code print "hello"?

for (var i = 0; i < 8; i += 2) { println("hello"); }

a. 0 b. 2 c. 4 d. 8

78. Which of the following returns a random number between 1 and 10?

a. randomInt(1, 10) b. randInt(1, 10) c. Randomizer.int(1, 10) d. Randomizer.nextInt(1, 10)

79. How many possible values can Randomizer.nextBoolean() return?

a. 0 b. 1 c. 2 d. 3

80. How many times will this program print "hello"? var i = 50; while (i < 100) { println("hello"); }

a. 0 b. 50 c. 100 d. This code will loop infinitely

81. What statement can we use inside of a loop to end it?

a. println b. break c. for d. else

82. What is the term for the value that signals the end of user input?

a. sentinel b. break c. -1 d. done

83. What is printed when the following code is run?

function doubleNumber(x){ return 2*x; } function start(){ var y = 4; var doubledY = doubleNumber(y); var result = doubleNumber(doubledY); print(result); }

a. 4 b. 8 c. 16 d. 64

84. How many parameters go into the function sum?

function sum(first, second){ var result = first + second; return result; }

a. 1 b. 0 c. 3 d. 2

85. How many return values come out of the function sum? function sum(first, second){ var result = first + second; return result; }

a. 0 b. 1 c. 2 d. 3