Set - 7

Question 1 :

What is the difference between a Scrollbar and a ScrollPane?

Answer :

A Scrollbar is a Component, but not a Container. A ScrollPane is a Container. A ScrollPane handles its own events and performs its own scrolling.


Question 2 :

What is the difference between a public and a non-public class?

Answer :

A public class may be accessed outside of its package. A non-public class may not be accessed outside of its package.


Question 3 :

To what value is a variable of the boolean type automatically initialized?

Answer :

The default value of the boolean type is false.


Question 4 :

Can try statements be nested?

Answer :

Try statements may be tested.


Question 5 :

What is the difference between the prefix and postfix forms of the ++ operator?

Answer :

The prefix form performs the increment operation and returns the value of the increment operation. The postfix form returns the current value all of the expression and then performs the increment operation on that value.


Question 6 :

What is the purpose of a statement block?

Answer :

A statement block is used to organize a sequence of statements as a single statement group.


Question 7 :

What is a Java package and how is it used?

Answer :

A Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces. Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces.


Question 8 :

What modifiers may be used with a top-level class?

Answer :

A top-level class may be public, abstract, or final.


Question 9 :

What are the Object and Class classes used for?

Answer :

The Object class is the highest-level class in the Java class hierarchy. The Class class is used to represent the classes and interfaces that are loaded by a Java program.


Question 10 :

How does a try statement determine which catch clause should be used to handle an exception?

Answer :

When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception is executed. The remaining catch clauses are ignored.


Question 11 :

Can an unreachable object become reachable again?

Answer :

An unreachable object may become reachable again. This can happen when the object's finalize() method is invoked and the object performs an operation which causes it to become accessible to reachable objects.


Question 12 :

When is an object subject to garbage collection?

Answer :

An object is subject to garbage collection when it becomes unreachable to the program in which it is used.


Question 13 :

What method must be implemented by all threads?

Answer :

All tasks must implement the run() method, whether they are a subclass of Thread or implement the Runnable interface.


Question 14 :

What methods are used to get and set the text label displayed by a Button object?

Answer :

getLabel() and setLabel()


Question 15 :

Which Component subclass is used for drawing and painting?

Answer :

Canvas


Question 16 :

What are the two basic ways in which classes that can be run as threads may be defined?

Answer :

A thread class may be declared as a subclass of Thread, or it may implement the Runnable interface.


Question 17 :

What are the problems faced by Java programmers who don't use layout managers?

Answer :

Without layout managers, Java programmers are faced with determining how their GUI will be displayed across multiple windowing systems and finding a common sizing and positioning that will work within the constraints imposed by each windowing system.


Question 18 :

What is the difference between an if statement and a switch statement?

Answer :

The if statement is used to select among two alternatives. It uses a Boolean expression to decide which alternative should be executed. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.


Question 19 :

Can there be an abstract class with no abstract methods in it?

Answer :

yes.


Question 20 :

Can an Interface be final?

Answer :

yes.


Question 21 :

Can an Interface have an inner class?

Answer :

Yes

public interface abc {
 static int i=0;
 void dd();
 class a1 {
  a1() {
   int j;
   System.out.println("in interfia");
  };
  public static void main(String a1[]) {
   System.out.println("in interfia");
  }
 }
}


Question 22 :

Can we define private and protected modifiers for variables in interfaces?

Answer :

Yes.


Question 23 :

What is Externalizable?

Answer :

Externalizable is an Interface that extends Serializable Interface. And sends data into Streams in Compressed Format.
It has two methods, writeExternal(ObjectOuput out) and readExternal(ObjectInput in)


Question 24 :

What modifiers are allowed for methods in an Interface?

Answer :

Only public and abstract modifiers are allowed for methods in interfaces.


Question 25 :

What is a local, member and a class variable?

Answer :

Variables declared within a method are "local" variables.
Variables declared within the class i.e not within any methods are "member" variables (global variables).
Variables declared within the class i.e not within any methods and are defined as "static" are class variables


Question 26 :

I made my class Cloneable but I still get 'Can't access protected method clone. Why?

Answer :

Yeah, some of the Java books, in particular "The Java Programming Language", imply that all you have to do in order to have your class support clone() is implement the Cloneable interface. Not so. Perhaps that was the intent at some point, but that's not the way it works currently. As it stands, you have to implement your own public clone() method, even if it doesn't do anything special and just calls super.clone().


Question 27 :

What are the different identifier states of a Thread?

Answer :

The different identifiers of a Thread are:
R - Running or runnable thread
S - Suspended thread
CW - Thread waiting on a condition variable
MW - Thread waiting on a monitor lock
MS - Thread suspended waiting on a monitor lock


