Labels

.NET Job Questions About Java Absract class Abstract class Abstract Class and Interface Aggregation ajax aop apache ofbiz Apache ofbiz tutrial Association authentication autocad basics batch Binary Tree bootstrap loader in java build Builder design pattern C++ Job Questions caching CallableStatement in java certifications Chain of responsibility Design pattern charts check parentheses in a string Classes classloader in java classloading concept code quality collage level java program Composition concurrency Concurrency Tutorial Converting InputStream to String Core Java core java concept core java interview questions Core Java Interview Questions Core Java Questions core java tutorial CyclicBarrier in Java data structures database Database Job Questions datetime in c# DB Db2 SQL Replication deserialization in java Design Patterns designpatterns Downloads dtd Eclipse ejb example/sample code exception handling in core java file handling injava File I/O vs Memory-Mapped Filter first program in spring flex Garbage Collection Generics concept in java grails groovy and grails Guice Heap hibernate Hibernate Interview Questions how-to IBM DB2 IBM DB2 Tutorial ide immutable Interceptor Interface interview Interview Questions for Advanced JAVA investment bank j2ee java JAVA Code Examples Java 7 java changes java class loading JAVA Classes and Objects Java Classloader concept Java classloading concept java cloning concept java collection Java collection interview questions Java Collections java concurrency Java CountDownLatch java definiton Java design pattern Java EE 5 Java EE 6 Java Exceptions Java file Java Garbage Collection Java generics Java Glossary java hot concept java immutable concept Java Interface Java interview Question java interview question 2012 java interview question answer Java Interview Questions Java Interview Questions and Answers java interview topic java investment bank Java Job Questions java multithreading java multithreading concept java new features Java Packages java proxy object java questions Java Serialization Java serialization concept java serialization interview question java session concept java string Java Swings Questions java synchronization java threading Java Threads Questions java tutorial java util; java collections; java questions java volatile java volatile interview question Java Wrapper Classes java.java1.5 java.lang.ClassCastException JavaNotes javascript JAX-WS jdbc JDBC JDBC Database connection jdk 1.5 features JDK 1.5 new features Concurrent HashMap JMS interview question JMS tutorial job JSESSIONID concept JSESSIONID interview Question JSF jsp JSP Interview Question JSP taglib JSTL with JSP Junit Junit Concept Junit interview question.Best Practices to write JUnit test cases in Java JVM Linux - Unix tutorial Marker Interfaces MD5 encryption and decryption messaging MNC software java interview question musix NCR java interview question Networking Job Questions news Object Serialization Objects ojdbc14.jar OOP Oracle Oracle SQL Query for two timestamp difference orm own JavaScript function call in Apache ofbiz Packages Palm Apps patterns pdf persistence Portal Portlet Spring Integration Prime number test in java programs Rails Reboot remote computers REST Ruby Sample application schema SCJP security Senior java developer interviews servlet3 servlets session tracking singleton design pattern Spring Spring 2.5 Framework spring ebook Spring framework concept spring MVC spring pdf Spring Security Spring Security interview questions SQL SQL performance SQL Query to create xml file Sql Query tuning ssis and ssrs StAX and XML string concept string immutable string in java strings struts Struts2 Struts2 integration synchronization works in java Technical Interview testing tips Tomcat top Tutorial Volatile in deep Volatile working concept web Web Developer Job Questions web services weblogic Weblogic Application Server websphere what is JSESSIONID xml XML parsing in java XML with Java xslt


Monday, 1 July 2013

Core Java Interview Questions


Question What is the return type of a program's main() method?
Answer A program's main() method has a void return type.

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

Question What class allows you to read objects directly from a stream?
Answer The ObjectInputStream class supports the reading of objects from input streams.

Question What is the difference between a field variable and a local variable?
Answer A field variable is a variable that is declared as a member of a class. A local variable is
a variable that is declared local to a method.

Question How are this() and super() used with constructors?
Answer this() is used to invoke a constructor of the same class. super() is used to invoke a
superclass constructor.

Question What is the relationship between a method's throws clause and the exceptions that can
be thrown during the method's execution?
Answer A method's throws clause must declare any checked exceptions that are not caught
within the body of the method.

Question Why are the methods of the Math class static? (CoreJava)
Answer So they can be invoked as if they are a mathematical code library.

Question What are the legal operands of the instanceof operator?
Answer The left operand is an object reference or null value and the right operand is a class,
interface, or array type.

Question What an I/O filter?
Answer An I/O filter is an object that reads from one stream and writes to another, usually
altering the data in some way as it is passed from one stream to another.

Question What modifiers may be used with an interface declaration?
Answer An interface may be declared as public or abstract.

Question Is a class a subclass of itself?
Answer A class is a subclass of itself.

