Which of the following reserved words in Java is used to create an instance/obje
ID: 3810871 • Letter: W
Question
Which of the following reserved words in Java is used to create an instance/object of a class? a. private b import c. final d. public e. new In order to preserve the encapsulation of an object, which of the following is done? a. Make the constructor private b. Make the instance data private c. Make the class final d. Make the class interface public e. Make the data final If a method does not have a return statement, then a. It must be defined as a public method b. It must be a private method c. The return type is user-defined d. Make the class interface public e. It must be a void method A class' constructor usually defines a. the number of methods in a class b. how an object is initialized c. how an object is interfaced d. Make the class interface public e. the number of instance data in the class Instance data for a Java class a. may be primitive data types b. may be primitive types or objects c. are limited to String d. are limited to primitive data types e. are limited to class objects The class constructor a. has the same name as the class and has void return type b. has the same name as the class c. has the same name as the class and is never used d. has the same name as the class and has no return type e. has the same name as the class and returns a methodExplanation / Answer
1. Answer is e. new.
In Java new keyword is used to create an instance or object of a class. The new keyword when used will create an object to a class and returns the object. It is implicitly equivalent to the calling of a constructor of a class.
2. Answer is b. Make the instance data private.
Encapsulation is the process of protecting the accessing of an object from outside the class. It provides a wrapper to the object. In order to preserve the encapsulation of an object, the instance data has to be made private using the private keyword which is one of the access specifiers in Java.
3. Answer is e. It must be a void method.
A class in Java is a collection of instance variables and methods. The methods are in general used to operate on the instance variables of a class. The method in Java has a return type (void if the method does not return any value), a name and a list of parameters. A method can have a return statement (return statement without any value) even if the method does not return a value.
Eg:
public class MyClass
{
int a;
public void sample()
{
System.out.println(a);
return; // Return statement without any value
}
}
4. Answer is b. how an object is initialized.
A class constructor usually defines how an object is initialized. The constructor gets invoked automatically when an object is created and initializes the instance variables to either default values or to the specified values.
5. Answer is b. may be aprimitive types or objects.
Instance data for a Java class can be primitive types int,float,double,byte, short or objects.
6. Answer is d. has the same name as the class and has no return type.
The constructor of any class should have the same name as the class in which it is defined. And there cannot be any explicit return type for a constructor.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.