flipkart

Saturday, 2 May 2015

JAVA PROGRAMMING MID 2 QUESTIONS

Exception handling
1.An exception is a run-time error
2.Code monitor for exceptions, it must be part of try block.
3.A catch block receives exception.
4.The type of exception in a catch must match the type of exception that you want to catch.
5. An uncaught Exception ultimately leads to abnormal program termination.
6. throw generates an exception.
7.Exception classes are subclasses of what class? Throwable
8. A finally block is the last thing executed when a try/catch block is exited.
9. To print a stack trace,call printStackTrace(), which is defined by Throwable.
10. No throws clause is needed for unchecked exception.
11. In Java, all exceptions are derived from the class
a. Throwable     b.Exception        c.Error   d.Runtime Exception
12. Java’s exception handling mechanism is meant to handle
a. only compile time errors
b. only runtime error
c. both runtime and compile time error
d. only logical error
13.The code contained in _______ block is monitored for exception.
a.try       b. catch                                c. throw               d.finally
14.After execution of a ‘catch’ block, the control goes to
a. the ‘try’ block where the exception was raised and continues execution.
b. just after the ‘catch’ block and continues execution.
c. end of the program and halts the execution displaying the exception message.
d. None of the above is true.
15.Which of the following lists the keywords associated with exception handling?
i. try                       ii.catch                  iii.throw               iv.throws             v.finally
a.i,ii,iii and v only
b. I,ii,iii,iv and v only
c. I,ii and v only
d. I,ii,iii,iv and v

I/O Streams
1. A stream is an abstraction that either produces or consumes information.
2. What type of streams does Java define?  byte and character streams
3. what are the built-in streams? System.in,System.out,System,err
4. What method is used to read a byte from System.in? Read()
5.what method can be used to write a byte of System.out?  write()
6.What does read() return when the end of the file is reached?-1
7. What class is used to read characters from a file?FileReader
8. What class is used to write characters to a file? FileWriter
9.What classes top the character-based stream classes? Reader and Writer
10. To read from console, you will open what type of reader?BufferedReader
11.What streams are used to read and write binary data? DataInputStream and DataOutputStream
12. Which of the following statements is false with reference to Java I/O?
a.Byte and character, two types of streams are available for I/O.
b. At the lowest leve, all I/O is byte-oriented
c.Reader and Writer class streams belong to byte streams.
d. Same I/O classes and methods can be applied to both console I/O and File I/O.
13. Which of the following statements is false with reference to system.in
a.It is a byte-based I/O stream
b. It is a character-based I/O stream
c. It is used for character I/O from and to console.
d.It is predefined stream variable of public, final and static type.
14. Which class object may be used to obtain or manipulate the information associated with a file?
a. FileReader      b.File    c.FileInputStream            d. None of these
4.Which of the following is a character-based console output stream?
a. System.out    b.PrintWriter    c.DataOutputStream      d.None of these
15.’System’ is a class in which of the following Package?
a. Java.lang         b. Java.io             c. Java.util           d.Java.sql
16.Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes
Threads
1. An exception is a run-time error
2. Code monitored for exceptions must be part of what statement? Try
3. What does catch do? Receives exceptions
4. What happens if an exception is not caught? Abnormal program termination
5. One try block be used to handle two or more different types of exception?
6. catch for a superclass exception also catch subclasses of that superclass?
7. An exception not caught by an inner try/catch block moves outward to the enclosing try block.
8. throw   generates an exception
9.  throw throws objects must be instances of valid exception classes.
10. An exception can be rethrown after it is caught
11. Exception classes are subclasses of Throwable class.
12. A finally block is the last thing executed when a try/catch block is exited.
13. printStackTrace() display a stack trace of the events leading up to an exception?
14.When a method generates a checked exception that it does not handle, it must state this fact using a throws  clause.
15.No throws clause is needed for unchecked exceptions.
16. In Java, all exceptions are derived from the class
a. Throwbale     b.Exception        c.Error   d.Runtime Exception
17. Java’s exception handling mechanism is meant to handle
a. only compile time errors          b.only runtime error      c.both runtime and compile time errors                d.only logical errors
18.The code contained in _______ block is monitored for exception
a.try       b.catch                 c.throw                                d.finally
19.After execution of a ‘catch’ block, the control goes to
a. the ‘try’ block where the exception was raised and continues execution
b. just after the ‘catch’ block and continues execution
c. end of the program and halts the execution displaying the exception message
d. None of the above is true
20.Which of the following lists the keywords associated with exception handling?
i.try        ii.catch  iii. Throw              iv.throws             v.finally
a. I,ii,iii and v only
b.i,ii,iii,iv and v only
c. I,ii and v only
d. I,ii,iii, iv and v
21. What method is used to read a byte from System.in? read()
22. What method can be used to write a byte to System.out? write()
23. What are the built-in streams? System.in,System.out,System.err
24. What types of streams does java define? Byte and Character streams



