1.
This is how you create a Java multi-line comment to place the following into a comment.
/* Replace the image on the screen and
then move it right 10 pixels */
Correct Answer
A. True
Explanation
The given code snippet demonstrates the creation of a multi-line comment in Java. The comment starts with /* and ends with */. It is used to provide explanatory or descriptive information about the code. In this case, the comment states that the image on the screen should be replaced and then moved right by 10 pixels. Since this is a valid way to create a multi-line comment in Java, the correct answer is True.
2.
Suppose you are given a variable named temperature that holds a value. This is how you would write an if-else statement that will display Above Freezing if temperature is greater than 32, Freezing if temperature is equal to 32, else it will display At or Below Freezing.
if (temperature > 32)
System.out.println("Above Freezing");
else (temperature == 32)
System.out.println("Freezing");
else
System.out.println("At or Below Freezing");
Correct Answer
B. False
Explanation
The given if-else statement is not written correctly. In an if-else statement, the condition in the "else" part should not have a condition. It should simply be "else". Therefore, the correct answer is false.
3.
Simplify the following expressions to either true or false:
Given:
x is an integer variable holding the value 3
y is an integer variable holding the value -5
z is an integer variable holding the value -4
(x > z) || (y < z) && !(y < x)
Correct Answer
A. True
Explanation
The expression is evaluating whether the statement "(x > z) || (y < z) && !(y < x)" is true or false.
The expression consists of three parts connected by logical operators.
1) (x > z) - This part checks if the value of x is greater than the value of z. In this case, x is 3 and z is -4, so this part evaluates to true.
2) (y < z) - This part checks if the value of y is less than the value of z. In this case, y is -5 and z is -4, so this part evaluates to true.
3) !(y < x) - This part checks if the value of y is not less than the value of x. In this case, y is -5 and x is 3, so y is indeed less than x. Therefore, this part evaluates to false.
Finally, the expression is evaluated using the logical operators. The "&&" operator has higher precedence than the "||" operator, so the expression is evaluated as follows:
(y < z) && !(y < x) = true && false = false
(x > z) || false = true || false = true
Therefore, the overall expression evaluates to true.
4.
True of False. Java is not case-sensitive
Correct Answer
B. False
Explanation
Java is case-sensitive, meaning that it distinguishes between uppercase and lowercase letters. This means that variables, method names, and other identifiers must be written with the correct capitalization in order for the code to compile and run correctly. For example, "myVariable" and "myvariable" would be considered as two different variables in Java. Therefore, the correct answer is False.
5.
This is how you write an if-else statement that will display JCCC 45% of the time, else it will display Cavaliers.
if (Greenfoot.getRandomNumber(100) < 45)
System.out.println("JCCC");
else
System.out.println("Cavaliers");
Correct Answer
A. True
Explanation
The given code uses the Greenfoot.getRandomNumber(100) method to generate a random number between 0 and 99. If the generated number is less than 45, it will display "JCCC", otherwise it will display "Cavaliers". Since the condition is checking if the generated number is less than 45, which means it has a 45% chance of being true, the statement "JCCC" will be displayed approximately 45% of the time. Therefore, the correct answer is true.
6.
Write a statement that will declare an integer instance variable called count and set its initial value to 0.
Correct Answer
A. Public int count = 0;
Explanation
The correct answer is "public int count = 0;" because it declares an integer instance variable called "count" and initializes its initial value to 0. This statement follows the correct syntax for declaring and initializing an integer instance variable in Java.
7.
To pass additional data to a method we specify a
Correct Answer
C. Parameter
Explanation
To pass additional data to a method, we specify a parameter. A parameter is a variable that is used to receive and store the additional data that is passed into the method when it is called. By specifying a parameter, we can provide the necessary information for the method to perform its task effectively.
8.
A compiler
Correct Answer
A. Translates source code into executable code
Explanation
A compiler is a software tool that translates source code, which is written in a high-level programming language, into executable code, which can be run on a computer. It performs various tasks such as lexical analysis, syntax analysis, semantic analysis, and code generation. The output of a compiler is a compiled program that can be executed by the computer's processor. Therefore, the correct answer is "translates source code into executable code".
9.
In object-oriented programming a class is
Correct Answer
D. A model or template from which objects are created
Explanation
In object-oriented programming, a class serves as a model or template that defines the properties and behaviors of objects. It specifies the attributes (data) and methods (functions) that an object of that class can have. Objects are instances of a class, meaning they are created based on the class definition. Therefore, the correct answer is "a model or template from which objects are created."
10.
Write one statement that declares a local variable named city and initializes it to Overland Park.
Correct Answer
A. String city = “Overland Park”;
Explanation
The correct answer is "String city = "Overland Park";" because it declares a local variable named "city" and initializes it with the value "Overland Park". This statement follows the correct syntax for declaring and initializing a string variable in Java.
11.
The best example for the correct way to name a class is
Correct Answer
B. BankAccount
Explanation
The correct way to name a class is "BankAccount" because it follows the standard naming convention of using PascalCase for class names. This convention suggests that each word in the class name should start with a capital letter and there should be no underscores or spaces between the words. Using this naming convention improves code readability and consistency.
12.
Write a method signature for a method named "randomMove". The method has no parameters, and it does not return a value.
Correct Answer
B. Public void randomMove()
Explanation
The correct answer is "public void randomMove()". This is the correct method signature for a method named "randomMove" that has no parameters and does not return a value. The "public" keyword indicates that the method can be accessed from other classes. The "void" keyword indicates that the method does not return any value.
13.
What the value of x is at the end:
int i=0;
int x=0;
while (i < 4)
{
x = x + i;
i++;
}
Correct Answer
C. 3
Explanation
The value of x at the end of the code is 3. This is because the while loop runs 4 times, with i starting at 0 and incrementing by 1 each time. Inside the loop, x is updated by adding the current value of i to it. So, on the first iteration, x becomes 0+0=0, on the second iteration x becomes 0+1=1, on the third iteration x becomes 1+2=3, and on the fourth iteration x becomes 3+3=6. However, since the condition i
14.
Given the following variable declarations, evaluate the following Boolean expression:
int a= 7;
int b = 12;
int c = 12;
int d = 7;
(a ==c ||a ==b)
Correct Answer
B. False
Explanation
The given Boolean expression is evaluating whether either a is equal to c or a is equal to b. In this case, a is not equal to c (7 is not equal to 12) and a is not equal to b (7 is not equal to 12), so the expression evaluates to False.
15.
Variable Declarations need a data type and a variable name
Correct Answer
A. True
Explanation
Variable declarations in programming languages require both a data type and a variable name. The data type specifies the type of data that the variable can hold, such as integer, float, or string. The variable name is used to identify and refer to the variable in the code. Without a data type, the programming language would not know how to allocate memory for the variable, and without a variable name, the programmer would not be able to access or manipulate the variable. Therefore, both a data type and a variable name are necessary for variable declarations.
16.
Which of the following is not a data type in Java?
Correct Answer
E. Constant
Explanation
The data types in Java include int, double, boolean, and String. However, "constant" is not a data type in Java. Constants in Java are typically declared using the "final" keyword and can have data types such as int, double, boolean, or String, but constant itself is not a data type.
17.
An identifier can be named in Java using letters, digits, underscores and the $ sign. They may not begin with an underscore.
Correct Answer
B. False
Explanation
they cannot begin with a digit
18.
Mark the following identifiers that are valid names to use in Java.
Correct Answer(s)
B. _days
C. NumDays
D. $days
Explanation
Can not start with a digit and cannot have a dot.
19.
Which is the best declaration for a variable that will hold monthly rainfall. It is initialized to 0?
Correct Answer
A. Float totalMonthlyRainfall = 0.0 ;
Explanation
The best declaration for a variable that will hold monthly rainfall and is initialized to 0 is "float totalMonthlyRainfall = 0.0 ;". This is because rainfall is typically measured in decimal values, so using a float data type allows for more precise calculations. Additionally, initializing the variable to 0 ensures that it starts with a default value before any rainfall data is recorded.
20.
Practice with logical operators
Given:
int age1 = 21;
int age2 = 14;
int age3 = 15;
int birth = 0;
String name = “JCCC”;
age1 < age2 && age2 < age3
Correct Answer
B. False
Explanation
age1 < age2 && age2 < age3
(21) < (14) && (14) < (15)
F && T
F
21.
Given the code below, what are the local variables?
public class MathStudent extends Actor
{
protected String firstName;
protected String lastName;
public MathStudent()
{
firstName = "";
lastName = "";
}
public void countToFive()
{
int i = 1;
int limit = 5;
while (i <= 5)
{
System.out.println(i);
i = i + 1;
}
}
}
Correct Answer
B. Limit, i
Explanation
The local variables in the given code are "limit" and "i". These variables are declared and initialized within the method "countToFive" and are used within the while loop to control the iteration and print the value of "i" until it reaches the limit of 5. The variables "firstName" and "lastName" are instance variables of the class and are not considered local variables in this context.
22.
Mark the arrays that are declared correctly by good programming standard.
Correct Answer(s)
A. Int[] scores;
C. Float[] rates;
Explanation
The arrays "int[] scores;" and "float[] rates;" are declared correctly by good programming standard. In Java, it is recommended to declare arrays using the square brackets immediately after the data type. Therefore, "int[] scores;" and "float[] rates;" follow this convention and are considered to be declared correctly. On the other hand, "int scores [];" and "float rates [];" also work in Java, but they are not considered as good programming practice.
23.
Declare and create a new array of 5 elements for a scores array.
Correct Answer
A. Int[] scores = new int[5];
Explanation
The correct answer is "int[] scores = new int[5];". This is the correct way to declare and create a new array of 5 elements for a scores array in Java. The "int[]" specifies that the variable "scores" is an array of integers, and "new int[5]" creates a new array with a size of 5 elements.
24.
Write a line of code to change the element at index 3 to 5 in the scores array.
Correct Answer
A. Scores[3] = 5;
Explanation
The correct answer is "scores[3] = 5;" because it correctly assigns the value of 5 to the element at index 3 in the scores array.
25.
Objects are created from
Correct Answer
B. Classes
Explanation
Objects are created from classes. In object-oriented programming, a class is a blueprint or template that defines the properties and behaviors of an object. It is like a blueprint for creating objects. When we create an object, we are essentially creating an instance of a class, which allows us to access and use the properties and behaviors defined within the class. Therefore, classes are used to create objects.
26.
Objects have several ___________ that are blocks of code that perform specific tasks or actions.
Correct Answer
A. Methods
Explanation
Methods are blocks of code that are used to perform specific tasks or actions. They are associated with objects and can be called upon to execute a particular set of instructions. Methods can have parameters, which are variables that are passed into the method to provide it with necessary information. In this context, methods are the correct answer because they are the code blocks responsible for performing specific tasks or actions within objects.
27.
This specifies what type of data a method call with return:
Correct Answer
D. Return type
Explanation
The correct answer is "return type". In programming, a method is a block of code that performs a specific task. When a method is called, it can return a value or perform an action. The return type specifies the type of data that the method will return after it has completed its execution. It could be a primitive type like int or double, or a non-primitive type like a class or an array. The return type is important as it helps the programmer understand what kind of data they can expect to receive from the method.
28.
The mechanism that is used to pass additional information to a method is called a:
Correct Answer
B. Parameter
Explanation
A parameter is a mechanism used to pass additional information to a method. It allows the method to accept values or variables that can be used within the method's code block. By providing parameters, we can make the method more flexible and reusable as it can work with different values each time it is called. Therefore, the correct answer is "parameter".
29.
The specification of a method, which shows its return type, name and parameters is called its:
Correct Answer
D. Signature
Explanation
The specification of a method, which includes its return type, name, and parameters, is called its signature. The signature acts as a unique identifier for the method and helps distinguish it from other methods in the program. It provides crucial information about the method's functionality and how it can be used.
30.
Another name for an objects is an:
Correct Answer
D. Instance
Explanation
Another name for an object is an instance. In object-oriented programming, an instance refers to a specific occurrence or realization of a class. A class is a blueprint or template that defines the properties and behaviors of an object, while an instance is a unique entity created from that class. Therefore, the term "instance" is synonymous with "object" in this context.
31.
An instruction that tells an object to perform a specific task it has programming code to do is called a:
Correct Answer
C. Method call
Explanation
A method call is an instruction that tells an object to perform a specific task using programming code. It is used to invoke a method, which is a block of code that performs a particular action. By calling a method, the object executes the code within that method and carries out the desired task. Therefore, a method call is the correct term for this type of instruction.
32.
The command that allows you to make a decision based on true or false conditions is a(n):
Correct Answer
D. If statement
Explanation
The correct answer is "if statement". An if statement is a programming command that allows the program to make a decision based on true or false conditions. It is used to execute a block of code only if a certain condition is true. This helps in controlling the flow of the program and making it more dynamic and responsive to different scenarios.
33.
When you need to call a method from a different class you must specify the class name before the method name using:
Correct Answer
A. Dot notation
Explanation
When calling a method from a different class, the class name must be specified before the method name using dot notation. Dot notation is a way to access members (methods or variables) of a class by using the class name followed by a dot (.) and then the member name. This allows the program to identify which class the method belongs to and execute the correct method. Parenthetical citation, curly braces, and spaces are not used for this purpose.
34.
Methods that belong to the class itself are marked with the keyword:
Correct Answer
C. Static
Explanation
Static methods belong to the class itself rather than to any specific instance of the class. They can be accessed using the class name without creating an object of the class. The keyword "static" is used to mark these methods.
35.
By convention, class names in Java should always start with a:
Correct Answer
A. Capital letter
Explanation
Class names in Java should always start with a capital letter. This is a convention followed by Java programmers to make the code more readable and distinguish class names from variables and methods. Starting class names with a capital letter helps to easily identify and differentiate them from other elements in the code. It is a widely accepted practice in the Java community and adhering to this convention improves the overall code consistency and maintainability.
36.
This default Greenfoot method is automatically created in each new class and is what is executed when you click on Act or Run in the window:
Correct Answer
D. Act()
Explanation
The correct answer is "act()". This method is automatically created in each new class in Greenfoot and is executed when you click on Act or Run in the window. It is responsible for defining the actions that an object should perform during each frame of the simulation.
37.
Comments are ignored by the computer but necessary for documentation for the user and they are started with:
Correct Answer
A. /**
38.
By convention, method names in Java should always start with a:
Correct Answer
B. Small letter but use capital letters for the beginning of words with no spaces (camel casing)
Explanation
In Java, it is a convention to start method names with a small letter but use capital letters for the beginning of words with no spaces. This convention is known as camel casing. By following this convention, it improves code readability and consistency. Starting method names with a capital letter is not a convention in Java.
39.
A bit of memory that belongs to an object to store information is called a:
Correct Answer
B. Instance variable
Explanation
An instance variable is a bit of memory that belongs to an object and is used to store information. It is different from a method call, which is a way to invoke a method or function in an object. The return type, on the other hand, refers to the type of value that a method or function returns. Therefore, the correct answer is instance variable.
40.
Good Java coding practice dictates that we should declare all of our variables:
Correct Answer
A. At the top of the class
Explanation
In Java, it is considered good coding practice to declare all variables at the top of the class. This is known as variable declaration at the class level. By declaring variables at the top of the class, it becomes easier to locate and understand all the variables used in the class. It also helps in avoiding any confusion or errors that may arise from declaring variables in different parts of the class. Additionally, declaring variables at the top of the class allows for better organization and readability of the code.
41.
An assignment statement enables us to store something into a variable. The symbol that allows us to do that is:
Correct Answer
B. =
Explanation
The symbol "=" is used in an assignment statement to store a value into a variable. It is used to assign a specific value to a variable, allowing us to store and manipulate data in a program.
42.
This statement can be added to the IF statement to specify what should happen if the condition is false:
Correct Answer
D. Else
Explanation
The correct answer is "else" because in programming, the "else" statement is used in conjunction with the "if" statement to specify an alternative action to be taken if the condition in the "if" statement evaluates to false. It allows the program to execute a different set of instructions when the condition is not met.
43.
Putting this symbol in front of something means NOT:
Correct Answer
B. ! (exclamation mark)
Explanation
The exclamation mark (!) is used to indicate "NOT" in programming languages and logical expressions. It is commonly used to negate a condition or reverse the truth value of a statement. In this context, putting the exclamation mark in front of something means that the opposite or negation of that something is true.
44.
These symbols mean AND, which will join two conditions:
Correct Answer
A. &&
Explanation
The symbols "&&" represent the logical operator "AND" in programming. It is used to join two conditions and evaluates to true only if both conditions are true. In other words, it checks if both conditions are satisfied simultaneously.
45.
When we need to repeat code or do a similar task repeatedly, we need a:
Correct Answer
B. Loop
Explanation
When we need to repeat code or do a similar task repeatedly, we need a loop. A loop is a programming construct that allows us to execute a block of code multiple times. It helps in automating repetitive tasks and saves us from writing the same code multiple times. By using loops, we can iterate over a collection of data, perform calculations, or execute a set of instructions until a certain condition is met. Overall, loops provide a way to efficiently and effectively repeat code and perform repetitive tasks in programming.
46.
This type of data structure allows us to store many values of the same type and access them using an index, like this whiteKeys[2], which would access the third element:
Correct Answer
B. Array
Explanation
An array is a data structure that allows us to store multiple values of the same type and access them using an index. In this case, the example given is whiteKeys[2], which would access the third element in the array. Arrays are commonly used when we need to store and manipulate a collection of values in a systematic way.
47.
A special expression that means “nothing” or “no object” is:
Correct Answer
A. Null
Explanation
The correct answer is "null." In programming, "null" is a special expression that represents the absence of a value or the lack of an object. It is often used to indicate that a variable or reference does not currently point to any valid data.
48.
A method with a void return type does not return a value.
Correct Answer
A. True
Explanation
A method with a void return type does not return a value. In programming, the return type of a method determines the type of value that the method can return. When a method has a void return type, it means that the method does not return any value. This is useful for methods that perform actions or have side effects but do not need to return a result.
49.
Actors are classes that are placed into the world
Correct Answer
A. True
Explanation
Actors are classes that are placed into the world. This means that in a programming context, actors are objects or instances of a class that can be placed or instantiated within a virtual or physical environment. They can interact with other actors and the environment, and perform actions or behaviors defined by their class. This is a common concept in game development, where actors can represent characters, objects, or other entities within the game world.
50.
Computers do not understand source code that we type in so it needs to be translated into machine code by a compiler before being executed.
Correct Answer
A. True
Explanation
Computers operate using machine code, a low-level language that consists of binary instructions. However, humans typically write programs in high-level languages such as C++, Java, or Python. To bridge this gap, a compiler is used to translate the source code written by humans into machine code that the computer can understand and execute. Therefore, the statement that computers do not understand source code and require a compiler to translate it into machine code is true.