Você está na página 1de 13

1.Why do you create interfaces, and when MUST you use one?

You would create interfaces when you have two or more functionalities talking to each other. Doing it
this way help you in creating a protocol between the parties involved. Interfaces are mainly used for two
purposes first is that it removes the prob of multiple inheritence in classes.such as subclass cannot
inherit the properties of two superclasses bt this can be possible by using interfaces.
secondly we use interface as a common class for all other classes.

2.Why Java is not fully object oriented?

Java is not a fully object oriented language because it does not support multiple inheritance
directly.But it does so by using theconcept of interfaces.

A language is said to be fully object oriented if it supports classes,objects,inheritance and


polymorphism.For example C++ supports full fledged feature of inheritance and all the types of
inheritances(single,multilevel,multiple,hierarchical and multipath inheritances), and if we take
the polymorphism C++ supports static binding and operator overloading which come under static
polymorphism where as Java does not support operator overloading.

We cannot say Java as an Object based language because it supports Inheritance and
polymorphism to some extent. Where as in object based languages there are classes and objects
but no inheritance and polymorphism.

whatever u told may be correct but according to u c++ is purely object oriented but we can write
c++ prgrm without object.in case of java we have to create object,so i think it is purely object
oriented though java may not support multiple inheritance.

according to me java is not pure object oriented language because it does not directly support multiple
inheritence and it does not gives the output in the form of object.for ex if we add two no(3+5) it gives 8
which is not the part of class and not any object

How System.out.println() works?

System is a class which helps in giving information / and access to out computer system.To check heap
memory, system properties and so on.
Printstreem is a class which streems the data to the standard output. Now java people made an
association by adding the object which is static and final out in the system class. So we as programmers
can use it as System the class name (System.out) out is static object of Printstreem class and println is a
method in Printstreem class which are overloaded methods. so we directly write
System.out.println(data ). BUT WE SHOULD NOT USE IT ... AS STREAMS ARE DEPENDENT TO THE
HARDWARE...Try to use the PrintWriter in the java.io package..as most of the time we are playing with
the charectar stream so using Printwriter makes it more independent ,,,PrintWriter out = new
PrintWriter(System.out,"true");
What is the disadvantage of threads?

The execution of thread is based upon the choice of OS .though user has created the thread but it will
execute when OS allows it. that's why output of thread program is differ from PC to PC.master control is
take by OS.it will decide on the basis of CPU life cycle

The Main disadvantage of threads is : Threads is operating system dependent.

What is difference between & and && in java

Single Ampercent (& ) can be used as Bitwise operator and Boolean logical operator.

Now the difference arises when & is used as logical operator.

This can be best explained with the help of an example.

int i=5;
if(i<3 & i++ < 10)
{
//perform action
}

Now in this case


First expression is i<3
Second expression is i++ < 10

when & is used it will evaluate both the expressions regardless of the fact that it finds first expression as
FALSE and only then will it give an answer.

Whereas if && was used in place of & , after it had evaluated first expression and had found result of
first expression as FALSE, it would not have evaluated second expression. Thus saving time.

FYI... && is also called as counterpart of & and the evaluation it does is called short-circuit evaluation.

Why Java is case sensitive?

Java is multi platform-> Platform Independent Language, so its used widely in very big softwares or
games where multiple variables needed. To Differentiate among that huge no of variables,,Java is Case
Sensitive. Java is a platform independent.It can be used in any machines irrespective of platform.so it is a
case sensitive. Because java support unicode.

Access specifiers: "public", "protected", "private", nothing?

Public : Any other class from any package can instantiate and execute the classes and methods.
Protected : Only subclasses and classes inside of the package can access the classes and methods.
Private : The original class is the only class allowed to execute the methods.
the three acess specifiers public, private, protected. public means can run outside the class, private
means can run within the class.protected means it takes the similarities of both the public and private
and it runs according to those specifiers

public:the class members of any package can access the base class.
private:only the members of base class can access to it.
protected:it is used in case of inheritence,only the immediate sub-class members can access the
base class.

What is the difference between length and length() ?

length() is function which is defined for string class whereas length is used only for array. Both returns
an int value.length parameter is used with String array to get the number of elements of array and
length()method is used with String Buffer to get the length.

Why we can not override static method?

We can not override final method by definition, but we can override static method. We CaNNOT
override a static method. See a static method has nothing to do with the instance of the class. So when
u r using a static method using .(dot) it actually subsitutes the class name. Inheritance is a concept based
on instances. So when overriding u r actually redefining the method. Consider the example which will
made ur concept more clear:
class X{
public static void do(){
SOP("I m method do inX");
}
public void go(){
SOP("I m method go in X");
}
}

