JAVA PROGRAMMING QUESTIONS 1.) What is the difference between the contents of a
ID: 3759706 • Letter: J
Question
JAVA PROGRAMMING QUESTIONS
1.) What is the difference between the contents of a Java variable of a primitive type and a Java variable of a class type?
2.) How is it possible for two String objects with identical values not to be equal when compared with the == operator?
3.) Why can a class variable of a base type be set to reference an object of a derived type, eg: Shape s = new Circle("Blue", 1.0);
4.) How can the methods of a derived class access private instance variables of its base class?
5.) How is the caller's flow of control (sequence of statement execution) different when an exception is thrown from a method that it calls, vs. when the method returns normally?
6.) When does a class require an 'equals' method to be written?
7.) How does Java support the concept of encapsulation?
8.) Describe the difference between an object and a class.
9.) How is a 'static' variable in a class different from a regular (instance) variable in a class?
10.) What is method "overriding"? Why would you override a method?
Explanation / Answer
What is the difference between the contents of a Java variable of a primitive type and a Java variable of a class type?
Ans :
Every variable in Java has a type, which essentially tells Java how that variable should be treated, and how much memory should be allocated for that variable. Java has basic types for characters, different kinds of integers, and different kinds of floating point numbers (numbers with a decimal point), and also types for the values true and false – char, int, float, and bool. All of these basic types are known as primitive types.
Java classes, as you probably already know, are created to solve object oriented problems. Any object of a class has the type “class”. Because an object of a class is more complex than a simple integer, boolean, or other “primitive” type, a variable naming an object is known to be a class type.
2.) How is it possible for two String objects with identical values not to be equal when compared with the == operator?
Answer : The == operator compares two objects to determine if they are the same object in memory. It is possible for two String objects to have the same value, but located indifferent areas of memory.
Normally, when define a string instance, if using, String s = "ABC"; the existing string variable at the string pool is returned. However, if using new String("ABC") there is always one additional String object created. Besides, the new String() object is != "ABC", however, using equals, it return true.
3.) Why can a class variable of a base type be set to reference an object of a derived type, eg: Shape s = new Circle("Blue", 1.0);
Answer: That is what inheritance does for you. Circle extends Shape. If you make an array of Shape and load the array with all the derived types you can loop over the collection to invoke a common method.
Shape[] shapes = new Shape[3];
shapes[ 0 ] = new Circle("Blue", 1.0);
shapes[ 1 ] = new Square("Yellow", 2.2);
shapes[ 2 ] = new Rectangle("Clear", 14, 10 );
for( int i = 0; i< shapes.length; i++)
shapes[ i ].draw();
Because all derived types would have a draw method, the Runtime knows which method of the many choices to invoke. The Circle would have a different draw method than the Rectangle. This is polymorphism.
The class for Circle might look like this...
class Circle extends Shape {
public Circle( String color, double radius ) {
super( color, radius );
The constructor of Circle invokes the constructor of Shape.
4.) How can the methods of a derived class access private instance variables of its base class?
Answer :
A private class member or constructor is accessible only within the body of the top level class that encloses the declaration of the member or constructor. It is not inherited by subclasses.
But regardless of this language restriction, you can access private fields through reflection:
Field privateStringField =
MyClass.class.getDeclaredField("privateString");
privateStringField.setAccessible(true);
String fieldValue = (String) privateStringField.get(privateObject);
System.out.println("fieldValue = " + fieldValue);
5.) How is the caller's flow of control (sequence of statement execution) different when an exception is thrown from a method that it calls, vs. when the method returns normally?
Answer :
The following try/catch has code to explicitly handle any
IllegalArgumentException thrown. Show the modified code that
will also catch any other type of exception that could be thrown
by code called within the try block.
try
{
... methods called here ...
...
}
catch (IllegalArgumentException e)
{
... code to handle IllegalArgumentException ...
}
Well it looks to me that all that needs modification is the catch block. Since it should just catch all exceptions it shouldn't matter what methods are within the try. I saw this code posted in a tutorial for "the catch-any clause":
try
{
// ...
}
catch(Exception ex)
{
// Exception handling code for ANY exception
}
try
{
// ...
}
catch(Exception ex)
{
// Exception handling code for ANY exception
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.