Question What is the difference between a while statement and a do statement?
Answer A while statement checks at the beginning of a loop to see whether the next loop
iteration should occur. A do statement checks at the end of a loop to see whether the next
iteration of a loop should occur. The do statement will always execute the body of a loop at least
once.

Question What modifiers can be used with a local inner class?
Answer A local inner class may be final or abstract.

Question What is the purpose of the File class? (CoreJava)
Answer The File class is used to create objects that provide access to the files and directories of
a local file system.

Question Can an exception be rethrown? (CoreJava)
Answer Yes, an exception can be rethrown.

Question When does the compiler supply a default constructor for a class?
Answer The compiler supplies a default constructor for a class if
no other constructors are provided.

Question If a method is declared as protected, where may the method be accessed?
Answer 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.

Question How is rounding performed under integer division?
Answer The fractional part of the result is truncated. This is known as rounding toward zero.

Question What is the difference between the Reader/Writer class hierarchy and the
InputStream/OutputStream class hierarchy?
Answer The Reader/Writer class hierarchy is character-oriented,
and the InputStream/OutputStream class hierarchy is byte-oriented.

Question What classes of exceptions may be caught by a catch clause?
Answer A catch clause can catch any exception that may be assigned
to the Throwable type. This includes the Error and Exception types.

Question If a class is declared without any access modifiers, where may the class be accessed?
Answer A class that is declared without any access modifiers is said to have package access.
This means that the class can only be accessed by other classes and interfaces that are defined
within the same package.

Question Does a class inherit the constructors of its superclass?
Answer A class does not inherit constructors from any of its superclasses.

Question What is the purpose of the System class?
Answer The purpose of the System class is to provide access to system resources.

Question Name the eight primitive Java types.
Answer The eight primitive types are byte, char, short, int, long,float, double, and boolean.

Question Which class should you use to obtain design information about an object?

Answer The Class class is used to obtain information about an object's design.

Question Is "abc" a primitive value?
Answer The String literal "abc" is not a primitive value. It is a String object.

Question What restrictions are placed on the values of each case of a switch statement?
Answer During compilation, the values of each case of a switch statement must evaluate to a
value that can be promoted to an int value.

Question What value does read() return when it has reached the end of a file?
Answer The read() method returns -1 when it has reached the end of a file.

Question Can a Byte object be cast to a double value? (CoreJava)
Answer No, an object cannot be cast to a primitive value.

Question What is the difference between a static and a non-static inner class?
Answer A non-static inner class may have object instances that are associated with instances of
the class's outer class. A static inner class does not have any object instances.

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

Question What is an object's lock and which object's have locks?
Answer 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. All objects and classes have locks. A class's lock is acquired on the
class's Class object.

Question What is the % operator?
Answer It is referred to as the modulo or remainder operator. It returns the remainder of dividing
the first operand by the second operand.

Question When can an object reference be cast to an interface reference?
Answer An object reference be cast to an interface reference when
the object implements the referenced interface.

Question Which class is extended by all other classes? (CoreJava)
Answer The Object class is extended by all other classes.

Question Can an object be garbage collected while it is still reachable?
Answer A reachable object cannot be garbage collected. Only unreachable objects may be
garbage collected.

Question Is the ternary operator written x : y ? z or x ? y : z ?
Answer It is written x ? y : z.

Question How many times may an object's finalize() method be invoked by the garbage
collector?
Answer An object's finalize() method may only be invoked once by the garbage collector.

Question What is the purpose of the finally clause of a try-catch-finally statement?
Answer The finally clause is used to provide the capability to execute code no matter
whether or not an exception is thrown or caught.

Question What is the argument type of a program's main() method?
Answer A program's main() method takes an argument of the String[] type.

Question Which Java operator is right associative? (CoreJava)
Answer The = operator is right associative.

Question Can a double value be cast to a byte? (CoreJava)
Answer Yes, a double value can be cast to a byte.

Question What is the difference between a break statement and a continue statement?
Answer 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.

Question What must a class do to implement an interface?
Answer It must provide all of the methods in the interface and identify the interface in its
implements clause.

Question What is the advantage of the event-delegation model over the earlier event-inheritance
model?
Answer The event-delegation model has two advantages over the event-inheritance model. First,
it enables event handling to be handled by objects other than the ones that generate the events (or
their containers). This allows a clean separation between a component's design and its use.
The other advantage of the event-delegation model is that it performs much better in
applications where many events are generated. This performance improvement is due to the fact
that the event-delegation model does not have to repeatedly process unhandled events, as is the
case of the event-inheritance model
.
Question How are commas used in the intialization and iteration parts of a for statement?
Answer Commas are used to separate multiple statements within the initialization and
iteration parts of a for statement.

Question What is an abstract method?
Answer An abstract method is a method whose implementation is deferred to a subclass.
Question What are wrapped classes?

Answer Wrapped classes are classes that allow primitive types to be accessed as objects.

Question What restrictions are placed on the location of a package statement within a source
code file?

