Core Java – III

Q: Explain the use of sublass in a Java program?
A: Sub class inherits all the public and protected methods and the implementation. It also inherits all the default modifier methods and their implementation.

Q: What’s the difference between constructors and other methods?
A: Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.

Q: Is there any limitation of using Inheritance?
A: Yes, since inheritance inherits everything from the super class and interface, it may make the subclass too clustering and sometimes error-prone when dynamic overriding or dynamic overloading in some situation.

Q: When is the ArrayStoreException thrown?
A: When copying elements between different arrays, if the source or destination arguments are not arrays or their types are not compatible, an ArrayStoreException will be thrown.

Q: Can you call one constructor from another if a class has multiple constructors?
A: Yes, use this() syntax.

Q: What’s the difference between the methods sleep() and wait()?
A: The code sleep(2000); puts thread aside for exactly two seconds. The code wait(2000), causes a wait of up to two second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.

Q: When ArithmeticException is thrown?
A: The ArithmeticException is thrown when integer is divided by zero or taking the remainder of a number by zero. It is never thrown in floating-point operations.

Q: What is a transient variable?
A: A transient variable is a variable that may not be serialized during Serialization and which is initialized by its default value during de-serialization,

Q: What is synchronization?
A: Synchronization is the capability to control the access of multiple threads to shared resources. synchronized keyword in java provides locking which ensures mutual exclusive access of shared resource and prevent data race.

Q: What is the Collections API?
A: The Collections API is a set of classes and interfaces that support operations on collections of objects.

Q: Does garbage collection guarantee that a program will not run out of memory?
A: Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection.

Q: The immediate superclass of the Applet class?
A: Panel is the immediate superclass. A panel provides space in which an application can attach any other component, including other panels.

Q: Which Java operator is right associative?
A: The = operator is right associative.

Q: What is the difference between a break statement and a continue statement?
A: A break statement results in the termination of the statement to which it applies (switch, for, do, or while). A continue statement is used to end the current loop iteration and return control to the loop statement.

Q: If a variable is declared as private, where may the variable be accessed?
A: A private variable may only be accessed within the class in which it is declared.

Q: What is the purpose of the System class?
A: The purpose of the System class is to provide access to system resources.

Q: List primitive Java types?
A: The eight primitive types are byte, char, short, int, long, float, double, and boolean.

Q: What is the relationship between clipping and repainting under AWT?
A: When a window is repainted by the AWT painting thread, it sets the clipping regions to the area of the window that requires repainting.

Q: Which class is the immediate superclass of the Container class?
A: Component class is the immediate super class.

Q: What class of exceptions are generated by the Java run-time system?
A: The Java runtime system generates RuntimeException and Error exceptions.

Q: Under what conditions is an object’s finalize() method invoked by the garbage collector?
A: The garbage collector invokes an object’s finalize() method when it detects that the object has become unreachable.

Q: How can a dead thread be restarted?
A: A dead thread cannot be restarted.

Q: Which arithmetic operations can result in the throwing of an ArithmeticException?
A: Integer / and % can result in the throwing of an ArithmeticException.

Q: Variable of the boolean type is automatically initialized as?
A: The default value of the boolean type is false.

Q: Can try statements be nested?
A: Yes

Q: What are ClassLoaders?
A: A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class.

Q: What is the difference between an Interface and an Abstract class?
A: An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation.

Q: What will happen if static modifier is removed from the signature of the main method?
A: Program throws “NoSuchMethodError” error at runtime .

Q: What is the default value of an object reference declared as an instance variable?
A: Null, unless it is defined explicitly.

Q: Can a top level class be private or protected?
A: No, a top level class can not be private or protected. It can have either “public” or no modifier.

Q: Why do we need wrapper classes?
A: We can pass them around as method parameters where a method expects an object. It also provides utility methods.

Q: What is the difference between error and an exception?
A: An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. Exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist.

Q: Is it necessary that each try block must be followed by a catch block?
A: It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block or a finally block.

Q: When a thread is created and started, what is its initial state?
A: A thread is in the ready state as initial state after it has been created and started.

