Você está na página 1de 10

1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence?

Platform independence means that we can write and compile the java code in one platform (eg Windows) and can execute the class in any other supported platform eg ( inux!"olaris!etc). #. Are JVM's platform independent? J$%&s are not platform independent. J$%&s are platform specific run time implementation provided 'y the vendor. (. What is a JVM? J$% is Java $irtual %achine which is a run time environment for the compiled java class files. ). What is the difference between a JDK and a JVM? J*+ is Java *evelopment +it which is for development purpose and it includes execution environment also. ,ut J$% is purely a run time environment and hence you will not 'e a'le to compile your source files using a J$%. -. What is a pointer and does Java support pointers? Pointer is a reference handle to a memory location. .mproper handling of pointers leads to memory lea/s and relia'ility issues hence Java doesn&t support the usage of pointers. 0. What is the base class of all classes? java.lang.1'ject 2. Does Java support multiple inheritance? Java doesn&t support multiple inheritance. 3. Is Java a pure ob ect oriented lan!ua!e? Java uses primitive data types and hence is not a pure o'ject oriented language. 14. Are arrays primitive data types? .n Java! 5rrays are o'jects. 11. What is difference between "ath and #lasspath? Path and 6lasspath are operating system level environment variales. Path is used define where the system can find the executa'les(.exe) files and classpath is used to specify the location .class files. 12. What are local variables? ocal varaia'les are those which are declared within a 'loc/ of code li/e methods. ocal varia'les should 'e initialised 'efore accessing them. 1#. What are instance variables? .nstance varia'les are those which are defined at the class level. .nstance varia'les need not 'e initiali7ed 'efore using them as they are automatically initiali7ed to their default values. 1(. $ow to define a constant variable in Java? 8he varia'le should 'e declared as static and final. "o only one copy of the varia'le exists for all instances of the class and the value can&t 'e changed also. static final int P. 9 2.1(: is an example for constant.

1). %hould a main method be compulsorily declared in all ava classes? ;o not re<uired. main method should 'e defined only if the source class is a java application. 1-. What is the return type of the main method? %ain method doesn&t return anything hence declared void. 10. Why is the main method declared static? main method is called 'y the J$% even 'efore the instantiation of the class hence it is declared as static. 12. What is the ar!uement of main method? main method accepts an array of "tring o'ject as arguement. 13. #an a main method be overloaded? =es. =ou can have any num'er of main methods with different method signature and implementation in the class. 24. #an a main method be declared final? =es. 5ny inheriting class will not 'e a'le to have it&s own default main method. 21. Does the order of public and static declaration matter in main method? ;o it doesn&t matter 'ut void should always come 'efore main(). 22. #an a source file contain more than one #lass declaration? =es a single source file can contain any num'er of 6lass declarations 'ut only one of the class can 'e declared as pu'lic. 2#. What is a pac&a!e? Pac/age is a collection of related classes and interfaces. pac/age declaration should 'e first statement in a java class. 2(. Which pac&a!e is imported by default? java.lang pac/age is imported 'y default even without a pac/age declaration. 2). #an a class declared as private be accessed outside it's pac&a!e? ;ot possi'le. 2-. #an a class be declared as protected? 5 class can&t 'e declared as protected. only methods can 'e declared as protected. 20. What is the access scope of a protected method? 5 protected method can 'e accessed 'y the classes within the same pac/age or 'y the su'classes of the class in any pac/age. 22. What is the purpose of declarin! a variable as final? 5 final varia'le&s value can&t 'e changed. final varia'les should 'e initiali7ed 'efore using them. 23. What is the impact of declarin! a method as final? 5 method declared as final can&t 'e overridden. 5 su'>class can&t have the same method signature with a different implementation. #4. I don't want my class to be inherited by any other class' What should i do? =ou should declared your class as final. ,ut you can&t define your class as final! if it is an a'stract class. 5 class declared as final can&t 'e extended 'y any other class. #1. #an you !ive few e(amples of final classes defined in Java A"I? java.lang."tring!java.lang.%ath are final classes. #2. $ow is final different from finally and finali)e? final is a modifier which can 'e applied to a class or a method or a varia'le. final

