Introduction to Programming Unit Quiz
what is the proper format for a single line comment in Karel? This is a comment
//
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++ 1){ if(ballsPresent()) { takeBall(); } else { putBall(); putBall(); } }
1
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 wrong with this for loop? for(var i= 0, i <10, i+ 1){ move(); } A.the for loop uses commas instead of semicolons B.it should say i++ instead of i+ 1
A and B
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
How can we improve the following program? function start(){ move(); move(); move(); move(); move(); move(); move(); move(); move(); }
Use a for loop to repeat the move command
how can we teach Karel new command?
Define a new function
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
Super Karel starts at Street 1, Avenue 1, facing East in a 5*5 world. What will happen after this code runs? move(); move(); turnRight(); move();
Karel will crash into a wall
Karel starts at Street 1 and Avenue 1, facing East. After calling the stair Step function twice, where will Karel be and what direction will Karel be facing ? function stairStep(){ move(); turnLeft(); move(); turnRight(); }
Street 3, Avenue 3, Facing East
Karel starts at Street 1, Avenue 1, facing East in a 5*5 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
In the following code below from the CleanupKarel example, that 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()){ takeBalls(); } move(); } //if statement #2 if(ballsPresent()){ takeBall(); } }
To pick up the ball that is in the last spot, if there is one
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.
why does programming indent their code? -helps show the structure of the code. -easier for other people to understand -a key part of good programming
all of the above
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();
Say you want to write a program to have Karel put down 300 tennis balls. Which control structure would you use?
for loop
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();
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? move(); turnLeft(); putball(); turnLeft(); turnLeft(); turnLeft(); move(); turnLeft();
street 1, avenue3, facing North
What makes the following command an invalid Karel command? -turnleft();
the "l" should be a capital "L"
Whats wrong with this code? function start(){ move(); go(); go(); } function go(){ move(); move(); } function go(){ move(); move(); }
the go function has been defined twice
Which of the following is not a valid condition to go in an if statement for Karel?
turnLeft()