Applets
1. An Applet is a special type of java program that is designed for transmission over the Internet and that runs inside a browser.
2. The Paint() method displays output in an AWT-based applet’s window.
3. The package java.applet must be included when creating an applet.
4. Applets are executed by a browser or by special tools, such as appletviewer.
5. When start() method called, the applet must be started, or restared.
6. When stop() is called, the applet must be paused.
7. What are the four lifecycle methods that most applets will override?
Init(),start(),stop() and destroy().
8.showStatus() displays output in the status window
9.What method is used to obtain a parameter specified in the APPLET tag?
getParameter().
10.Which of the following statements is true about Java Applet?
a.They use the resources of client machine in order to execute.
b. They may be GUI or Stream I/o based.
c. They may be based on AWT or Swing.
d. They do not contain a main() method.
11. Which of the following classes is inherited by a Java Applet?
a. Applet b. String c. Graphics          d. All of the these
12. Which of the following methods causes an applet to terminate?
a. destroy()    b. kill() c.stop()            d. Any of these
13. Which of the following methods is used to show banner scrolled in an applet?
a. init()             b.paint()           c.repaint()      d. println()
14. Which of the following statement is false about native methods?
a.These are subroutines written in language other than Java.
b.Native methods can be called from inside Java Program using native keyword.
c. Native method cannot be called from inside java programs. Rather, they should be provided at runtime.
d. Native methods are often written in C language.
16.Suppose in a generic class ‘Gen’,T is a type parameter. Which of the following declares the generic ‘Gen’ class correctly?                                Class Gen  <T> {
13.A condition that must be true before a method call will work correctly is called a precondition
Swings
1.All Swing components must be stored in a Container
2.What Swing class creates a label? JLabel
3. What package must be included in all Swing programs? javax.swing
4.All code that creates or modifies the GUI must be executed on the event-dispatching thread
5.The delegation event model is based on event sources and event listeners
6. What is the name of the event listener interface for action event? Action Listener
7.What class creates a Swing push button? JButton
8.When the user presses enter while within a JTextField, what event is generated
ActionEvent
9.Which of the following is the default layout manager of the content pane?
a. flow Layout             b.Border Layout        c.Grid Layout  d.Box Layout
10. Which package must be included in all swing programs:
a. java.swing               b.javax.swing                        c.java.swingsx                        d.javax.swingx
11. The method to receive and process the event is implemented by
a.Event Source
b. Event Listener
c. Both (a) and (b) above
d.None of the above
MID 10 MARKS QUESTIONS
1. Write Difference Between Unchecked Exception and Checked Exception?
2. Write about Life Cycle of the Thread?
3. Why thread is called light weight task and process heavy weight task.
4. How are the exceptions handled in java?
5. Define Interface? Briefly?
6. What is package? How do you create a package? Explain about the access
Protection in packages?
7. Define Applet? Difference between Application and Applet?
8. Explain Buffered Reader and BufferedWriter classes from I/O package with an example.
9. Difference b/w class and interface in java
10. Explain FileInputStream and FileOutputStream with an example for each.
11.What are Checked Exceptions and Unchecked Exceptions? Explain some of These exceptions with an example and also give the differences between them.

12.Write short notes on  passing parameters to applets
13. Briefly explain the following terms:
    (a) Component.    (b) Container.    (c) Panel.    (d) Window.                                                        

                                            MID 2M QUESTIONS
What is a IO stream?
It is a stream of data that flows from source to destination. Good example is file copying. Two streams are involved – 
input stream and output stream. An input stream reads from the file and stores the data in the process (generally in a temporary variable). The output stream reads from the process and writes to the destination file
·  What is a IO stream?
It is a stream of data that flows from source to destination. Good example is file copying. Two streams are involved – 
input stream and output stream. An input stream reads from the file and stores the data in the process (generally in a temporary variable). The output stream reads from the process and writes to the destination file.
·  What is the necessity of two types of streams – byte streams and character streams?
Byte streams were introduced with JDK 1.0 and operate on the files containing ASCII characters. We know Java supports other language characters also known as Unicode characters. To read the files containing Unicode characters, the designers introduced character streams with JDK 1.1. As ASCII is a subset of Unicode, for the files of English characters, we can go with either byte streams or character streams.
·  What are the super most classes of all streams?
All the byte stream classes can be divided into two categories (input stream classes and output stream classes) and all character streams classes into two (reader classes and writer classes). There are 
four abstract classes from which all these streams are derived. The super most class of all byte stream classes is java.io.InputStream and for all output stream classes, java.io.OutputStream. Similarly for all reader classes is java.io.Reader and for all writer classes isjava.io.Writer.
·  What are FileInputStream and FileOutputStream?
These 
two are general purpose classes used by the programmer very often to copy file to file. These classes work well with files containing less data of a few thousand bytes as by performance these are very poor. For larger data, it is preferred to use BufferedInputStream (or BufferedReader) and BufferedOutputStream (or BufferedWriter).
·  Which you feel better to use – byte streams or character streams?
I feel personally to go with character streams as they are the latest. Many features exist in character streams that do not in byte streams like a) using BufferedReader in place of BufferedInputStreams and DataInputStream (one stream for two) and b) using newLine() method to go for next line and for this effect we must go for extra coding in byte streams etc.
·  What System.out.println()?
"println()" is a method of PrintStream class. "out" is a static object of PrintStream class defined in "System" class. 
System is a class from java.lang package used to interact with the underlying operating system by the programmer.
·  What are filter streams?
Filter streams are a category of IO streams whose responsibility is to add extra functionality (advantage) to the existing streams like giving 
line numbers in the destination file that do not exist int the source file or increasing performance of copying etc.
·  Name the filter streams available?
There are four filter streams in 
java.io package – two in byte streams side and two in character streams side. They are FilterInputStreamFilterOutputStreamFilterReader and FilterWriter. These classes are abstract classes and you cannot create of objects of these classes.
·  Name the filter stream classes on reading side of byte stream?
There are four classes – LineNumberInputStream (the extra functionality is it adds line numbers in the destination file), DataInputStream (contains special methods like readInt(), readDouble() and readLine() etc that can read an int, a double and a string at a time), BufferedInputStream (gives buffering effect that increases the performance to the peak) and PushbackInputStream (pushes the required character back to the system).
·  What is the functionality of SequenceInputStream?
It is very useful to copy multiple source files into one destination file with very less code.
·  What is PrintStream and PrintWriter?
Functionally both are same but belong to two different categories – 
byte streams and character streams. println() method exists in both classes.
·  Which streams are advised to use to have maximum performance in file copying?
BufferedInputStream and BufferedOutputStream on byte streams side and BufferedReader and BufferedWriter on character streams side.
·  What are piped streams?
There are four piped streams – PipedInputStream, PipedOutputStreamPipedReader and PipedWriter. These streams are very useful to pass data between two running threads (say, processes).
·  What is File class?
It is a non-stream (not used for file operations) class used to know the properties of a file like when it was created (or modified), has read and write permissions, size etc.
·  What is RandomAccessFile?
It is a special class from java.io package which is neither a input stream nor a output stream (because it can do both). It is directly a subclass of Object class. Generally, a stream does only one purpose of either reading or writing; but 
RandomAccessFile can do both reading from a file and writing to a file. All the methods of DataInputStream and DataOutStream exist in RandomAccessFile

