1.
Which two keywords directly support looping?
Correct Answer(s)
B. For
C. While
Explanation
The keywords "for" and "while" directly support looping. The "for" keyword is used for creating a loop that executes a block of code a specified number of times. It consists of an initialization, a condition, and an increment or decrement statement. The "while" keyword is used for creating a loop that executes a block of code as long as a specified condition is true. It only requires a condition to be evaluated.
2.
Which are primitive data types?
Correct Answer(s)
A. Int
D. Long
E. Boolean
Explanation
The primitive data types in this question are int, long, and boolean. These data types are considered primitive because they are the most basic and fundamental data types in programming. They are not objects and do not have any methods or properties associated with them. Int and long are used to represent whole numbers, while boolean is used to represent true or false values. String, on the other hand, is not a primitive data type as it is an object that represents a sequence of characters.
3.
Given:
1. public interface Player {
2. // insert code here
3. // insert code here
4. }
5. class F implements Player {
6. public void play(){
7. // ...
8. }
9. public void stop(){
10. // ...
11. }
12. }
Which two, inserted independently at lines 2 and 3, allow the code to compile?
(Choose two.)
Correct Answer(s)
B. Void play (); void stop();
D. Public void play (); public void stop();
Explanation
The correct answer is "void play (); void stop();,public void play (); public void stop();". These two options allow the code to compile because they correctly implement the methods specified in the Player interface. The interface requires the implementation of a play() method and a stop() method, so inserting these two options at lines 2 and 3 would satisfy this requirement.
4.
You are asked to create code that defines a Beverage, and includes method implementation code for some beverage behaviors. Beverage subtypes will be required to provide implementations of some, but not all, of the methods defined in Beverage. Which approach correctly implements these goals?
Correct Answer
D. Create an abstract Beverage class that defines both abstract and concrete methods.
Explanation
The correct answer is to create an abstract Beverage class that defines both abstract and concrete methods. This approach allows for the creation of a base class that provides some default behavior for the beverage, while also allowing subclasses to override and provide their own implementation for specific methods. This provides flexibility and reusability in the code, as different beverage subtypes can share common behavior while also having the ability to customize certain methods as needed.
5.
Given:
1. // insert code here
2. void play();
3. void stop();
4. }
5. // insert code here
6. public void play() { }
7. public void stop() { }
8. }
Which, inserted at lines 1 and 5, allows the code to compile?
Correct Answer
A. 1. interface Player {
5. class DVDPlayer implements Player {
Explanation
The code snippet provided is defining a class named "DVDPlayer" that implements the "Player" interface. In order for the code to compile, the class "DVDPlayer" needs to implement all the methods declared in the "Player" interface. Therefore, the correct answer is 1. interface Player { 5. class DVDPlayer implements Player {
6.
Which two are true? (Choose two.)
Correct Answer(s)
B. A class can implement more than one interface.
C. Many classes can implement the same interface.
Explanation
A class can implement more than one interface because interfaces provide a way for a class to define multiple contracts or behaviors that it can adhere to. This allows a class to inherit and implement the methods and properties of multiple interfaces simultaneously.Many classes can implement the same interface because interfaces define a contract or a set of methods and properties that a class must implement. Multiple classes can implement the same interface, each providing their own implementation of the methods and properties defined in the interface. This allows for code reusability and polymorphism.
7.
Which two are true about JavaScript and HTML? (Choose two.)
Correct Answer(s)
B. JavaScript and HTML are NOT compiled.
C. JavaScript provides more client-side functionality than HTML alone.
Explanation
JavaScript and HTML are not compiled, meaning they are not converted into machine code before execution. Instead, they are interpreted by the browser at runtime. JavaScript provides more client-side functionality than HTML alone, as it allows for dynamic content, interactivity, and manipulation of web page elements.
8.
Which ones are legal declarations?
Correct Answer(s)
A. String st = null;
B. String st = "Hello";
E. String st = new String ("Hello");
Explanation
The correct answer options are all legal declarations of a string variable.
- "String st = null;" is a valid declaration where the variable "st" is assigned a null value.
- "String st = 'Hello';" is an incorrect declaration as single quotes should not be used for string literals.
- "String st =="Hello";" is an incorrect declaration as it uses double equal signs instead of a single equal sign for assignment.
- "String st = new String ("Hello");" is a valid declaration where a new instance of the String class is created using the constructor.
9.
Which type of primitive can be assigned a new value within a conditional expression?
Correct Answer
D. Boolean
Explanation
A boolean is the only primitive type that can be assigned a new value within a conditional expression. This is because a conditional expression evaluates to a boolean value (true or false), and therefore a boolean variable can be assigned the result of the evaluation. In contrast, the other primitive types (int, short, byte, and char) cannot be assigned a new value within a conditional expression as they do not directly represent boolean values.
10.
Which demonstrates inheritance?
Correct Answer
C. Class A extends B { }
Explanation
The correct answer is "class A extends B { }". This demonstrates inheritance because the class A is extending the class B, which means that class A will inherit all the properties and methods of class B. This allows class A to reuse the code from class B and add additional functionality if needed.