Java Quiz (Sections 1-10)

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Why do we use methods in Java programming (for us)?

- Break down our program into smaller parts - Avoid repeating code - Make our program more readable

In the code snippet below, how many times will the putBall command run? putBall(); for(int i = 0; i < 100; i++) { putBall(); } for(int i = 0; i < 6; i++) { putBall(); }

107 times

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

A for loop

What is a method in Karel?

A method is a command that Karel can do. It has a name and a set of instructions.

What is a condition in Karel programming?

A method that returns a true or false answer. Typically used within a control structure.

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 to pick up all of the tennis balls in a location, but you don't know how many tennis balls there are. Which control structure would you use?

A while loop

Which of these code snippets would be the best implementation of a method called moveToWall() which moves Karel to the end of a row in the current direction? A) while(frontIsClear()) { move(); } B) while(frontIsBlocked()) { move(); } C) while(frontIsClear()) { turnLeft(); move(); } D) for(int i = 0; i < 10; i++) { move(); }

A)

Which of these is a valid way to write a single line comment in Java? A) // This is a comment B) ++ This is a comment C) This is a comment // D) /* This is a comment

A)

Which answer(s) are wrong with this method declaration? public karelDance() [ move(); turnLeft(); move(); turnLeft(); move(); ] A) Not using curly brackets B) Missing void C) Using public instead of private D) Illegal characters in the method name

A, B, C

Which answer(s) are wrong with the style of this method declaration? private void spinKarel() { turnLeft(); turnLeft(); turnLeft(); turnLeft(); } A) indenting is wrong B) no comments C) no camelCase D) brackets are not on separate line

A, B, D

Which method will teach Karel how to spin in a circle one time? A) private void spin() { turnRight(); } B) private void spin() { turnLeft(); turnLeft(); turnLeft(); turnLeft(); } C) private void spin() { turnLeft(); turnLeft(); } D) private void spin() { move(); move(); move(); move();How do you write a SuperKarel class? }

B

Which of these loops has Karel move 7 times? A) for(int i = 1; i < 7; i++) { move(); } B) for(int i = 0; i < 7; i++) { move(); } C) for(int i = 0; i <= 7; i++) { move(); } D) for(i = 0; i < 7; i++) { move(); }

B)

What is the correct class signature in Java for a SuperKarel program? A) public class FunKarel << SuperKarel B) public class FunKarel extends Karel C) public class FunKarel extends SuperKarel D) public FunKarel extends SuperKarel

C)

Which of these loops will run correctly 10 times? A) for(int i = 0, i < 10, i++) { turnLeft(); } B) for(int i = 0; i < 10; i + 1) { turnLeft(); } C) for(int i = 0; i < 10; i++) { turnLeft(); } D) for(int i = 0; i > 10; i++) { turnLeft(); }

C)

What does a Karel run method look like? A) public run() { //code } B) public run(){ //code } C) public void run { //code } D) public void run() { //code }

D)

What is the name of this class: public class FunKarel extends Karel { public void run() { move(); putBall(); move(); } }

FunKarel

If Karel is not on a tennis ball, and all directions around are clear, what will happen when running this code? while(noBallsPresent()) { turnLeft(); }

Karel will go into an infinite loop

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 3 facing East, what row and column will Karel be on after this code runs? move(); move(); move(); turnLeft(); move();

Street 2 and Avenue 6

What is the run method in a Java program with Karel?

The method that is called to start a Karel program.

Why do we use while loops in Java?

To repeat some code while a condition is true

Why do we use for loops in Java?

To repeat something for 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.

How can we teach Karel new commands?

define a new method

What is a general for loop definition?

for(int i = 0; i < count; i++) { //code }

What can be used to teach Karel to turn right?

methods

Which of these is a valid Karel command in Java? - move(); - MOVE - move; - move()

move();

What method would create a method called turnRight that will have Karel turn left three times?

private void turnRight() { turnLeft(); turnLeft(); turnLeft(); }

Which is the correct class signature for a Karel program named CleanupKarel? - public class CleanupKarel extends Karel - public class CleanupKarel < Karel - public CleanupKarel - CleanupKarel extends Karel

public class CleanupKarel extends Karel

How do you write a SuperKarel class?

public class FunProgram extends SuperKarel

Streets are

rows

What is the name of the method that gets called to start a Java Karel program? - go() - start() - run() - move()

run()

Why do we use methods in a Java program for Karel?

to teach Karel new commands

Which of these is a valid Karel command in Java? - turnLeft(); - turnleft(); - turnLeft() - TurnLeft

turnLeft();

What is a general while loop definition?

while(condition) { //code }

Which are the proper formats for a Java comment? A) /* * * My java comment */ B) /** * * My java comment */ C) // My java comment

all of the above

Why does a programmer indent their code? - Helps show the structure of the code. - Easier for other people to understand. - A key part of good programming style! - All of the above

all of the above

Avenues are

columns


संबंधित स्टडी सेट्स

Life and health insurance - exam fx part 6

View Set

Chapter 6 Organizational behavior Smartbook stuff

View Set

Chapter 46: Urinary Elimination (Urinary Elimination and the Nursing Process)

View Set

Life Science Chapter 4: Genetics

View Set

Chapter 24: Growth and Development of the Toddler: 1 to 3 Years

View Set