Question 28 :

What are some alternatives to inheritance?

Answer :

Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often safer than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesn't force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass).


Question 29 :

Why isn't there operator overloading?

Answer :

Because C++ has proven by example that operator overloading makes code almost impossible to maintain. In fact there very nearly wasn't even method overloading in Java, but it was thought that this was too useful for some very basic methods like print(). Note that some of the classes like DataOutputStream have unoverloaded methods like writeInt() and writeByte().


Question 30 :

What does it mean that a method or field is "static"?

Answer :

Static variables and methods are instantiated only once per class. In other words they are class variables, not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class.
Static methods can be referenced with the name of the class rather than the name of a particular object of the class (though that works too). That's how library methods like System.out.println() work. out is a static field in the java.lang.System class.


Question 31 :

Why do threads block on I/O?

Answer :

Threads block on i/o (that is enters the waiting state) so that other threads may execute while the i/o Operation is performed.


Question 32 :

What is synchronization and why is it important?

Answer :

With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value. This often leads to significant errors.


Question 33 :

Is null a keyword?

Answer :

The null value is not a keyword.


Question 34 :

Which characters may be used as the second character of an identifier,but not as the first character of an identifier?

Answer :

The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier.


Question 35 :

What is the difference between notify() and notifyAll()?

Answer :

notify() is used to unblock one waiting thread; notifyAll() is used to unblock all of them. Using notify() is preferable (for efficiency) when only one blocked thread can benefit from the change (for example, when freeing a buffer back into a pool). notifyAll() is necessary (for correctness) if multiple threads should resume (for example, when releasing a "writer" lock on a file might permit all "readers" to resume).


Question 36 :

Why can't I say just abs() or sin() instead of Math.abs() and Math.sin()?

Answer :

The import statement does not bring methods into your local name space. It lets you abbreviate class names, but not get rid of them altogether. That's just the way it works, you'll get used to it. It's really a lot safer this way.
However, there is actually a little trick you can use in some cases that gets you what you want. If your top-level class doesn't need to inherit from anything else, make it inherit from java.lang.Math. That *does* bring all the methods into your local name space. But you can't use this trick in an applet, because you have to inherit from java.awt.Applet. And actually, you can't use it on java.lang.Math at all, because Math is a "final" class which means it can't be extended.


Question 37 :

Why are there no global variables in Java?

Answer :

Global variables are considered bad form for a variety of reasons: · Adding state variables breaks referential transparency (you no longer can understand a statement or expression on its own: you need to understand it in the context of the settings of the global variables).
· State variables lessen the cohesion of a program: you need to know more to understand how something works. A major point of Object-Oriented programming is to break up global state into more easily understood collections of local state.
· When you add one variable, you limit the use of your program to one instance. What you thought was global, someone else might think of as local: they may want to run two copies of your program at once.
For these reasons, Java decided to ban global variables.


Question 38 :

What does it mean that a class or member is final?

Answer :

A final class can no longer be subclassed. Mostly this is done for security reasons with basic classes like String and Integer. It also allows the compiler to make some optimizations, and makes thread safety a little easier to achieve. Methods may be declared final as well. This means they may not be overridden in a subclass.
Fields can be declared final, too. However, this has a completely different meaning. A final field cannot be changed after it's initialized, and it must include an initializer statement where it's declared. For example,

public final double c = 2.998;

It's also possible to make a static field final to get the effect of C++'s const statement or some uses of C's #define, e.g.

public static final double c = 2.998;


Question 39 :

What does it mean that a method or class is abstract?

Answer :

