This document provides concise summaries of key Java concepts in response to common interview questions:
1. It summarizes data types that can be used as switch statement cases, interface modifiers, differences between while and do loops, and purpose of the File class.
2. It describes how exceptions can be rethrown, when the compiler supplies a default constructor, and restrictions on method overloading.
3. It explains the difference between field and local variables, how this() and super() are used in constructors, and what transient variables are.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online from Scribd
This document provides concise summaries of key Java concepts in response to common interview questions:
1. It summarizes data types that can be used as switch statement cases, interface modifiers, differences between while and do loops, and purpose of the File class.
2. It describes how exceptions can be rethrown, when the compiler supplies a default constructor, and restrictions on method overloading.
3. It explains the difference between field and local variables, how this() and super() are used in constructors, and what transient variables are.
This document provides concise summaries of key Java concepts in response to common interview questions:
1. It summarizes data types that can be used as switch statement cases, interface modifiers, differences between while and do loops, and purpose of the File class.
2. It describes how exceptions can be rethrown, when the compiler supplies a default constructor, and restrictions on method overloading.
3. It explains the difference between field and local variables, how this() and super() are used in constructors, and what transient variables are.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online from Scribd
This document provides concise summaries of key Java concepts in response to common interview questions:
1. It summarizes data types that can be used as switch statement cases, interface modifiers, differences between while and do loops, and purpose of the File class.
2. It describes how exceptions can be rethrown, when the compiler supplies a default constructor, and restrictions on method overloading.
3. It explains the difference between field and local variables, how this() and super() are used in constructors, and what transient variables are.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online from Scribd
Download as docx, pdf, or txt
You are on page 1of 6
INFOSYS POWER PREPARATION
Good questions asked during Java interview
Is 'abc a primitive value? - The String literal 'abc is not a primitive value. It is a String object. " What restrictions are placed on the values oI each case oI a switch statement? - During compilation, the values oI each case oI a switch statement must evaluate to a value that can be promoted to an int value. What modiIiers may be used with an interIace declaration? - An interIace may be declared as public or abstract. Is a class a subclass oI itselI? - A class is a subclass oI itselI. What is the diIIerence between a while statement and a do statement? - A while statement checks at the beginning oI a loop to see whether the next loop iteration should occur. A do statement checks at the end oI a loop to see whether the next iteration oI a loop should occur. The do statement will always execute the body oI a loop at least once. What modiIiers can be used with a local inner class? - A local inner class may be Iinal or abstract. What is the purpose oI the File class? - The File class is used to create objects that provide access to the Iiles and directories oI a local Iile system. Can an exception be rethrown? - Yes, an exception can be rethrown. When does the compiler supply a deIault constructor Ior a class? - The compiler supplies a deIault constructor Ior a class iI no other constructors are provided. II a method is declared as protected, where may the method be accessed? - A protected method may only be accessed by classes or interIaces oI the same package or by subclasses oI the class in which it is declared. Which non-Unicode letter characters may be used as the Iirst character oI an identiIier? - The non-Unicode letter characters $ and may appear as the Iirst character oI an identiIier What restrictions are placed on method overloading? - Two methods may not have the same name and argument list but diIIerent return types. What is casting? - There are two types oI casting, casting between primitive numeric types and casting between object reIerences. Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. Casting between object reIerences is used to reIer to an object by a compatible class, interIace, or array type reIerence. What is the return type oI a program`s main() method? - A program`s main() method has a void return type. What class oI exceptions are generated by the Java run-time system? - The Java runtime system generates RuntimeException and Error exceptions. What class allows you to read objects directly Irom a stream? - The ObjectInputStream class supports the reading oI objects Irom input streams. What is the diIIerence between a Iield variable and a local variable? - A Iield variable is a variable that is declared as a member oI a class. A local variable is a variable that is declared local to a method. How are this() and super() used with constructors? - this() is used to invoke a constructor oI the same class. super() is used to invoke a superclass constructor. What is the relationship between a method`s throws clause and the exceptions that can be thrown during the method`s execution? - A method`s throws clause must declare any checked exceptions that are not caught within the body oI the method. Why are the methods oI the Math class static? - So they can be invoked as iI they are a mathematical code library. What are the legal operands oI the instanceoI operator? - The leIt operand is an object reIerence or null value and the right operand is a class, interIace, or array type. What an I/O Iilter? - An I/O Iilter is an object that reads Irom one stream and writes to another, usually altering the data in some way as it is passed Irom one stream to another. II an object is garbage collected, can it become reachable again? - Once an object is garbage collected, it ceases to exist. It can no longer become reachable again. What are E and PI? - E is the base oI the natural logarithm and PI is mathematical value pi. Are true and Ialse keywords? - The values true and Ialse are not keywords. What is the diIIerence between the File and RandomAccessFile classes? - The File class encapsulates the Iiles and directories oI the local Iile system. The RandomAccessFile class provides the methods needed to directly access data contained in any part oI a Iile. What happens when you add a double value to a String? - The result is a String object. What is your platIorm`s deIault character encoding? - II you are running Java on English Windows platIorms, it is probably Cp1252. II you are running Java on English Solaris platIorms, it is most likely 88591. Which package is always imported by deIault? - The java.lang package is always imported by deIault. What interIace must an object implement beIore it can be written to a stream as an object? - An object must implement the Serializable or Externalizable interIace beIore it can be written to a stream as an object. How can my application get to know when a HttpSession is removed? - DeIine a Class HttpSessionNotiIier which implements HttpSessionBindingListener and implement the Iunctionality what you need in valueUnbound() method. Create an instance oI that class and put that instance in HttpSession. Whats the diIIerence between notiIy() and notiIyAll()? - notiIy() is used to unblock one waiting thread; notiIyAll() is used to unblock all oI them. Using notiIy() is preIerable (Ior eIIiciency) when only one blocked thread can beneIit Irom the change (Ior example, when Ireeing a buIIer back into a pool). notiIyAll() is necessary (Ior correctness) iI multiple threads should resume (Ior example, when releasing a 'writer lock on a Iile might permit all 'readers to resume). Why can`t I say just abs() or sin() instead oI Math.abs() and Math.sin()? - The import statement does not bring methods into your local name space. It lets you abbreviate class names, but not get rid oI them altogether. That`s just the way it works, you`ll get used to it. It`s really a lot saIer this way. However, there is actually a little trick you can use in some cases that gets you what you want. II your top-level class doesn`t need to inherit Irom anything else, make it inherit Irom 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 Irom java.awt.Applet. And actually, you can`t use it on java.lang.Math at all, because Math is a 'Iinal class which means it can`t be extended. Why are there no global variables in Java? - Global variables are considered bad Iorm Ior a variety oI reasons: Adding state variables breaks reIerential transparency (you no longer can understand a statement or expression on its own: you need to understand it in the context oI the settings oI the global variables), State variables lessen the cohesion oI a program: you need to know more to understand how something works. A major point oI Object-Oriented programming is to break up global state into more easily understood collections oI local state, When you add one variable, you limit the use oI your program to one instance. What you thought was global, someone else might think oI as local: they may want to run two copies oI your program at once. For these reasons, Java decided to ban global variables. What does it mean that a class or member is Iinal? - A Iinal class can no longer be subclassed. Mostly this is done Ior security reasons with basic classes like String and Integer. It also allows the compiler to make some optimizations, and makes thread saIety a little easier to achieve. Methods may be declared Iinal as well. This means they may not be overridden in a subclass. Fields can be declared Iinal, too. However, this has a completely diIIerent meaning. A Iinal Iield cannot be changed aIter it`s initialized, and it must include an initializer statement where it`s declared. For example, public Iinal double c 2.998; It`s also possible to make a static Iield Iinal to get the eIIect oI C`s const statement or some uses oI C`s #deIine, e.g. public static Iinal double c 2.998; What does it mean that a method or class is abstract? - 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 Iloat price(); Abstract methods may only be included in abstract classes. However, an abstract class is not required to have any abstract methods, though most oI them do. Each subclass oI an abstract class must override the abstract methods oI its superclasses or itselI be declared abstract. What is a transient variable? - transient variable is a variable that may not be serialized. How are Observer and Observable used? - Objects that subclass the Observable class maintain a list oI observers. When an Observable object is updated it invokes the update() method oI each oI its observers to notiIy the observers that it has changed state. The Observer interIace is implemented by objects that observe Observable objects. Can a lock be acquired on a class? - Yes, a lock can be acquired on a class. This lock is acquired on the class`s Class object. What state does a thread enter when it terminates its processing? - When a thread terminates its processing, it enters the dead state. How does Java handle integer overIlows and underIlows? - It uses those low order bytes oI the result that can Iit into the size oI the type allowed by the operation. What is the diIIerence between the ~~ and ~~~ operators? - The ~~ operator carries the sign bit when shiIting right. The ~~~ zero-Iills bits that have been shiIted out. Is sizeoI a keyword? - The sizeoI operator is not a keyword. Does garbage collection guarantee that a program will not run out oI memory? - Garbage collection does not guarantee that a program will not run out oI memory. It is possible Ior programs to use up memory resources Iaster than they are garbage collected. It is also possible Ior programs to create objects that are not subject to garbage collection Can an object`s Iinalize() method be invoked while it is reachable? - An object`s Iinalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object`s Iinalize() method may be invoked by other objects. What value does readLine() return when it has reached the end oI a Iile? - The readLine() method returns null when it has reached the end oI a Iile. Can a Ior statement loop indeIinitely? - Yes, a Ior statement can loop indeIinitely. For example, consider the Iollowing: Ior(;;) ; To what value is a variable oI the String type automatically initialized? - The deIault value oI an String type is null. What is a task`s priority and how is it used in scheduling? - A task`s priority is an integer value that identiIies the relative order in which it should be executed with respect to other tasks. The scheduler attempts to schedule higher priority tasks beIore lower priority tasks. What is the range oI the short type? - The range oI the short type is -(2`15) to 2`15 - 1. What is the purpose oI garbage collection? - The purpose oI garbage collection is to identiIy and discard objects that are no longer needed by a program so that their resources may be reclaimed and reused. What do you understand by private, protected and public? - These are accessibility modiIiers. Private is the most restrictive, while public is the least restrictive. There is no real diIIerence between protected and the deIault type (also known as package protected) within the context oI the same package, however the protected keyword allows visibility to a derived class in a diIIerent package. What is Downcasting ? - Downcasting is the casting Irom a general to a more speciIic type, i.e. casting down the hierarchy Can a method be overloaded based on diIIerent return type but same argument type ? - No, because the methods can be called without using their return type in which case there is ambiquity Ior the compiler What happens to a static var that is deIined within a method oI a class ? - Can`t do it. You`ll get a compilation error How many static init can you have ? - As many as you want, but the static initializers and class variable initializers are executed in textual order and may not reIer to class variables declared in the class whose declarations appear textually aIter the use, even though these class variables are in scope. What is the diIIerence amongst JVM Spec, JVM Implementation, JVM Runtime ? - The JVM spec is the blueprint Ior the JVM generated and owned by Sun. The JVM implementation is the actual implementation oI the spec by a vendor and the JVM runtime is the actual running instance oI a JVM implementation Describe what happens when an object is created in Java? - Several things happen in a particular order to ensure the object is constructed properly: Memory is allocated Irom heap to hold all instance variables and implementation-speciIic data oI the object and its superclasses. Implemenation-speciIic data includes pointers to class and method data. The instance variables oI the objects are initialized to their deIault values. The constructor Ior the most derived class is invoked. The Iirst thing a constructor does is call the consctructor Ior its superclasses. This process continues until the constrcutor Ior java.lang.Object is called, as java.lang.Object is the base class Ior all objects in java. BeIore the body oI the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the body oI the constructor is executed. Thus, the constructor Ior the base class completes Iirst and constructor Ior the most derived class completes last. What does the 'Iinal keyword mean in Iront oI a variable? A method? A class? - FINAL Ior a variable: value is constant. FINAL Ior a method: cannot be overridden. FINAL Ior a class: cannot be derived What is the diIIerence between instanceoI and isInstance? - instanceoI is used to check to see iI an object can be cast into a speciIied type without throwing a cast class exception. isInstance() Determines iI the speciIied Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent oI the Java language instanceoI operator. The method returns true iI the speciIied Object argument is non-null and can be cast to the reIerence type represented by this Class object without raising a ClassCastException. It returns Ialse otherwise. Why does it take so much time to access an Applet having Swing Components the Iirst time? - Because behind every swing component are many Java objects and resources. This takes time to create them in memory. JDK 1.3 Irom Sun has some improvements which may lead to Iaster execution oI Swing applications.