Q: What is the Locale class?
A: The Locale class is used to tailor program output to the conventions of a particular geographic, political, or cultural region.

Q: What are synchronized methods and synchronized statements?
A: Synchronized methods are methods that are used to control access to an object. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.

Q: What is runtime polymorphism or dynamic method dispatch?
A: Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass.

Q: What is Dynamic Binding(late binding)?
A: Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run-time.

Q: Can constructor be inherited?
A: No, constructor cannot be inherited.

Q: What are the advantages of ArrayList over arrays?
A: ArrayList can grow dynamically and provides more powerful insertion and search mechanisms than arrays.

Q: Why deletion in LinkedList is fast than ArrayList?
A: Deletion in linked list is fast because it involves only updating the next pointer in the node before the deleted node and updating the previous pointer in the node after the deleted node.

Q: How do you decide when to use ArrayList and LinkedList?
A: If you need to frequently add and remove elements from the middle of the list and only access the list elements sequentially, then LinkedList should be used. If you need to support random access, without inserting or removing elements from any place other than the end, then ArrayList should be used.

Q: What is a Values Collection View ?
A: It is a collection returned by the values() method of the Map Interface, It contains all the objects present as values in the map.

Q: What is dot operator?
A: The dot operator(.) is used to access the instance variables and methods of class objects.It is also used to access classes and sub-packages from a package.

Q: Where and how can you use a private constructor?
A: Private constructor is used if you do not want other classes to instantiate the object and to prevent subclassing.T

Q: What is type casting?
A: Type casting means treating a variable of one type as though it is another type.

Q: Describe life cycle of thread?
A: A thread is a execution in a program. The life cycle of a thread include:

  • Newborn state
  • Runnable state
  • Running state
  • Blocked state
  • Dead state

Q: What is the difference between the >> and >>> operators?
A: The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out.

Q: Which method of the Component class is used to set the position and size of a component?
A: setBounds() method is used for this purpose.

Q: What is the range of the short type?
A: The range of the short type is -(2^15) to 2^15 – 1.

Q: What is the immediate superclass of Menu?
A: MenuItem class

Q: Does Java allow Default Arguments?
A: No, Java does not allow Default Arguments.

Q: Which number is denoted by leading zero in java?
A: Octal Numbers are denoted by leading zero in java, example: 06

Q: Which number is denoted by leading 0x or 0X in java?
A: Hexadecimal Numbers are denoted by leading 0x or 0X in java, example: 0XF

Q: Break statement can be used as labels in Java?
A: Yes, an example can be break one;

Q: Where import statement is used in a Java program?
A: Import statement is allowed at the beginning of the program file after package statement.

Q: Explain suspend() method under Thread class>
A: It is used to pause or temporarily stop the execution of the thread.

Q: Explain isAlive() method under Thread class?
A: It is used to find out whether a thread is still running or not.

Q: What is currentThread()?
A: It is a public static method used to obtain a reference to the current thread.

Q: Explain main thread under Thread class execution?
A: The main thread is created automatically and it begins to execute immediately when a program starts. It ia thread from which all other child threads originate.

Q: Life cycle of an applet includes which steps?
A: Life cycle involves the following steps:

  • Initialization
  • Starting
  • Stopping
  • Destroying
  • Painting

Q: Why is the role of init() method under applets?
A: It initializes the applet and is the first method to be called.

Q: Which method is called by Applet class to load an image?
A: getImage(URL object, filename) is used for this purpose.

Q: Define code as an attribute of Applet?
A: It is used to specify the name of the applet class.

Q: Define canvas?
A: It is a simple drawing surface which are used for painting images or to perform other graphical operations.

Q: Define Network Programming?
A: It refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network.

Q: What is a Socket?
A: Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its end of the communication and attempts to connect that socket to a server.

Q: Advantages of Java Sockets?
A: Sockets are flexible and sufficient. Efficient socket based programming can be easily implemented for general communications. It cause low network traffic.

Q: Disadvantages of Java Sockets?
A: Socket based communications allows only to send packets of raw data between applications. Both the client-side and server-side have to provide mechanisms to make the data useful in any way.

Q: Which class is used by server applications to obtain a port and listen for client requests?
A: class is used by server applications to obtain a port and listen for client requests