class Y extends X{
public static do(){
SOP("I m method do inY");
}
public void go(){
SOP("I m method go in Y");
}
public static void main(String a[])
{ X z = new Y();
z.do();
z.go();
}
}

The output will be:


I m method do inX //static is not overriden
I m method go in Y //overridden

What is package? Define with example?

A package is a grouping of related types providing access protection and name space management. Note
that types refers to classes, interfaces, enumerations, and annotation types. Enumerations and
annotation types are special kinds of classes and interfaces, respectively, so types are often referred to
in this lesson simply as classes and interfaces.

Package is a directory of related classes and interfaces.


Eg: java.lang.*
Here java is a directory,lang stands for language & it is a sub directory, * indicates collection of related
classes & interfaces

In Java, You can create a String object as:


String str = "abc"; &
String str = new String("abc");
Why cant a button object be created as : Button bt = "abc"
Why is it compulsory to create a button object as: Button bt = new Button("abc");
Why is this not compulsory in String's case?

The main reason you cannot create a button by Button bt1= "abc"; is because "abc" is a literal string
(something slightly different than a String object, bytheway)and bt1 is a
Button object. That simple. The only object in Java that can be assigned a literal String is java.lang.String.
Important to note that you are NOT calling a java.lang.String
constuctor when you type String s = "abc";
For example
String x = "abc";
String y = "abc";
refer to the same object.
While
String x1 = new String("abc");
String x2 = new String("abc");
refer to two different objects.

String class is extensively used and it is an immutable class (means each time new object is created). so
each time you create a String object it goes to memory. if you create using 'new' keyword then it creates
two objects, one in heap memory and one in string pool. so to utilize memory in a better way with
fulfilling object creation; String class has a way to create an object without using new keyword and is
called as String literal (String s = "sandeep"). this way only one object is created which goes to string
pool.it is also referred by any String with same value so less memory consumption.

What is synchronization and why is it important?

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 causes dirty data and leads to significant errors.

Synchronization occurs when you want two threads to run, but they are somewhat dependent on each
other.
A thread is a distinct flow of program logic that can be executed at the same time as other threads. This
make it seem like two things are running simultaneously (kind of like how word spell checks as you
type).
Occasionally you might need one thread to wait until another thread gets to the same point.
An example might be if you had a program, and a fancy user interface that required animation. You
might pause the logic part of your program and wait until the animation finished before the threads
continue on. This is synchronization.

Are constructors inherited?Can a subclass call the parent's class constructor?When?

You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor
of one of it's superclasses. One of the main reasons is because you probably don't want to overide the
superclasses constructor, which would be possible if they were inherited. By giving the developer the
ability to override a superclasses constructor you would erode the encapsulation abilities of the
language. constructors are not inherited.You can call the superclass constructor with the help of super()
from derived class.

What is the difference between InputStream/Outputstream classes?

InputStream and its sub classes are used for reading data in the form of streams.

Ex : FileInputStream, DataInputStream

OutputStream and its sub classes are used for writing data in the form of streams.

Ex: FileOutputStream, DataOutputStream


InputStream is use for retrive the data from file in streams & OutPutStream is use for write the data in
file in the form of stream

What is the difference between Java and J2EE? Is J2EE advanced version of Java?

Java is a object-oriented programming language which is platform neutral. That is unlike C or C++ java
programs, Java can be run on any operating system with its JVM. J2EE is just a specification for server
side programs. That is to support internet applications, distributed and uses component model. So that
Enterprises use this server side technology in their distributed business. Since J2EE is just a server side
specification, anybody can implement the specification but again using Java. Hence today we have
Custom implemented J2EE servers from Oracle, SUN, IBM and so on..

ava is a technology and an architecture which provides a platform, a language and an API; unlike j2ee is
an internet enabled application software for development of APPLETS and other internet services
Difference: Java Beans, Servlets

java bean is a reusable component,where as the servlet is the java program which extends the
server capability. java beans is used as the MODEL in MVC architecture whereas the servlets is
used as the CONTROLLER in MVC architecture
2)the java beans has the setter/getter methods that are used in the servlets to be initialized

What restrictions are placed on method overloading?

Two methods may not have the same name and argument list but different return types. In method
overloading function name will be same but the argument count or type or order must vary. They can
have different return type also. The only restriction on method overloading is the signature of the
method. The signature is the number, type, and order of the arguments passed to a method.Any
number of methods which have the same name(unless overridden) cannot have the same
signature,though they can have the same return types in the same scope.The compiler uses the
signature to detect which overloaded method to refer when a overloaded method is called.If two
methods have the same name and signature the compiler will throw an runtime error.

restrictions for method overloading:


