core java interview quesitons part 4
Q - The Java read() method reads and returns a single byte
from the standard input device. It
stores that byte
according to what type. What does the method return if the user enters an eof?
A - The Java read() method reads and returns a single byte
from the standard input device and stores that byte
in an integer. It returns an integer value of -1 if the user
enters an eof.
Q - What keystroke combination can be used to simulate an
eof at the keyboard of a DOS system?
A - An eof can be simulated on a DOS system keyboard by
holding down the ctrl key and pressing the z key.
Q - Provide a Java code fragment illustrating how you would
read a stream of bytes from the
standard input device until encountering an eof and quit
reading when the eof is encountered.
A - The following Java code fragment will read a stream of
bytes from the standard input device until
encountering an eof.
while(System.in.read() != -1) { //do something
}
This code fragment accesses the read()method of the object
referred to by the class variable named in of the
class named System.
Q - Provide a Java code fragment illustrating two different
ways to display a String argument on the
Java standard output device. Explain how your code works in
object-oriented terms. Make certain
that you explain the difference between the two.
A - The following two code fragments will each display a
string argument on the Java standard output device.
System.out.println("String
argument")
System.out.print("String argument")
In the first case, the code fragment accesses the println()
method of the object referred to by the class variable
named out of the class named System. In the second case, the
print() method is accessed instead of the println() method.
The difference between the two is that the println() method
automatically inserts a newlineat the end of the
string argument whereas the print() method leaves the
display cursor at the end of the string argument.
===============
OOP
Q - In Object-Oriented Programming, an object is often said
to be an ____________ of a class.
A - An object is often said to be an instance of a class.
Q - In Object-Oriented Programming, an object is often said
to have s_______ and b_______.
Provide the missing words which begin with the letters
shown.
A - In OOP, an object is often said to have state and
behavior.
Q - An object's state is contained in its ________ and its
behavior is implemented through its
________.
A - An object's state is contained in its member variables (
or data members) and its behavior is implemented through its methods ( or
member functions).
Q - The member variables of an object can be either
____________ or _________ .
A - Its member variables can be either instance variables or
class variables.
Q - What is generally meant by the terminology "sending
a message to an object?"
A - We activate the behavior of an object by invoking one of
its methods (sending it a message).
Q - What are the two things that can usually happen when an
object receives a message?
A - When an object receives a message, it usually either
performs an action, or modifies its state, or both.
Q - What happens to the memory occupied by an object in Java
when the object is no longer
needed, and what do we normally do to make that happen?
A - When an object is no longer needed in Java, we simply
forget it. Eventually, the garbage collector may (or may not) come by and pick
it up for recycling.
Q - Identify as the stages of an object's life?
A - The stages of an Object's life are:
Creation
Use
Cleanup
Q - The creation of
an object involves three steps (which are often combined). What are the three
steps?
A - The three steps are:
declaration
(providing a name for the object)
instantiation
(setting aside memory for the object)
optional
initialization (providing initial values for the object's instance variables)
Q - Java allows the instantiation of variables of primitive
types in dynamic memory: True or False?
If false, explain why and what you might be able to do to
achieve almost the same result.
A - False. Java does not allow the instantiation of
primitive variables in dynamic memory. (However, there are wrapper classes for
primitive types which can be used to turn them into objects for this purpose.)
Q - An array of objects in Java is instantiated as an array
of reference variables where each
reference variable can then be used to instantiate an object
pointed to by the reference variable:
True or False. If false, explain why and either provide a
code fragment that illustrates your answer
A - True.
Q - In Java, it is always necessary to declare (give a name
to) all new objects: True or False? If
false, explain why and either provide a code fragment that
illustrates your answer
A - It is not always necessary in Java to declare an object
(to give it a name). Consider, for example a case where a
0 comments:
Post a Comment