Answer A package statement must appear as the first line in a source code file
(excluding blank lines and comments).

Question What is the difference between preemptive scheduling and time slicing?
Answer Under preemptive scheduling, the highest priority task executes until it enters the
waiting or dead states or a higher priority task comes into existence. Under time slicing, a task
executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler
then determines which task should execute next, based on priority and other factors.

Question What is a native method?
Answer A native method is a method that is implemented in a language other than Java.

Question What are order of precedence and associativity, and how are they used?
Answer 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

Question What is the catch or declare rule for method declarations?
Answer If a checked exception may be thrown within the body of a method, the method must
either catch the exception or declare it in its throws clause.

Question Can an anonymous class be declared as implementing an interface and extending a
class?
Answer An anonymous class may implement an interface or extend a superclass, but may not be
declared to do both.

Question What is the range of the char type?
Answer The range of the char type is 0 to 2^16 - 1.

Question What is the purpose of finalization?
Answer The purpose of finalization is to give an unreachable object the opportunity to perform
any cleanup processing before the object is garbage collected.

Question What is the difference between the Boolean & operator and the && operator?
Answer If an expression involving the Boolean & operator is evaluated, both operands are
evaluated. Then the & operator is applied to the operand. When an expression involving the &&
operator is evaluated, the first operand is evaluated. If the first operand returns a value of true
then the second operand is evaluated. The && operator is then applied to the first and second
operands. If the first operand evaluates to false, the evaluation of the second operand is skipped.

Question Why do threads block on I/O? (CoreJava)
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 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 Is null a keyword?
Answer The null value is not a keyword.

Question 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 What modifiers may be used with an inner class that is a member of an outer class?
Answer A (non-local) inner class may be declared as public, protected, private, static, final, or
abstract.

Question How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?
Answer Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character
set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16,
and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.

Question What is the disadvantage of using an inner class?
Answer Java bytecode has no concept of inner classes, so the compiler translates inner classes
into ordinary classes that are accessible to any code in the same package. An inner class gets
access to the fields of the enclosing outer class-even if these fields are declared private and
the inner class is translated into a separate class. To let this separate class access the fields of
the outer class, the compiler silently changes these fields' scope from private to package. As a
result, when you use inner classes, you not only have the inner class exposed, but you also
have the compiler silently overruling your decision to make some fields private.

Question Does java support pointers?
Answer Strictly speaking, in Java, a pointer is a reference that is guaranteed not to be null.
However, when people use the term pointer, they usually mean C++ style direct hardware
address pointers.

How can you write serious code without pointers? Java does not have raw pointers like C or
C++. It has something almost as powerful, but many times safer called references (Java refers to
them as pointers in one place, the NullPointerException). They are like pointers,
except that the dangerous features are removed.

Question Does java support global variables?
Answer Though java doesn't support global variables, you can achieve this by creating
variables public and static.

Question In a scenario, where you have to write an application which navigates between
different pre-defined states of the application, what should I be using, an Abstract class or an
Interface?

Answer I would suggest Abstract class as your application is navigating through pre-defined
states of the application.

Question What is the differences between inheritance and composition?
Answer Inheritance:
Inheritance is the ability to derive one class from another; the derived class (also called
the subclass) inherits all of the methods and data members of its superclass.

class Fruit {
//...
}
class Apple extends Fruit {
//...
}

In the above example, class Apple is related to class Fruit by inheritance, because Apple
extends Fruit. In this example Fruit is superclass and Apple is subclass.

Composition:

Composition (called "has-a") is a relationship between classes where
one class has a data member that is an instance of the other class.

class Fruit {
//...
}
class Apple {
private Fruit fruit = new Fruit();
//...
}

In the example above, class Apple is related to class Fruit by composition, because Apple has an
instance variable that holds a reference to Fruit object.

In this example, Apple is what I will call front-end class and Fruit is what I will call back-end
class. In a composition relationship, the front-end class holds a reference in one of its instance
variables to a back-end class.

Question What is meant by upcasting?
Answer Upcasting is where a derived object reference is cast to one of its base objects reference.

class Base
{
public void show()
{
System.out.println("In Base class");

}
}

class Derived extends Base
{
public void show()
{
System.out.println("In Derived class");
}

public static void main(String args[])
{
//upcasting
Base base = new Derived();
base.show();
}
}

When an object is upcast it becomes a base object for the purpose of
the cast, therefore any new fields and methods
declared in the derived class are not accessible.

Question Why Java is not complete Object Oriented Programming language?

,

"Java doesn't support multiple inheritance".

}
}

Question What is the difference between break, continue and return statements?
Answer

break: breaks the current loop and moves the cursor to next line
after the loop

e.g.
for(int i=0; i < 10; i++)
{
System.out.println("i"+i);
if(i == 5) break;
}

continue: Use continue when you have to skip few lines of code in a
loop when a condition is satisfied

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...