class can&t 'e inherited! final method can&t 'e overridden and final varia'le can&t 'e changed. finally is an exception handling code section which gets executed whether an exception is raised or not 'y the try 'loc/ code segment. finali7e() is a method of 1'ject class which will 'e executed 'y the J$% just 'efore gar'age collecting o'ject to give a final chance for resource releasing activity. ##. #an a class be declared as static? ;o a class cannot 'e defined as static. 1nly a method!a varia'le or a 'loc/ of code can 'e declared as static. #(. When will you define a method as static? When a method needs to 'e accessed even 'efore the creation of the o'ject of the class then we should declare the method as static. #). What are the restriction imposed on a static method or a static bloc& of code? 5 static method should not refer to instance varia'les without creating an instance and cannot use ?this? operator to refer the instance. #-. I want to print *$ello* even before main is e(ecuted' $ow will you acheive that? Print the statement inside a static 'loc/ of code. "tatic 'loc/s get executed when the class gets loaded into the memory and even 'efore the creation of an o'ject. @ence it will 'e executed 'efore the main method. 5nd it will 'e executed only once. #0. What is the importance of static variable? static varia'les are class level varia'les where all o'jects of the class refer to the same varia'le. .f one o'ject changes the value then the change gets reflected in all the o'jects. #2. #an we declare a static variable inside a method? "tatic varai'les are class level varia'les and they can&t 'e declared inside a method. .f declared! the class will not compile. #3. What is an Abstract #lass and what is it's purpose? 5 6lass which doesn&t provide complete implementation is defined as an a'stract class. 5'stract classes enforce a'straction. (4. #an a abstract class be declared final? ;ot possi'le. 5n a'stract class without 'eing inherited is of no use and hence will result in compile time error. (1. What is use of a abstract variable? $aria'les can&t 'e declared as a'stract. only classes and methods can 'e declared as a'stract. (2. #an you create an ob ect of an abstract class? ;ot possi'le. 5'stract classes can&t 'e instantiated. (#. #an a abstract class be defined without any abstract methods? =es it&s possi'le. 8his is 'asically to avoid instance creation of the class. ((. #lass # implements Interface I containin! method m+ and m, declarations' #lass # has provided implementation for method m,' #an i create an ob ect of #lass #? ;o not possi'le. 6lass 6 should provide implementation for all the methods in the

.nterface .. "ince 6lass 6 didn&t provide implementation for m1 method! it has to 'e declared as a'stract. 5'stract classes can&t 'e instantiated. (). #an a method inside a Interface be declared as final? ;o not possi'le. *oing so will result in compilation error. pu'lic and a'stract are the only applica'le modifiers for method declaration in an interface. (-. #an an Interface implement another Interface? .ntefaces doesn&t provide implementation hence a interface cannot implement another interface. (0. #an an Interface e(tend another Interface? =es an .nterface can inherit another .nterface! for that matter an .nterface can extend more than one .nterface. (2. #an a #lass e(tend more than one #lass? ;ot possi'le. 5 6lass can extend only one class 'ut can implement any num'er of .nterfaces. (3. Why is an Interface be able to e(tend more than one Interface but a #lass can't e(tend more than one #lass? ,asically Java doesn&t allow multiple inheritance! so a 6lass is restricted to extend only one 6lass. ,ut an .nterface is a pure a'straction model and doesn&t have inheritance hierarchy li/e classes(do remem'er that the 'ase class of all classes is 1'ject). "o an .nterface is allowed to extend more than one .nterface. )4. #an an Interface be final? ;ot possi'le. *oing so so will result in compilation error. )1. #an a class be defined inside an Interface? =es it&s possi'le. )2. #an an Interface be defined inside a class? =es it&s possi'le. )#. What is a Mar&er Interface? 5n .nterface which doesn&t have any declaration inside 'ut still enforces a mechanism. )(. Which -- #oncept is achieved by usin! overloadin! and overridin!? Polymorphism. )). If i only chan!e the return type. does the method become overloaded? ;o it doesn&t. 8here should 'e a change in method arguements for a method to 'e overloaded. )-. Why does Java not support operator overloadin!? 1perator overloading ma/es the code very difficult to read and maintain. 8o maintain code simplicity! Java doesn&t support operator overloading. )0. #an we define private and protected modifiers for variables in interfaces? ;o )2. What is /(ternali)able? Axternali7a'le is an .nterface that extends "eriali7a'le .nterface. 5nd sends data into "treams in 6ompressed Bormat. .t has two methods! writeAxternal(1'ject1uput out) and readAxternal(1'ject.nput in) )3. What modifiers are allowed for methods in an Interface? 1nly pu'lic and a'stract modifiers are allowed for methods in interfaces.

