1.
What is the difference between private and public functions ?
Correct Answer
A. Public functions can be used by anyone, private can only be used by other code in the class you are writin
Explanation
Public functions can be accessed and used by any code, whether it is within the same class or outside of it. On the other hand, private functions are only accessible and usable within the class they are defined in. They cannot be accessed or used by any other code outside of that class. Therefore, the main difference between public and private functions is their accessibility and usability by other code.
2.
With inheritance, a derived subclass object can directly access any *
Correct Answer
A. public superclass member (and protected subclass members if it's in the same package)
Explanation
Inheritance allows a derived subclass object to directly access any public superclass member. Additionally, if the subclass is in the same package as the superclass, it can also access protected subclass members. This means that the derived subclass object can access both public and protected superclass members, as long as it is in the same package as the subclass. However, it cannot access private superclass members directly.
3.
What is a member of a class ?
Correct Answer
D. Attribute or method
Explanation
A member of a class refers to any attribute or method that is associated with that class. It can be an attribute, which represents the data or state of an object, or it can be a method, which represents the behavior or actions that an object can perform. Therefore, a member of a class can be either an attribute or a method.
4.
Methods that are marked protected can be called in any subclass of that class.
Correct Answer
A. True
Explanation
Protected methods in a class can be accessed by any subclass of that class. This means that subclasses inherit the protected methods from the parent class and can call them within their own code. This allows for code reuse and promotes the concept of encapsulation and inheritance in object-oriented programming. Therefore, the statement "Methods that are marked protected can be called in any subclass of that class" is true.
5.
Choose the appropriate data type for this field: weightInKilos
Correct Answer
C. double
Explanation
The appropriate data type for the field "weightInKilos" would be double because it suggests that the weight will be measured in decimal numbers, which can include fractions or decimals. The double data type allows for the storage of floating-point numbers with a larger range and higher precision compared to other data types like int or float.
6.
What displays from the following statements?
String word = "abcde";
for (int i = 0; i <4; i+=2)
System.out.print(word[i]);
Correct Answer
D. Ac
Explanation
The given code initializes a string variable "word" with the value "abcde". The for loop then iterates over the indices of the string from 0 to 4 with a step of 2. In each iteration, it prints the character at the current index.
In the first iteration, i=0, so it prints the character at index 0 which is 'a'.
In the second iteration, i=2, so it prints the character at index 2 which is 'c'.
Therefore, the output is "ac".
7.
Choose the best definition for a Class.
Correct Answer
A. An object definition, containing the data and function elements necessary to create an object
Explanation
A class is a blueprint or template for creating objects. It contains the data and function elements that define the properties and behaviors of the objects that will be created from it. This definition allows for the creation of multiple instances of objects with similar characteristics and functionality.
8.
What would display from the following statements?
int [ ] nums = {1,2,3,4,5,6};
System.out.println((nums[1] + nums[3]));
Correct Answer
A. 6
Explanation
The code initializes an array called "nums" with the values {1,2,3,4,5,6}. The statement "System.out.println((nums[1] + nums[3]))" prints the sum of the second element (nums[1]) and the fourth element (nums[3]) of the array, which is 2 + 4 = 6.
9.
Which package do you need to use widgets such as JApplet, JFrame, JPanel and JButton?
Correct Answer
D. Javax.swing
Explanation
The correct answer is javax.swing. This package is needed to use widgets such as JApplet, JFrame, JPanel, and JButton. The javax.swing package provides a set of components and classes for creating graphical user interfaces in Java. It is an extension of the java.awt package and provides more advanced and customizable components for building GUI applications.
10.
The methods wait(), notify() and notifyAll() in Object need to be called from synchronized pieces of code.
Correct Answer
A. True
Explanation
The methods wait(), notify(), and notifyAll() in the Object class need to be called from synchronized pieces of code because they are used for inter-thread communication and synchronization. When a thread calls the wait() method, it releases the lock on the object and waits until another thread calls the notify() or notifyAll() method to wake it up. This communication and synchronization between threads should be done within synchronized blocks or methods to ensure thread safety and prevent race conditions. Therefore, the statement is true.