1. method names should be the same.
2. signatures should be different
3. return type may or may not be same.

can the main() method be overloaded? or overrided? and what happens when we do so?

yes, It cam be overloaded, I am not sure if it can be overridden. In over loading u have same method
name with different parameteres. So main can have no params or it can have a String objects as
arguments. The purpose of passing string as args can be to dynamically submit, input, output, error
filenames etc.

I think it also can be overridden

check this code i am sure its working:

class Scon

{
Scon()
{
System.out.println("obj created");
}

public static void main(String[] args)


{
System.out.println("Hello World A!");
}
}
public class B extends Scon
{
public static void main(String[] args)
{
System.out.println("Hello World B!");

Given A = 1, B = 2, C = 3, D = 4 ?? Z = 26, write a java standalone program to encode the word ?


TELEPHONE? into numbers.

class enCode
{
public static void main(String ar[])
{
String enCodeWord = "TELEPHONE";
String result = "";
for ( int i = 0; i < enCodeWord.length(); i++ )
{
switch( enCodeWord.charAt( i ) )
{
case 'A':
result = result + "1";
break;
case 'B':
result = result + "2";
break;
case 'C':
result = result + "3";
break;
case 'D':
result = result + "4";
break;
case 'E':
result = result + "5";
break;
case 'F':
result = result + "6";
break;
case 'G':
result = result + "7";
break;
case 'H':
result = result + "8";
break;
case 'I':
result = result + "9";
break;
case 'J':
result = result + "10";
break;
case 'K':
result = result + "11";
break;
case 'L':
result = result + "12";
break;
case 'M':
result = result + "13";
break;
case 'N':
result = result + "14";
break;
case 'O':
result = result + "15";
break;
case 'P':
result = result + "16";
break;
case 'Q':
result = result + "17";
break;
case 'R':
result = result + "18";
break;
case 'S':
result = result + "19";
break;
case 'T':
result = result + "20";
break;
case 'U':
result = result + "21";
break;
case 'V':
result = result + "22";
break;
case 'W':
result = result + "23";
break;
case 'X':
result = result + "24";
break;
case 'Y':
result = result + "25";
break;
case 'Z':
result = result + "26";
break;
}
}
System.out.println( result );
}
}

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

The if statement is used to select among two alternatives. It uses a boolean expression todecide
whichalternative should be executed. The switch statement is used to select among multiple
alternatives. It usesan int expression to determine which alternative should be executed.

"if" statement can be used to compare about objects. for example: String compare to String. But for
"switch" statement, it can only compare about int, byte,char, long, short, and enum. In if statement we
can use relation operator such as(<,>,<=,>=) and also use boolean.
Here in if statement if we use nested if statement then the first condition is false then they go to next
'else-if' . But in switch we can we can just the value and direclty go to that 'case'

What are wrapped classes?

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

egs:- intergerclass
doubleclass

Wrapper classes are classes that allow primitive data types.Every primitive data types in java has got the
corresponding wrapper class it supports autoboxing and unboxing. Wrapper classes are those classes
which allow to use primitive datatype with additional functionalities associated with it.

Can I create final executable from Java?

yes u can create executable for java just download install4j and use it. Add below stuff to your
manifest file, and create jar

META-INF/MANIFEST.MF

Manifest-Version: 1.0
Class-Path: your-app.jar
Main-Class: com.your.Main.class
What is the difference between attribute and parameter ?

attribute - is a member of a class


parameter - is a member of a method signature

argument - is used to send values to the method from the caller.


parameter - is a place holder for the incoming values for a method from the caller.

Attribute is the value we pass to the calling function. Parameter is the list of variables we use in a
function.

1. Attributes are of the following types:Session attribute,Request attribute, Context attribute.


Parameters are of the following types:Servlet parameters,Request parameters,Context
parameters.
2. The attributes are set using the setAttribute() method. Parameters are set in the deployment
descriptor using the <param-name> and <param-value> elements.
3. Attributes are obtained using the getAttribute(String name) method. Parameters are obtained
using the getParameter(String name) method.
4. The attributes are returned as Objects.The parameters are returned as Strings.

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. The
synchronized keyword tells the JVM that the method requires a lock in order to run. The JVM
then creates the lock and manages the allocation of the lock to threads during execution. When a
thread has the lock for a class, it is the only thread that can execute any of the class methods that
require a lock.

What is the priority of main thread in java?? and why??

The 'main()' method in Java is referred to the thread that is running, whenever a Java program
runs. It calls the main thread because it is the first thread that starts running when a program
begins. Other threads can be spawned from this main thread. The main thread must be the last
thread in the program to end. When the main thread stops, the program stops running.

Main thread is created automatically, but it can be controlled by the program by using a Thread
object. The Thread object will hold the reference of the main thread with the help of
currentThread() method of the Thread class.

Is true that JAVA is not 100% object oriented language ?

yes. It is true that JAVA is 100% object oriented language.


It follows all object oriented concepts like Encapsulation, Inheritance and polymorphism.
regarding primitive types...There are Wrapper classes to replace the primitive types. Since the
primitive types' performance is better than the wrapper classes, we use primitive types. no, Java
is not 100% OO Programming language. Because it does not support multiple inheritance as in
C++. I am not satisfied with this answer. We can not use multiple inheritances in Java,
Java provides us multiple implementations. So Java is not 100% Object Oriented.

What is the main difference between access modifiers and access specifiers?

Access specifiers are public,private,protected,default , were access modifiers are


static,final,abstact,transient,synchronized and volitale

What is the difference between Serializalble and Externalizable interface?

When you use Serializable interface, your class is serialized automatically by default. But you
can override writeObject() and readObject()two methods to control more complex object
serailization process. When you use Externalizable interface, you have a complete control over
your class's serialization process.

How can i call parameterized constructor if am instantiating a object using


"Class.forName("Abc").newInstance()"?

We can do by the reflection mechanism. For example here is the code.


TestConstructor testConstructor; // This is my class which i want to call.
For arguments of construtor.
Class[] args = new Class[] {String.class} // specify the type of arugument and with no of
aruguments.
Declare the value for argument ie. String a1 = "abc";
Object[] argObj = new Object[] {a1}
Get the Construtor object of TestConstructor object i.e
Constructor defaultConstruct =
Class.forName("packagename.TestConstructor").getConstructor(args);
TestConstructor test = (TestConstructor) defaultConstruct.newInstance(argObj).
This gives you the class object so u can call repective methods available with the class like
test.display();

What is super class of an Exception class?

The super class of exception is throw able class. Object Class. Exception is a supper class have
many child class as run time exception etc. Throwable class is the super class of exception class.
Throwable class is the super class of exceptions and Error class. Object class is the super class of
Throwable class

What is the difference between a static and a non-static inner class?

A non-static inner class may have object instances that are associated with instances ofthe class's
outer class. A static inner class does not have any object instances. non static nested classes(or
inner class) have direct access to the members of the enclosing class.Static nested class can
access members only thru instance of enclosing class.
Why java does not support Multiple Inheritance?

The java designer's view was to develop a simple, object oriented, and familiar language. In the
designers' opinion, multiple inheritance causes more problems and confusion than it solves.
JAVA DOES SUPPORT MULTIPLE INHERITANCE.IT CAN BE MADE POSSIBLE BY
USING THE KEYWORD "INTERFACE".

Java Supports multiple inheritance via interfaces.


A class can implement N no. of interfaces, but can extend only one class.

What value does read() return when it has reached the end of a file?

The read() method returns -1 when it has reached the end of a file.

What is java virtual machine and what is its uses?

JVM is basic run time environment for java applications. Where all java objets birth,live and
dead. JVM is used to convert the machine code to byte code. Portability is the one of the features
of java. its done by using JVM.

What is class loader

A class loader is an object that is responsible for loading classes.The class ClassLoader is an
abstract class.

What is the meaning of "final" keyword?

Final keyword is used for define constant variables and method and no body can change that
variable and method within program. Final class cannot be extended. Final method cannot be
overriden and value of final variable cannot be changed

What is the Map interface?

A Map (in the API reference documentation) is an object that maps keys to values. A map cannot
contain duplicate keys: Each key can map to at most one value. The Map interface follows.

public interface Map {

//Basic operations
V put(K key, V value);
V get(Object key);
V remove(Object key);
boolean containsKey(Object key);
boolean containsValue(Object value);
int size();
boolean isEmpty();
//Bulk operations
void putAll(Map<? extends K,? extends V> t);
void clear();

// Collection Views
public Set<K> keySet();
public Collection<V> values();
public Set<Map.Entry<K,V>> entrySet();

// Interface for entrySet elements


public interface Entry {
K getKey();
V getValue();
V setValue(V value);
}
}

The Java platform contains three general-purpose Map implementations: HashMap (in the API
reference documentation), TreeMap (in the API reference documentation), and LinkedHashMap
(in the API reference documentation). Their behavior and performance are precisely analogous
to HashSet, TreeSet, and LinkedHashSet, as described in The Set Interface (in the Collections
trail) section. Also, Hashtable was retrofitted to implement Map.

Pls refer http://java.sun.com/docs/books/tutorial/collections/interfaces/map.html for complete


information on Map Interface.

Você também pode gostar