-4. What is a local. member and a class variable? $aria'les declared within a method are ?local? varia'les. $aria'les declared within the class i.e not within any methods are ?mem'er? varia'les (glo'al varia'les). $aria'les declared within the class i.e not within any methods and are defined as ?static? are class varia'les -1. What is an abstract method? 5n a'stract method is a method whose implementation is deferred to a su'class. -2. What value does read01 return when it has reached the end of a file? 8he read() method returns >1 when it has reached the end of a file. -#. #an a 2yte ob ect be cast to a double value? ;o! an o'ject cannot 'e cast to a primitive value. -(. What is the difference between a static and a non3static inner class? 5 non>static inner class may have o'ject instances that are associated with instances of the class&s outer class. 5 static inner class does not have any o'ject instances. -). What is an ob ect's loc& and which ob ect's have loc&s? 5n o'ject&s loc/ is a mechanism that is used 'y multiple threads to o'tain synchroni7ed access to the o'ject. 5 thread may execute a synchroni7ed method of an o'ject only after it has ac<uired the o'ject&s loc/. 5ll o'jects and classes have loc/s. 5 class&s loc/ is ac<uired on the class&s 6lass o'ject. --. What is the 4 operator? .t is referred to as the modulo or remainder operator. .t returns the remainder of dividing the first operand 'y the second operand. -0. When can an ob ect reference be cast to an interface reference? 5n o'ject reference 'e cast to an interface reference when the o'ject implements the referenced interface. -2. Which class is e(tended by all other classes? 8he 1'ject class is extended 'y all other classes. -3. Which non35nicode letter characters may be used as the first character of an identifier? 8he non>Cnicode letter characters D and E may appear as the first character of an identifier 04. What restrictions are placed on method overloadin!? 8wo methods may not have the same name and argument list 'ut different return types. 01. What is castin!? 8here are two types of casting! casting 'etween primitive numeric types and casting 'etween o'ject references. 6asting 'etween numeric types is used to convert larger values! such as dou'le values! to smaller values! such as 'yte values. 6asting 'etween o'ject references is used to refer to an o'ject 'y a compati'le class! interface! or array type reference. 02. What is the return type of a pro!ram's main01 method? void. 0#. If a variable is declared as private. where may the variable be accessed? 5 private varia'le may only 'e accessed within the class in which it is declared.

0(. What do you understand by private. protected and public? 8hese are accessi'ility modifiers. Private is the most restrictive! while pu'lic is the least restrictive. 8here is no real difference 'etween protected and the default type (also /nown as pac/age protected) within the context of the same pac/age! however the protected /eyword allows visi'ility to a derived class in a different pac/age. 0). What is Downcastin! ? *owncasting is the casting from a general to a more specific type! i.e. casting down the hierarchy 0-. What modifiers may be used with an inner class that is a member of an outer class? 5 (non>local) inner class may 'e declared as pu'lic! protected! private! static! final! or a'stract. 00. $ow many bits are used to represent 5nicode. A%#II. 5673+8. and 56739 characters? Cnicode re<uires 1- 'its and 5"6.. re<uire 0 'its. 5lthough the 5"6.. character set uses only 0 'its! it is usually represented as 2 'its. C8B>2 represents characters using 2! 1-! and 12 'it patterns. C8B>1- uses 1->'it and larger 'it patterns. 02. What restrictions are placed on the location of a pac&a!e statement within a source code file? 5 pac/age statement must appear as the first line in a source code file (excluding 'lan/ lines and comments). 03. What is a native method? 5 native method is a method that is implemented in a language other than Java. 24. What are order of precedence and associativity. and how are they used? 1rder of precedence determines the order in which operators are evaluated in expressions. 5ssociatity determines whether an expression is evaluated left>to> right or right>to>left 21. #an an anonymous class be declared as implementin! an interface and e(tendin! a class? 5n anonymous class may implement an interface or extend a superclass! 'ut may not 'e declared to do 'oth. 22. What is the ran!e of the char type? 8he range of the char type is 4 to 2F1- > 1. 2#. What is the ran!e of the short type? 8he range of the short type is >(2F1)) to 2F1) > 1. 2(. Why isn't there operator overloadin!? ,ecause 6GG has proven 'y example that operator overloading ma/es code almost impossi'le to maintain. 2). What does it mean that a method or field is *static*? "tatic varia'les and methods are instantiated only once per class. .n other words they are class varia'les! not instance varia'les. .f you change the value of a static varia'le in a particular o'ject! the value of that varia'le changes for all instances of that class. "tatic methods can 'e referenced with the name of the class rather than the name of a particular o'ject of the class (though that wor/s too). 8hat&s

