Computer Science Unit 1

Ace your homework & exams now with Quizwiz!

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

// This is a comment

How many times should the start function be called in a program?

1

How many times should the start function be defined in a program?

1

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(); } }

1

How many times should Karel turn left in order to turn right?

3

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

6

What is an avenue in a Karel world?

A column

What is a street in a Karel world?

A row

What is a code comment?

A way to give notes to the reader to explain what your code is doing

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

For loop

You need to write a program that has Karel move 6 times and then put a ball. Which control structure do you need to use?

For loop

What can be used to teach Karel to turn right?

Functions

Why should a programmer indent their code?

Helps show the structure of the code Easier for other people to understand Indenting is a key part of good programming style All of the above

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

I and II

Which of the below are examples of control structures? (I) if (II) if/else (III) while (IV) for

I, II, III, and IV

You need to write a program that has Karel move if the front is clear, but if it isn't clear, Karel will do nothing. Which control structure do you need to use?

If Statement

You need to write a program that has Karel put down a tennis ball if the world has dimensions of 1x1. Which control structure do you need to use?

If Statement

You need to write a program where Karel will take a ball if there is a ball present, otherwise Karel should put down a ball. Which control structure do you need to use?

If/Else statement

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(); }

Karel ends one spot above a tennis ball facing East.

What do we use control structures for in JavaScript?

Control the flow of the program; how the commands execute.

What does an if/else statement look like in JavaScript? A if (condition) { //code } B for (var i = 0; i < count; i++) { //code } C if (condition) { //code } if (condition) { //code } D if (condition) { //code } else { //code }

D

Which general while loop definition is written correctly? A while (x is true) { // code } B if (i<5) { //code } C while (var i = 0; i < count; i++) { //code } D while (condition) { //code }

D

How can we teach Karel new commands?

Define a new function

Which function will teach Karel how to spin in a circle one time? A function spin() { turnRight(); } B function spin() { turnLeft(); turnLeft(); turnLeft(); turnLeft(); } C function spin() { turnLeft(); turnLeft(); } D function spin() { move(); move(); move(); move(); }

B

Which general if statement definition is written correctly? A for(condition) { // code } B if(condition) { //code } C if(var i = 0; i < count; i++) { //code } D if false { //code }

B

Why do we use functions in Karel programs?

Break down our program into smaller parts Avoid repeating code Make our program more readable All of the above

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(); }

Called 3 times, defined 1 time

What does the mystery function do? function mystery() { while (noBallsPresent()) { move(); } }

Karel moves until it is on a ball

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

Karel will crash into a wall

If Karel is facing North and the code turnLeft(); turnLeft(); runs; which direction is Karel facing now?

South

If Karel starts at Street 1 and Avenue 1, facing East, where will Karel be, and what direction will Karel be facing after running the following code? (Assume the world is 10x10 in size) move(); turnLeft(); putBall(); turnLeft(); turnLeft(); turnLeft(); move(); turnLeft();

Street 1, Avenue 3, Facing North

If Karel starts at Street 1 and Avenue 3 facing East, what street (row) and avenue (column) will Karel be on after this code runs? move(); move(); move(); turnLeft(); move();

Street 2 and Avenue 6

Karel starts at Street 1 and Avenue 1, facing East. After calling the stairStep function twice, where will Karel be and what direction will Karel be facing? (assume this is a SuperKarel program and the world is 10x10 in size) function stairStep() { move(); turnLeft(); move(); turnRight(); }

Street 3, Avenue 3, Facing East

What's wrong with this code? function start() { move(); go(); go(); } function go() { move(); move(); } function go() { move(); move(); }

The go function has been defined twice

What makes the following command an invalid Karel command? turnleft();

The l should be a capital L

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

This code won't run because of a syntax error

Why do we use if statements in JavaScript?

To do something only if a condition is true

Why do we use if/else statements in JavaScript?

To either do something if a condition is true or do something else

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(); } }

To pick up the ball that is in the last spot, if there is one

Why do we use while loops in JavaScript?

To repeat some code while a condition is true

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

To repeat something a fixed number of times

What is top down design?

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.

You need to write a program that has Karel take all the tennis balls where Karel is standing if there are any there. Karel should end up with no tennis balls on that spot. Which control structure do you need to use?

While Loop

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(); }

ballsPresent()

What is the best way for Karel to move 10 times?

for (var i = 0; i < 10; i++) { move(); }

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

function turnRight() { turnLeft(); turnLeft(); turnLeft(); }

Which of the following commands is a valid Karel command?

move();

What commands does SuperKarel know that regular Karel does not?

turnAround() and turnRight()

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

turnLeft()


Related study sets

Anatomy 201: Chapter 10 - The Muscular System

View Set

English Reading Quiz "Silent Spring

View Set

nur 116 - Davis Advantage / Edge - Seizures

View Set

Behavioral Observation and Screening (BOSR)

View Set