Q: Which class represents the socket that both the client and server use to communicate with each other?
A: class represents the socket that both the client and server use to communicate with each other.

Q: Why Generics are used in Java?
A: Generics provide compile-time type safety that allows programmers to catch invalid types at compile time. Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods or, with a single class declaration, a set of related types.

Q: What environment variables do I need to set on my machine in order to be able to run Java programs?
A: CLASSPATH and PATH are the two variables.

Q: Is there any need to import java.lang package?
A: No, there is no need to import this package. It is by default loaded internally by the JVM.

Q: What is Nested top-level class?
A: If a class is declared within a class and specify the static modifier, the compiler treats the class just like any other top-level class. Nested top-level class is an Inner class.

Q: What is Externalizable interface?
A: Externalizable is an interface which contains two methods readExternal and writeExternal. These methods give you a control over the serialization mechanism.

Q: If System.exit (0); is written at the end of the try block, will the finally block still execute?
A: No in this case the finally block will not execute because when you say System.exit (0); the control immediately goes out of the program, and thus finally never executes.

Q: What is daemon thread?
A: Daemon thread is a low priority thread, which runs intermittently in the back ground doing the garbage collection operation for the java runtime system.

Q: Which method is used to create the daemon thread?
A: setDaemon method is used to create a daemon thread.

Q: Which method must be implemented by all threads?
A: All tasks must implement the run() method

Q: What is the GregorianCalendar class?
A: The GregorianCalendar provides support for traditional Western calendars

Q: What is the SimpleTimeZone class?
A: The SimpleTimeZone class provides support for a Gregorian calendar .

Q: What is the difference between the size and capacity of a Vector?
A: The size is the number of elements actually stored in the vector, while capacity is the maximum number of elements it can store at a given instance of time.

Q: Can a vector contain heterogenous objects?
A: Yes a Vector can contain heterogenous objects. Because a Vector stores everything in terms of Object.

Q: What is an enumeration?
A: An enumeration is an interface containing methods for accessing the underlying data structure from which the enumeration is obtained. It allows sequential access to all the elements stored in the collection.

Q: What is difference between Path and Classpath?
A: Path and Classpath are operating system level environment variales. Path is defines where the system can find the executables(.exe) files and classpath is used to specify the location of .class files.

Q: Can a class declared as private be accessed outside it’s package?
A: No, it’s not possible to accessed outside it’s package.

Q: What are the restriction imposed on a static method or a static block of code?
A: A static method should not refer to instance variables without creating an instance and cannot use “this” operator to refer the instance.

Q: Can an Interface extend another Interface?
A: Yes an Interface can inherit another Interface, for that matter an Interface can extend more than one Interface.

Q: Which object oriented Concept is achieved by using overloading and overriding?
A: Polymorphism

Q: What is an object’s lock and which object’s have locks?
A: An object’s lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object’s lock.

Q: What is Downcasting?
A: It is the casting from a general to a more specific type, i.e. casting down the hierarchy.

Q: What are order of precedence and associativity and how are they used?
A: Order of precedence determines the order in which operators are evaluated in expressions. Associatity determines whether an expression is evaluated left-to-right or right-to-left.

Q: If a method is declared as protected, where may the method be accessed?
A: A protected method may only be accessed by classes or interfaces of the same package or by subclasses of the class in which it is declared.

Q: What is the difference between inner class and nested class?
A: When a class is defined within a scope of another class, then it becomes inner class. If the access modifier of the inner class is static, then it becomes nested class.

Q: What restrictions are placed on method overriding?
A: Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides.

Q: What is constructor chaining and how is it achieved in Java?
A: A child object constructor always first needs to construct its parent. In Java it is done via an implicit call to the no-args constructor as the first statement.

Q: Can a double value be cast to a byte?
A: Yes, a double value can be cast to a byte.

Q: How does a try statement determine which catch clause should be used to handle an exception?
A: 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.

Q: What will be the default values of all the elements of an array defined as an instance variable?
A: If the array is an array of primitive types, then all the elements of the array will be initialized to the default value corresponding to that primitive type.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s