how li'rary methods li/e "ystem.out.println() wor/. out is a static field in the java.lang."ystem class. 2-. Is null a &eyword? 8he null value is not a /eyword. 20. Which characters may be used as the second character of an identifier.but not as the first character of an identifier? 8he digits 4 through 3 may not 'e used as the first character of an identifier 'ut they may 'e used after the first character of an identifier. 22. Is the ternary operator written ( : y ? ) or ( ? y : ) ? .t is written x H y I 7. 23. $ow is roundin! performed under inte!er division? 8he fractional part of the result is truncated. 8his is /nown as rounding toward 7ero. 34. If a class is declared without any access modifiers. where may the class be accessed? 5 class that is declared without any access modifiers is said to have pac/age access. 8his means that the class can only 'e accessed 'y other classes and interfaces that are defined within the same pac/age. 31. Does a class inherit the constructors of its superclass? 5 class does not inherit constructors from any of its superclasses. 32. ;ame the ei!ht primitive Java types' 8he eight primitive types are 'yte! char! short! int! long! float! dou'le! and 'oolean. 3#. What restrictions are placed on the values of each case of a switch statement? *uring compilation! the values of each case of a switch statement must evaluate to a value that can 'e promoted to an int value. 3(. What is the difference between a while statement and a do statement? 5 while statement chec/s at the 'eginning of a loop to see whether the next loop iteration should occur. 5 do statement chec/s at the end of a loop to see whether the next iteration of a loop should occur. 8he do statement will always execute the 'ody of a loop at least once. 3). What modifiers can be used with a local inner class? 5 local inner class may 'e final or a'stract. 3-. When does the compiler supply a default constructor for a class? 8he compiler supplies a default constructor for a class if no other constructors are provided. 30. If a method is declared as protected. where may the method be accessed? 5 protected method may only 'e accessed 'y classes or interfaces of the same pac/age or 'y su'classes of the class in which it is declared. 32. What are the le!al operands of the instanceof operator? 8he left operand is an o'ject reference or null value and the right operand is a class! interface! or array type. 33. Are true and false &eywords? 8he values true and false are not /eywords.

144. What happens when you add a double value to a %trin!? 8he result is a "tring o'ject. 141. What is the diffrence between inner class and nested class? When a class is defined within a scope od another class! then it 'ecomes inner class. .f the access modifier of the inner class is static! then it 'ecomes nested class. 142. #an an abstract class be final? 5n a'stract class may not 'e declared as final 14#. What is numeric promotion? ;umeric promotion is the conversion of a smaller numeric type to a larger numeric type! so that integer and floating>point operations may ta/e place. .n numerical promotion! 'yte! char! and short values are converted to int values. 8he int values are also converted to long values! if necessary. 8he long and float values are converted to dou'le values! as re<uired 14(. What is the difference between a public and a non3public class? 5 pu'lic class may 'e accessed outside of its pac/age. 5 non>pu'lic class may not 'e accessed outside of its pac/age. 14). 6o what value is a variable of the boolean type automatically initiali)ed? 8he default value of the 'oolean type is false 14-. What is the difference between the prefi( and postfi( forms of the << operator? 8he prefix form performs the increment operation and returns the value of the increment operation. 8he postfix form returns the current value all of the expression and then performs the increment operation on that value. 140. What restrictions are placed on method overridin!? 1verridden methods must have the same name! argument list! and return type. 8he overriding method may not limit the access of the method it overrides. 8he overriding method may not throw any exceptions that may not 'e thrown 'y the overridden method. 142. What is a Java pac&a!e and how is it used? 5 Java pac/age is a naming context for classes and interfaces. 5 pac/age is used to create a separate name space for groups of classes and interfaces. Pac/ages are also used to organi7e related classes and interfaces into a single 5P. unit and to control accessi'ility to these classes and interfaces. 143. What modifiers may be used with a top3level class? 5 top>level class may 'e pu'lic! a'stract! or final. 114. What is the difference between an if statement and a switch statement? 8he if statement is used to select among two alternatives. .t uses a 'oolean expression to decide which alternative should 'e executed. 8he switch statement is used to select among multiple alternatives. .t uses an int expression to determine which alternative should 'e executed. 111. What are the practical benefits. if any. of importin! a specific class rather than an entire pac&a!e 0e'!' import ava'net'= versus import ava'net'%oc&et1?

