Você está na página 1de 22

1. How do you create a Reader object from an inputstream object? a.

use the static createReader() method of the inputstream class. b. use the static createReader() method of the reader class c. create an inputstreamreader object, passing the inputstream object as an argument to the inputstreamreader constructor(*) d. create an outputstreamreader object, passing the inputstream object as an argument to the outputstreamreader constructor. 2. which of the following are valid identifiers? a. _id(*) b. $id(*) c. #id d. -id 3. which of the following are true? a. the sleep() method puts a thread in the ready state. b. the yield() method puts a thread in the waiting state. c. the suspend() method is the preferred method for stopping a thread's execution. d. a thread's interrupt() method results in the throwing of the interruptedexception.(*) 4. which of the following classes have a paint() method? a. canvas(*) b. image c. frame(*) d. graphics 5. which statement creates textarea with 10 rows and 20 columns? a. new TextArea(10,20)(*) b. new TextArea(20,10) c. new TextArea(Rows(10),Cols(20)) d. new TextArea(Row(10),Col(20)) 6. which of the following are true? a. the AWT automatically causes a window to be repainted when a portion of a window has been minimized and then maximized.(*) b. the AWT automatically causes a window to be repainted when a portion of a window has been covered and then uncovered.(*) c. the AWT automatically causes a window to be repainted when application data is changed. d. the AWT does not support repainting operations.

7. which of the following are java keywords? a. null b. NULL c. extends(*) d. main

8. which of the following thread state transitions are valid? a. from ready to running(*) b. from running to ready(*) c. from running to waiting(*) d. from waiting to running e. from waiting to ready(*) f. from ready to waiting

9. which of the following is not a wrapper class? a. string(*) b. integer c. boolean d. character

10. which is the advantage of encapsulation? a. only public methods are needed b. no exceptions need to be thrown from any method c. making the class final causes no cnsequential changes to other code. d. it changes the implementation without changing the interface and causes no consequential changes to other code.(*) e. it changes the interface without changing the implementatino and causes no consequential changes to other code.

11. which font attributes are available through the FontMetrics class? a. ascent(*) b. leading(*) c. case d. height(*)

12.which of the following are true?

a. component extends container b. menuitem extends component c. container extends component(*) d. menucomponent extends component

13. what is the ovtal equivalent of the decimal value 123? a. 0173(*) b. 123 c. 0x123 d. 0x173

14. in order for the public class Myclass to successfully compile, which of the following are true? a. myclasss must have a correctly formed main method b. myclass must be defined in the file myclass.java(*) c. myclass must be defined in the myclas package. d. myclass must be imported

15. which of the following are true? a. any unicode character is represented using 16 bits b. seven bits are needed to represent any ASCII character c. UTf-8 characters are represented using only eight bits. d. UTF-16 characters are represented using only 16 bits.

16. which of the following are true about a dead thread? a. the thread's object is discarded. b. the thread must wait until all other threads execute before it is restarted c. the thread cannot be restarted(*) d. the thread is synchornized

17. what is the output displayed by the following program? class question { public static void main(String args[]) { int n=7; n<<=3; n=n&n+1|n+2^n+3; n>>=2; System.out.println(n);

} } a. 0 b. -1 c. 14(*) d. 64

18. which of the following are true? a. a component may handle its own events by adding itself as an even listener(*) b. a component may handle its own events by overriding its event-dispatching method.(*) c. a component may not handle its own events d. a component may handle its own events only if it implements the handleEvent() method

19. what can contain objects that have a unique key field of string type, if it is required to retrieve the objects using that key field as an inded? a. map(*) b. set c. list d. collection e. enumeration

20. what output is displayed by the following program? import java.io.*; public class TestIOApp { public static void main(String args[]) throws IOException{ StringReader stringin = new StringReader("test") LineNumberReader in = new LineNumberReader(stringin); PrintWriter out = new PrintWriter(System.out); out.println(in.readLine()); out.flush(); } } a. test(*) b. 1. test c. 1: test d. 1 test

21. which of the following are characteristic of a fully encapsulated class? a. all variables are private(*) b. all methods are private c. methods are provided to access the class's properties(*) d. the class's design may be changed with minimal impact on its implementation

22. which of the following are true about the finally clause of a try-catch-finally statement? a. it is only executed after a catch clause has executed b. it is only executed if a catch clause has not executed c. it is always executed unless its thread terminates(*) it is only executed if an exception is thrown

23. which layout stacks components on tope of each other? a. cardlayout(*) b. null layout c. borderlayout d. setlayout

24. which of the following components generate action events? a. buttons(*) b. labels c. checkboxes d. windows

