java level 1 quizzes

6
Exercise - Get familiar with common syntax errors 1. Delete a semicolon. Compile. --- ‘;’ expected 2. Invoked a method, i.e., jump(), which is not defined. Compile. --- cannot find symbol - method jump() : 3. Add turn(1, 2) to the code. Compile. --- turn(int) in Animal cannot be applied to (int,int). Required:int; Found: int,int; reason: actual and formal argument list differ in length 4. Add turn(4.5) to the code. Compile. --- incompatible types: possible lossy conversion from double to int 5. Change to public double act(). Compile. --- act() cannot override act() in Animal; return type double is not compatible with void 6. Change to public voids act(). Compile. --- cannot find symbol - class voids 7. Change to public Turtle extends Actor. Compile. --- class, interface, or enum expected 8. Delete right parenthesis: public void act(. Compile. --- illegal start of type 9.Delete right parenthesis: move(;. Compile. ---illegal start of expression Quiz For each of the method signatures below, answer the following questions: a. What is the method name? b. Does the method return a value? If yes, what is the type of the return value? c. How many parameters does the method have d. Write the method call for the method signature. (How would you call the method in your code?) 1. 1. public void eat() a. b. c. d. 2. public void setSize(int sze) a. b. c.

Upload: steven-luo

Post on 21-Jan-2018

181 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java level 1 Quizzes

Exercise - Get familiar with common syntax errors 1. Delete a semicolon. Compile.

--- ‘;’ expected

2. Invoked a method, i.e., jump(), which is not defined. Compile.

--- cannot find symbol - method jump() :

3. Add turn(1, 2) to the code. Compile.

--- turn(int) in Animal cannot be applied to (int,int). Required:int; Found: int,int; reason: actual and formal argument list differ in length

4. Add turn(4.5) to the code. Compile. --- incompatible types: possible lossy conversion from double to int

5. Change to public double act(). Compile.

--- act() cannot override act() in Animal; return type double is not compatible with void

6. Change to public voids act(). Compile.

--- cannot find symbol - class voids

7. Change to public Turtle extends Actor. Compile. --- class, interface, or enum expected

8. Delete right parenthesis: public void act(. Compile. --- illegal start of type

9.Delete right parenthesis: move(;. Compile. ---illegal start of expression

Quiz For each of the method signatures below, answer the following questions:

a. What is the method name?

b. Does the method return a value? If yes, what is the type of the return value? c. How many parameters does the method have

d. Write the method call for the method signature. (How would you call the method in your code?)

1. 1. public void eat() a.

b. c. d.

2. public void setSize(int sze)

a. b. c.

Page 2: Java level 1 Quizzes

d. 3. public boolean isGreater(int number)

a. b.

c. d.

4. Write a method signature for a method named "randomMove". The method has no parameters, and it does not return a value.

5. Explain how you can tell the difference between a command method and a question method:

6. Explain the difference between a class and an object:

7. What is a subclass and what does it inherit?

Quiz:

1. Which of these is the correct way to write a comment that stops at the end of the line? a. /* b. ** c. */ d. //

2. Which of these is a method for writing comments that go over a number of lines?

a. /*, *, *, */ b. //, /*, */, // c. **, *, *, ** d. /*, /*, /*, /*

3. In the following sequence of code, what is being done?

super(20,20,10); setBackground("grasstile.jpg"); populate();

a. set up the grid with grasstile.jpg background and add Spider objects b. set up the grid with any background and define a function called populate

Page 3: Java level 1 Quizzes

c. set up the grid, set the background, drag objects onto the background d. set up the grid with grasstile.jpg background and run the populate function

4. Which two of these are correct ways to set up two variables: x and y, where two numbers can be stored until needed?

a. x,y; b. int x; int y; c. int x; y; d. int x,y;

5. The following code has an error, what is it?

public BugWorld() //constructor for BugWorld { super(30,20,20); setBackground("grassPic.jpg") populate(); }

a. one missing semi-colon

b. one missing curly bracket c. two missing semi-colons d. two missing curly brackets

Quiz:

1. What happens when you compile a Greenfoot program? A. the code you have entered is spell-checked B. the code you have entered is deleted

C. the code you have entered is copied to a separate disk D. the code you have entered is translated into machine code

2. In the code below, what will go between the curly brackets? public class Ant extends Actor

{

} A. just an Act function for an Ant B. all the code instructions for an Ant object

C. all the code instructions for every Actor object D. just a constructor function for an Ant

3. What is the name for the function that sets up a world, or other, object when it is first introduced to

the game?

A. NewWorld B. terminator C. act

D. constructor

4. Which of these is a heading for a constructor function? A. public void Act() B. public void BugWorld()

C. public BugWorld()

Page 4: Java level 1 Quizzes

D. public World()

Quiz:

1.The following questions refer to the code below:

public void moveForwardOrBack(boolean isScared)

{ if (isScared) {

move(-4); } else {

move(4); } }

1) In the code above, isScared is a...

a. method with a boolean return type b. boolean expression

c. literal boolean value d. parameter with a boolean data type

2) Draw the flowchart of moveForwardOrBack method

2. The following questions refer to the code below: int bonus, sales = 10000; if (sales < 5000)

{ bonus = 200; } else if (sales < 7500)

{ bonus = 500; } else if (sales < 10000) { bonus = 750; }

else if (sales < 20000) { bonus = 1000; }

else { bonus = 1250; }

1) What will be the value of bonus after the code above is executed? a. 200 b. 500 c. 750 d. 1000 e. 1250

2) Draw the flowchart of the code above

Quiz

Page 5: Java level 1 Quizzes

1. Match the following words with their definitions:

object compiler class signature Actor source code method instantiate type machine code void parameter

____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____

a. nothing b. the kind of data, such as boolean or int c. an instance of a class d. objects that can be placed into a world e. a blueprint for making objects f. the return type, method name, and parameter list g. a list of inputs to a method h. a direction or a question given to an instance i. directions we write to the computer j. directions the computer understands k. a program that changes source code into machine code L. the process of creating a new object

2. Given the following variable declarations, evaluate the following Boolean expressions: int a= 7; int b = 12; int c = 12; int d = 7; 1. (a ==c ||a ==b) _________ 2. (a==b && c==d) _________ 3. (a == b && b==c) _________ 4. (a==c) _________

3. The following questions are about making the Ball’s image animate a. Declare two instance variables to store the two different Greenfoot Images

b. Write a constructor that stores the image files and sets the current image to one of them c. Write the method switchImage that switches from one image to the other

4. When do you use = and when do you use == in Java? Quiz

1. Complete this for loop that would create 5 different Worms automatically and place them in random locations in the world. int i=0; while (i <____) {

addObject( new ball(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight() ); i++;

}

2. What is the value of x at the end:

int i=0; int x=0;

while (i < 4) { x = x + i;

i++; }

Page 6: Java level 1 Quizzes

A. 1 B. 2 C. 3 D. 4 E. 6

3. Given the following code, the output will be:

int i = 1; int limit = 3;

while (i <= limit) {

System.out.println(i);

i = i + 1; }

A. 1 2 3

B. 1 2

3 4

C. 1 2 3

D. 1 2 3 4

4. What is the output of the following code? int LIMIT = 4; int sum = 0;

while (i <= LIMIT) { sum = sum + i; i = i + 1; }

System.out.println(sum); A. 10 B. 9

C. 11 D. 8