.t ma/es no difference in the generated class files since only the classes that are actually used are referenced 'y the generated class file. 8here is another practical 'enefit to importing single classes! and this arises when two (or more) pac/ages have classes with the same name. 8a/e java.util.8imer and javax.swing.8imer! for example. .f . import java.util.J and javax.swing.J and then try to use ?8imer?! . get an error while compiling (the class name is am'iguous 'etween 'oth pac/ages). et&s say what you really wanted was the javax.swing.8imer class! and the only classes you plan on using in java.util are 6ollection and @ash%ap. .n this case! some people will prefer to import java.util.6ollection and import java.util.@ash%ap instead of importing java.util.J. 8his will now allow them to use 8imer! 6ollection! @ash%ap! and other javax.swing classes without using fully <ualified class names in. 112. #an a method be overloaded based on different return type but same ar!ument type ? ;o! 'ecause the methods can 'e called without using their return type in which case there is am'i<uity for the compiler 11#. What happens to a static var that is defined within a method of a class ? 6an&t do it. =ou&ll get a compilation error 11(. $ow many static init can you have ? 5s many as you want! 'ut the static initiali7ers and class varia'le initiali7ers are executed in textual order and may not refer to class varia'les declared in the class whose declarations appear textually after the use! even though these class varia'les are in scope. 11). What is the difference between method overridin! and overloadin!? 1verriding is a method with the same name and arguments as in a parent! whereas overloading is the same method name 'ut different arguments 11-. What is constructor chainin! and how is it achieved in Java ? 5 child o'ject constructor always first needs to construct its parent (which in turn calls its parent constructor.). .n Java it is done via an implicit call to the no>args constructor as the first statement. 110. What is the difference between the 2oolean > operator and the >> operator? .f an expression involving the ,oolean K operator is evaluated! 'oth operands are evaluated. 8hen the K operator is applied to the operand. When an expression involving the KK operator is evaluated! the first operand is evaluated. .f the first operand returns a value of true then the second operand is evaluated. 8he KK operator is then applied to the first and second operands. .f the first operand evaluates to false! the evaluation of the second operand is s/ipped. 112. Which Java operator is ri!ht associative? 8he 9 operator is right associative. 113. #an a double value be cast to a byte? =es! a dou'le value can 'e cast to a 'yte. 124. What is the difference between a brea& statement and a continue statement? 5 'rea/ statement results in the termination of the statement to which it applies

(switch! for! do! or while). 5 continue statement is used to end the current loop iteration and return control to the loop statement. 121. #an a for statement loop indefinitely? =es! a for statement can loop indefinitely. Bor example! consider the followingI for(::) : 122. 6o what value is a variable of the %trin! type automatically initiali)ed? 8he default value of an "tring type is null. What is the difference 'etween a field varia'le and a local varia'leH 5 field varia'le is a varia'le that is declared as a mem'er of a class. 5 local varia'le is a varia'le that is declared local to a method. 12#. $ow are this01 and super01 used with constructors? this() is used to invo/e a constructor of the same class. super() is used to invo/e a superclass constructor. 12(. What does it mean that a class or member is final? 5 final class cannot 'e inherited. 5 final method cannot 'e overridden in a su'class. 5 final field cannot 'e changed after it&s initiali7ed! and it must include an initiali7er statement where it&s declared. 12). What does it mean that a method or class is abstract? 5n a'stract class cannot 'e instantiated. 5'stract methods may only 'e included in a'stract classes. @owever! an a'stract class is not re<uired to have any a'stract methods! though most of them do. Aach su'class of an a'stract class must override the a'stract methods of its superclasses or it also should 'e declared a'stract. 12-. What is a transient variable? transient varia'le is a varia'le that may not 'e seriali7ed. 120. $ow does Java handle inte!er overflows and underflows? .t uses those low order 'ytes of the result that can fit into the si7e of the type allowed 'y the operation. 122. What is the difference between the ?? and ??? operators? 8he LL operator carries the sign 'it when shifting right. 8he LLL 7ero>fills 'its that have 'een shifted out. 123. Is si)eof a &eyword? 8he si7eof operator is not a /eyword.

Você também pode gostar