25. which methods will cause a frame to be displayed? a. show() (*) b. setVisible() (*) c. display() d. displayFrame()

26. what is the range of the short type? a. 0 to 2^16 b. -(2^16) to 2^16

c. -(2^15) to 2^15 d. -(2^15) to 2^15-1 (*)

27. what is the value of a[3] as the result of the following array declaration? int[] a={1,2,3,4,5}; a.1 b.2 c.3 d.4(*)

28. whati s the output of the following program? public class question { public static void main(String args[]) { boolean[] b = new boolean[2]; double[] d = new double[2]; System.out.print(b[0]); System.out.println(d[1]); a. true0.0 b. true0 c. false0.0 (*) d. false0

29. what is the output of the following program? class question { static int i=1,j=2; static { display(i); } public static void main(String args[]) { display(j); static void display(int n) { system.out.print(n); } } a. 1 b. 2

c. 12(*) d. 21

30. which of the following are true? a. a top-level class may be declared as private b. a method may be declared as transient c. a constructor may be declared as volatile d. a local variable may be declared as final(*)

31. when two or more objects are added as listners for the same event, which listener is first invoked to handle the event? a. the first object that was added as a listener b. the last object that was added as a listener c. there is no way to determine which listener will be invoked first.(*) it is impossible to have more than one listener for a given event.

32. which of the following are true? a. a reference to an array can be cast to a reference to an object(*) b. a reference to an array can be cast to a reference to a cloneable(*) c. a reference to an array can be cast to a reference to a string d. none of the above

33. which layout is used to layout the components of a container in a grid where each row and column are the same size? a. gridlayout b. null layout c. gridbaglayout d. gridbagconstraintlayout

34. what is the output of the following program? class outer { string s = "outer"; public static void main(string args[]) { new outer().new Inner(); } outer() { system.out.prints(s); } class inner { string s = "inner"; inner() { system.out.print(s); } } } a. outer b. inner c. outerinner(*) d. innerouter

35. what is the result of the expression 5.4 + "3.2" ? a. the double 8.6 b. the string 8.6 c. the long value 8 d. the string 5.43.2(*)

36. which are true about the container class? a. the validate() method is used to cause a container to be laid out and redisplayed (*) b. the add() method is used to add a component to a container (*) c. the getborder() method returns information about a containers insets d. the getcomponent() method is used to access a component that is contained in a container (*)

37. which of the following are true about this method declaration? void mymethod(string s) { } a. mymethod() is static b. mymethod does not return a value(*) c. mymethod is abstract d. mymethod may not be accessed outside of a package in which it is declared(*)

38. which lines of output are displayed by the following program? class question{ public static void main(string[] args) { for(int i=0;i<10;i++) { try { try { if (i%3==0) throw new Exception("E0"); system.out.println(i); } catch(exception inner) { i *=2; if(i%3==0) throw new exception("E1"); } finally { ++i; } } catch(exception outer) { i+=3; } finally { --i; } } } } a. 4(*) b. 5(*) c. 6 d. 7 e. 8

f. 9

39. if you run the following program, what lines would be included in its output? class question{ public static void main(string args[]) { int i,j; for(i=0,j=0;i+j<20;++i,j+=i--) { system.out.println(i+j); } } } a. 5(*) b. 8(*) c. 13(*) d. the program cannot be compiled because the for statement's syntax is incorrect.

40. which of the following are direct or indirect subclasses of component? a. button(*) b. label(*) c. checkboxmenuitem d. toolbar e. frame(*)

41. which layout is the defauld layout of a panel object? a. gridlayout b. flowlayout(*) c. gridlinelayout d. linelayout

43. which of the following are true?

a. java only supports preemptive scheduling. b. java only supports time slicing. c. the jvm has been implemented on os that use time slicing(*) d. the jvm has been implemented on os that use preemptive scheduling.(*)

44. what should you use to position a button within an application frame so that the size of the button is not affected by the frame size? a. flowlayout(*) b. gridlayout c. the center area of a borderlayout d. the east or west area of a borderlayout e. the north or south area of a borderlayout

45. a catch clause may catch exceptions of which types? a. thowable type(*) b. error type(*) c. exception(*) d. string

46. what is the value displayed by the following program? class question{ public static void main(string[] args) { int x=0; boolean b1,b2,b3,b4; b1= b2= b3= b4= true; x = (b1|b2&b3^b4)?x++:--x; system.out.println(x) } } a. 0(*) b. -1 c. 1

d. none of the above

47. which method is used to set the text of a label object? a. settext() (*) b. setlabel() c. settextlabel() d. setlabeltext()

48. which of the following methods cause the string object referenced by s to be changed? a. s.concat() b. s.touppercase(0 c. s.replace() d. none of the above(*)

49. in order for teh myprogram proram to be compiled and run, which of the followin must be true? a. the myprogram class must be defined in myprogram.java b. myprogram must be declared as public c. myprogram must have correctly formed main() method(*) d. myprogram must import java.lang

50. which of the followin are true? a. textcomponent extends textarea b. textarea extends textfield c. textfield extends textcomponent(*) d. textcomponent extends textfield d. textarea extends textcomponent(*)

51. what is the value of 111%13? a. 11 b. 5 c. 6

d. 7(*)

52. which of the following are methods of the graphics class? a. drawrect() (*) b. drawimage() (*) c. drawpoint() d. drawstring() (*)

53. if you run the following program, which lines would be included in its output? class question { public static void main(string[] args) { int i=1; int j=2; outer: while(i<j) { ++i; inner: do { ++j; if (j%3==0) continue outer; if (i%3==0) break inner; if (i%3==1) break outer; system.out.println(i*j); } while (j < i); system.out.println(i+j); } } } a. 5 b. 6 c. 7(*) d. the program does not display any output.

54. what is the output of the following program? class s1{ public static void main(string[] args) { new s2(); }

s1() { system.out.pritn("s1"); } } class s2 extends s1 { s2() { system.out.print("s2"); } } a. s1 b. s2 c. s1s2(*) d. s2s1

55. what is the range of char type? a. 0 to 2^16 b. 0 to 2^16-1(*) c. 0 to 2^15 d. 0 to 2^15-1

56. which of the following methods are methods of math class? a. absolute() b. cosine() c. sine() d. log() (*)

57. which of the following are true about local inner classes? a. they are not associated with any instance of an outer class.(*) b. they may access final initialized variables and parameters that are in the scope of the statement block in which the class is declared(*) c. they may be declared public, protected or private d. they may not implement an interface

58. when a thread blocks on I/O, which of the following are true? a. the thread enters the ready state. b. the thread enters the dead state c. no other thread may perform I/O d. the thread enters the waiting state(*)

59. what is the output of the following program when it is invoked using the command line java Test this is a test? class Test { public static void main(String arg[]) { system.out.println(arg[1]); } } a. this b. is(*) c. a d. test?

60. a frame's background color is set to color.yellow and button's background color is set to colr.blue. suppose the button is added to a panel, which is added to the frame. what background color will be used wth the panel? a. color.yellow(*) b. color.blue c. color.green d. color.white

30 Java Interview Questions [ Collected by Prabhu datta Praharaj, cool.prabhu@gmail.com ] *Q1. How could Java classes direct program messages to the system console, but error messages, say to a file?

A. The class System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both point at the system console. This how the standard output could be re-directed: Stream st = new Stream(new FileOutputStream("output.txt")); System.setErr(st); System.setOut(st); *Q2. What's the difference between an interface and an abstract class? A. An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class. *Q3. Why would you use a synchronized block vs. synchronized method? A. Synchronized blocks place locks for shorter periods than synchronized methods. *Q4. Explain the usage of the keyword transient? A. This keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers).

*Q5. How can you force garbage collection? A. You can't force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately. *Q6. How do you know if an explicit object casting is needed? A. If you assign a superclass object to a variable of a subclass's data type, you need to do explicit casting. For example: Object a; Customer b; b = (Customer) a; When you assign a subclass to a variable having a supeclass type, the casting is performed automatically. *Q7. What's the difference between the methods sleep() and wait() A. The code sleep(1000); puts thread aside for exactly one second. The code wait(1000), causes a wait of up to one 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. *Q8. Can you write a Java class that could be used both as an applet as well as an application?

A. Yes. Add a main() method to the applet. *Q9. 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. *Q10. Can you call one constructor from another if a class has multiple constructors A. Yes. Use this() syntax. *Q11. Explain the usage of Java packages. A. This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes. *Q12. If a class is located in a package, what do you need to change in the OS environment to be able to use it? A. You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable. Let's say a class Employee belongs to a package com.xyz.hr; and is located in the file c:\dev\com\xyz\hr\Employee.java. In this case, you'd need to add c:\dev to the variable CLASSPATH. If this class contains the method main(), you could test it from a command prompt window as follows: c:\>java com.xyz.hr.Employee *Q13. What's the difference between J2SDK 1.5 and J2SDK 5.0? A.There's no difference, Sun Microsystems just re-branded this version.

*Q14. What would you use to compare two String variables - the operator == or the method equals()? A. I'd use the method equals() to compare the values of the Strings and the == to check if two variables point at the same instance of a String object.

*Q15. Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written? A. Yes, it does. The FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first. *Q16. Can an inner class declared inside of a method access local variables of this method?

A. It's possible if these variables are final. *Q17. What can go wrong if you replace && with & in the following code: String a=null; if (a!=null && a.length()>10) {...} A. A single ampersand here would lead to a NullPointerException. *Q18. What's the main difference between a Vector and an ArrayList A. Java Vector class is internally synchronized and ArrayList is not.

*Q19. When should the method invokeLater()be used? A. This method is used to ensure that Swing components are updated through the eventdispatching thread.

*Q20. How can a subclass call a method or a constructor defined in a superclass? A. Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass's constructor For senior-level developers: **Q21. What's the difference between a queue and a stack? A. Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule **Q22. You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces? A. Sometimes. But your class may be a descendent of another class and in this case the interface is your only option. **Q23. What comes to mind when you hear about a young generation in Java? A. Garbage collection. **Q24. What comes to mind when someone mentions a shallow copy in Java? A. Object cloning. **Q25. If you're overriding the method equals() of an object, which other method you might also consider?

A. hashCode() **Q26. You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList? A. ArrayList **Q27. How would you make a copy of an entire Java object with its state? A. Have this class implement Cloneable interface and call its method clone(). **Q28. How can you minimize the need of garbage collection and make the memory use more effective? A. Use object pooling and weak object references. **Q29. There are two classes: A and B. The class B need to inform a class A when some important event has happened. What Java technique would you use to implement it? A. If these classes are threads I'd consider notify() or notifyAll(). For regular classes you can use the Observer interface. *Q30. What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it? A. You do not need to specify any access level, and Java will use a default package access level.

Frequently asked Questions in Java Interviews


74 rate or flag this page By Saurabh G

Java Interview Questions Core JAVA Interview Questions You'll Most Likely Be Asked Amazon Price: $17.04 List Price: $19.95 Java/J2EE Job Interview Companion Amazon Price: $32.00 List Price: $38.05 Cracking the Coding Interview, Fourth Edition: 150 Programming Interview Questions and Solutions Amazon Price: $28.80 List Price: $40.00 Java / J2EE Interview Questions You'll Most Likely Be Asked Amazon Price: $18.39 List Price: $19.95 Core JAVA Interview Questions You'll Most Likely Be Asked Amazon Price: $9.99

Advanced JAVA Interview Questions You'll Most Likely Be Asked Amazon Price: $9.99

Difference between C and C++ with Java


There are many frequently ask questions on Java Technology. I am discussing first interview question on Java Technology asked by every interviewer. The question is about Difference between Java, C & C++. Difference between Java and C

Java is not like C but the major difference between Java and C is that Java an object Oriented language. It has a mechanism to define classes and objects. In effort to build a simple and safe language, the Java team did not include some of the C features in Java.

Java does not include the c unique statement keywords sizeof and typedef. Java does not contain the data types struct and union. Java does support an Explicit pointer type. Java does not define the type modifiers keywords auto, extern, register, signed and unsigned. Java does not not have a preprocessor and therefore we can not use # define,

# include, #ifdef.

Java requires function with no arguments and must be declared with no parenthesis and not with the void keywords as done in C. Java adds new operator such as instanceof and >>>. Java adds labeled break and continue statements. Java adds many features required for object oriented programming.

Java and C++ Java is true object oriented language while C++ is basically C with object oriented extension. That is why the increment operator ++ indicates. C++ has maintained backward compatibility with C. It is therefore possible to write old style C program and Compile and run it under C++. Java appears similar to C++ when we consider only extension part of C++. However some object oriented features of C++ make the C++ code extremely difficult to follow and maintain. Following are features are omitted from Java Technology.

Java does not support operator overloading. Java does not have template classes as in C++. Java does not support multiple inheritance of classes. This is accomplished using a new feature called inheritance. Java does not support global variables. Every variable and method is declared with in a class and forms part of that class. Java does not use pointers Java has replaced the destructor function with a finalize() function. There are no header files in Java.

Java also adds some new features. While C++ is superset of C, Java is neither a superset nor a subset of C or C++. Beside this Java adds its own new features. They are as follows.

Java supports Multithreading. Java supports automatic Garbage collection and make lot of programming problems simply vanish. The destructor function is replaced with a finalize function.

Exception handling in Java is different because there is no Destructor. A finally clause is always executed to perform necessary cleanup. Java has built in support for comment documentation, so the source code file can also contain its own documentation.