An abstract class cannot be instantiated. Only its subclasses can be instantiated. You indicate that a class is abstract with the abstract keyword like this:
public abstract class Container extends Component {
Abstract classes may contain abstract methods. A method declared abstract is not actually implemented in the current class. It exists only to be overridden in subclasses. It has no body. For example,

public abstract float price();

Abstract methods may only be included in abstract classes. However, an abstract class is not required to have any abstract methods, though most of them do.
Each subclass of an abstract class must override the abstract methods of its superclasses or itself be declared abstract.


Question 40 :

What is the main difference between Java platform and other platforms?

Answer :

The Java platform differs from most other platforms in that it's a software-only platform that runs on top of other hardware-based platforms.
The Java platform has three elements:
Java programming language
The Java Virtual Machine (Java VM)
The Java Application Programming Interface (Java API)


Question 41 :

What is the Java Virtual Machine?

Answer :

The Java Virtual Machine is a software that can be ported onto various hardware-based platforms.


Question 42 :

What is the Java API?

Answer :

The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets.


Question 43 :

What is the package?

Answer :

The package is a Java namespace or part of Java libraries. The Java API is grouped into libraries of related classes and interfaces; these libraries are known as packages.


Question 44 :

What is native code?

Answer :

The native code is code that after you compile it, the compiled code runs on a specific hardware platform.


Question 45 :

Explain the user defined Exceptions?

Answer :

User defined Exceptions are the separate Exception classes defined by the user for specific purposed. An user defined can created by simply sub-classing it to the Exception class. This allows custom exceptions to be generated (using throw) and caught in the same way as normal exceptions.
Example:

class myCustomException extends Exception {
 // The class simply has to exist to be an exception
}


Question 46 :

Is Java code slower than native code?

Answer :

Not really. As a platform-independent environment, the Java platform can be a bit slower than native code. However, smart compilers, well-tuned interpreters, and just-in-time bytecode compilers can bring performance close to that of native code without threatening portability.


Question 47 :

Can main() method be overloaded?

Answer :

Yes. the main() method is a special method for a program entry. You can overload main() method in any ways. But if you change the signature of the main method, the entry point for the program will be gone.


Question 48 :

What is the serialization?

Answer :

The serialization is a kind of mechanism that makes a class or a bean persistence by having its properties or fields and state information saved and restored to and from storage.


Question 49 :

Explain the new Features of JDBC 2.0 Core API?

Answer :

The JDBC 2.0 API includes the complete JDBC API, which includes both core and Optional Package API, and provides inductrial-strength database computing capabilities.
New Features in JDBC 2.0 Core API:

Scrollable result sets- using new methods in the ResultSet interface allows programmatically move the to particular row or to a position relative to its current position
JDBC 2.0 Core API provides the Batch Updates functionality to the java applications.
Java applications can now use the ResultSet.updateXXX methods.
New data types - interfaces mapping the SQL3 data types
Custom mapping of user-defined types (UTDs)
Miscellaneous features, including performance hints, the use of character streams, full precision for java.math.BigDecimal values, additional security, and support for time zones in date, time, and timestamp values.


Question 50 :

How you can force the garbage collection?

Answer :

Garbage collection automatic process and can't be forced.


Question 51 :

Explain garbage collection?

Answer :

Garbage collection is one of the most important feature of Java. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. User program cann't directly free the object from memory, instead it is the job of the garbage collector to automatically free the objects that are no longer referenced by a program. Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. In Java, it is good idea to explicitly assign null into a variable when no more in use. I Java on calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected.


Question 52 :

Describe the principles of OOPS.

Answer :

There are three main principals of oops which are called Polymorphism, Inheritance and Encapsulation.


Question 53 :

Explain the Encapsulation principle.

Answer :

Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.


Question 54 :

Explain the Inheritance principle.

Answer :

Inheritance is the process by which one object acquires the properties of another object.


Question 55 :

Explain the Polymorphism principle.

Answer :

The meaning of Polymorphism is something like one name many forms. Polymorphism enables one entity to be used as as general category for different types of actions. The specific action is determined by the exact nature of the situation. The concept of polymorphism can be explained as "one interface, multiple methods".


Question 56 :

Explain the different forms of Polymorphism.

Answer :

From a practical programming viewpoint, polymorphism exists in three distinct forms in Java:

Method overloading
Method overriding through inheritance
Method overriding through the Java interface


Question 57 :

What are Access Specifiers available in Java?

Answer :

ccess specifiers are keywords that determines the type of access to the member of a class. These are:

Public
Protected
Private
Defaults


Question 58 :

Describe the wrapper classes in Java?

Answer :

Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type.
Following table lists the primitive types and the corresponding wrapper classes:

Primitive Wrapper
boolean java.lang.Boolean
byte java.lang.Byte
char java.lang.Character
double java.lang.Double
float java.lang.Float
int java.lang.Integer
long java.lang.Long
short java.lang.Short
void java.lang.Void


Question 59 :

Use the Externalizable interface when you need complete control over your Bean's serialization (for example, when writing and reading a specific file format).

Answer :

No. Earlier order is maintained.


Question 60 :

Read the following program:

public class test {
	public static void main(String [] args) {
		int x = 3;
		int y = 1;
		if (x = y)
			System.out.println("Not equal");
		else
			System.out.println("Equal");
	}
}

What is the result?


A). The output is "Equal"
B). The output in "Not Equal"
C). An error at " if (x = y)" causes compilation to fall.
D). The program executes but no output is show on console.
Answer : Option C