1) What is multithreading?
Multithreading is a process of executing multiple threads simultaneously. Its main advantage is:
  • Threads share the same address space.
  • Thread is lightweight.
  • Cost of communication between process is low.

What is thread?

A thread is a lightweight subprocess.It is a separate path of execution.It is called separate path of execution because each thread runs in a separate stack frame.

What is difference between wait() and sleep() method?

wait()
sleep()
1) The wait() method is defined in Object class.
The sleep() method is defined in Thread class.
2) wait() method releases the lock.
The sleep() method doesn't releases the lock.
13) What is the purpose of Synchronized block?
  • Synchronized block is used to lock an object for any shared resource.
  • Scope of synchronized block is smaller than the method.

16)What is the difference between notify() and notifyAll()?

The notify() is used to unblock one waiting thread whereas notifyAll() method is used to unblock all the threads in waiting state.

80)What is difference between throw and throws?

throw keyword
throws keyword
1)throw is used to explicitly throw an exception.
throws is used to declare an exception.
2)checked exceptions can not be propagated with throw only.
checked exception can be propagated with throws.
3)throw is followed by an instance.
throws is followed by class.
4)throw is used within the method.
throws is used with the method signature.
5)You cannot throw multiple exception
You can declare multiple exception e.g. public void method()throws IOException,SQLException.


Why string objects are immutable in java?

Because java uses the concept of string literal. Suppose there are 5 reference variables,all referes to one object "sachin".If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.

What is the purpose of toString() method in java ?

The toString() method returns the string representation of any object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.

What is nested class?

A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class.

What is difference between abstract class and interface?

Abstract class
Interface
1)An abstract class can have method body (non-abstract methods).
Interface have only abstract methods.
2)An abstract class can have instance variables.
An interface cannot have instance variables.
3)An abstract class can have constructor.
Interface cannot have constructor.
4)An abstract class can have static methods.
Interface cannot have static methods.
5)You can extends one abstract class.
You can implement multiple interfaces.

What is package?

A package is a group of similar type of classes interfaces and sub-packages. It provides access protection and removes naming collision.
What is an applet?
Ans. Applet is a program which can get downloaded into a client environment and start executing there.
What is the sequence for calling the methods by AWT for applets?
Ans. When an applet begins, the AWT calls the following methods, in this sequence: 

► init() 
► start() 
► paint() 
 Can we pass parameters to an applet from HTML page to an applet? How?
Ans. We can pass parameters to an applet using tag in the following way: 

► <param name="param1″ value="value1″> ► <param name="param2″ value="value2″> 
What is AWT?

Ans. AWT stands for Abstract Window Toolkit. AWT enables programmers to develop Java applications with GUI components, such as windows, and buttons. The Java Virtual Machine (JVM) is responsible for translating the AWT calls into the appropriate calls to the host operating system.