Java Viva Questions
Java Viva Questions
COM
WWW.VIDYARTHIPLUS.COM GUNJAN
WWW.VIDYARTHIPLUS.COM
A: It is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an
exception occurs. These exceptions cannot simply be ignored at the time of compilation.
Q.37: Explain Runtime Exceptions?
A: It is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of
compliation.
Q.38: Which are the two subclasses under Exception class?
A: The Exception class has two main subclasses : IOException class and RuntimeException Class.
Q.39: When throws keyword is used?
A: If a method does not handle a checked exception, the method must declare it using the throwskeyword. The throws keyword appears at the end of a method's signature.
Q.40: When throw keyword is used?
A: An exception can be thrown, either a newly instantiated one or an exception that you just caught, by using throw keyword.
Q.41: How finally used under Exception Handling?
A: The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.
Q.42: What things should be kept in mind while creating your own exceptions in Java?
A: While creating your own exception:
WWW.VIDYARTHIPLUS.COM GUNJAN
WWW.VIDYARTHIPLUS.COM
A: It includes:
WWW.VIDYARTHIPLUS.COM GUNJAN
WWW.VIDYARTHIPLUS.COM
A: The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.
Q94: Which class should you use to obtain design information about an object?
A: The Class class is used to obtain information about an object's design and java.lang.Class class instance represent classes, interfaces in a running Java application.
Q95: What is the difference between static and non-static variables?
A: A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.
Q96: What is Serialization and deserialization?
A: Serialization is the process of writing the state of an object to a byte stream. Deserialization is the process of restoring these objects.
Q97: What are use cases?
A: It is part of the analysis of a program and describes a situation that a program might encounter and what behavior the program should exhibit in that circumstance.
Q98: 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.
Q99: How to add menushortcut to menu item?
A: If there is a button instance called b1, you may add menu short cut by calling b1.setMnemonic('F'), so the user may be able to use Alt+F to click the button.
Q100: Can you write a Java class that could be used both as an applet as well as an application?
A: Yes, just add a main() method to the applet.
Q101: What is the difference between Swing and AWT components?
A: AWT components are heavy-weight, whereas Swing components are lightweight. Heavy weight components depend on the local windowing toolkit. For example,
java.awt.Button is a heavy weight component, when it is running on the Java platform for Unix platform, it maps to a real Motif button.
Q102: 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.
Q103: 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.
Q104: 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.
Q105: Can you call one constructor from another if a class has multiple constructors?
A: Yes, use this() syntax.
Q106: 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.
Q106: 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.
Q107: 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,
Q108: 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.
Q109: What is the Collections API?
A: The Collections API is a set of classes and interfaces that support operations on collections of objects.
Q110: 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.
Q111: 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.
Q112: Which Java operator is right associative?
A: The = operator is right associative.
Q113: 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.
Q114: 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.
Q115: What is the purpose of the System class?
A: The purpose of the System class is to provide access to system resources.
Q116: List primitive Java types?
A: The eight primitive types are byte, char, short, int, long, float, double, and boolean.
Q117: 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.
Q118: Which class is the immediate superclass of the Container class?
A: Component class is the immediate super class.
Q119: What class of exceptions are generated by the Java run-time system?
A: The Java runtime system generates RuntimeException and Error exceptions.
Q120: 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.
Q121: How can a dead thread be restarted?
A: A dead thread cannot be restarted.
Q122: Which arithmetic operations can result in the throwing of an ArithmeticException?
A: Integer / and % can result in the throwing of an ArithmeticException.
Q123: Variable of the boolean type is automatically initialized as?
A: The default value of the boolean type is false.
Q124: Can try statements be nested?
A: Yes
Q125: What are ClassLoaders?
A: A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class.
Q126: 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.
Q127: What will happen if static modifier is removed from the signature of the main method?
A: Program throws "NoSuchMethodError" error at runtime .
Q128: What is the default value of an object reference declared as an instance variable?
A: Null, unless it is defined explicitly.
Q129: 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.
Q130: 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.
WWW.VIDYARTHIPLUS.COM GUNJAN
WWW.VIDYARTHIPLUS.COM
Newborn state
Runnable state
Running state
Blocked state
Dead state
Q147 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.
Q148: 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.
Q149: What is the range of the short type?
A: The range of the short type is -(2^15) to 2^15 - 1.
Q150: What is the immediate superclass of Menu?
A: MenuItem class
Q151: Does Java allow Default Arguments?
A: No, Java does not allow Default Arguments.
Q152: Which number is denoted by leading zero in java?
A: Octal Numbers are denoted by leading zero in java, example: 06
Q153: 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
Q154 Break statement can be used as labels in Java?
A: Yes, an example can be break one;
Q155: Where import statement is used in a Java program?
A: Import statement is allowed at the beginning of the program file after package statement.
Q156: Explain suspend() method under Thread class>
A: It is used to pause or temporarily stop the execution of the thread.
Q157: Explain isAlive() method under Thread class?
A: It is used to find out whether a thread is still running or not.
Q158: What is currentThread()?
A: It is a public static method used to obtain a reference to the current thread.
Q159: 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.
Q160: Life cycle of an applet includes which steps?
A: Life cycle involves the following steps:
Initialization
Starting
Stopping
Destroying
WWW.VIDYARTHIPLUS.COM GUNJAN
WWW.VIDYARTHIPLUS.COM
Painting
WWW.VIDYARTHIPLUS.COM GUNJAN
WWW.VIDYARTHIPLUS.COM
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.
Q199: 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.
WWW.VIDYARTHIPLUS.COM GUNJAN