Java Impt Questions-1
Java Impt Questions-1
ans)
Almost all Java programs need to interact with the outside world for taking input and supplying
output. There could be three sources from where a program can receive input and send output to.
These sources are console IO, file IO, and network IO. In console IO end user usually supplies
input by keyboard and receives generated output on display screen. In file IO, input is supplied
from and output is redirected to a file stored on disk. In network IO, input comes from a
networked resource and output is sent to the networked resource over the network.
Java programs perform I/O through streams. A stream could be thought as a pipe which is linked
to a physical device at one end and to a Java program at another end by the Java I/O system.
There are following I/O streams supported by Java:
Byte Streams handle I/O of raw binary data and perform input and output of 8-bit bytes. All
byte stream classes are descended from InputStream and OutputStream.
Character Streams handle I/O of character data. The Java platform stores character values
using Unicode conventions. Character stream I/O automatically translates this internal format to
and from the local character set. A program that uses character streams in place of byte streams
automatically adapts to the local character set and is ready for internationalization - all without
extra effort by the programmer.
Buffered Streams optimize input and output by reducing the number of calls to the native API.
In Unbuffered I/O each read or write request is handled directly by the underlying OS. This can
make a program much less efficient, since each such request often triggers disk access, network
activity, or some other operation that is relatively expensive.
To reduce this kind of overhead, the Java platform implements buffered I/O streams. Buffered
input streams read data from a memory area known as a buffer; the native input API is called
only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the
native output API is called only when the buffer is full.
A program can convert an unbuffered stream into a buffered stream, where the unbuffered
stream object is passed to the constructor for a buffered stream class.
This article specially emphasizes on Buffered Streams. We will shortly discuss buffered
streams (BufferedReader class) in detail and their advantages over byte and character streams.
Data Streams handle binary I/O of primitive data type values (boolean, char, byte, short, int,
long, float, and double) as well as String values. All data streams implement either the DataInput
interface or the DataOutput interface.
Object Streams handle binary I/O of objects. Just as data streams support I/O of primitive data
types, object streams support I/O of objects. Most, but not all, standard classes support
serialization of their objects. Those that do implement the marker interface Serializable.
Programs Using Java.io.BufferedReader
A program can convert an unbuffered stream into a buffered stream by passing the unbuffered
stream object to the constructor for a buffered stream class. For example, In Java, console input
is accomplished by reading from System.in. To obtain a character based stream that is attached to
the console, wrap System.in in a BufferedReader object. BufferedReader supports a buffered
input stream. Its most commonly used constructor is shown here:
BufferedReader(Reader inputReader)
Here, inputReader is the stream that is linked to the instance of BufferedReader that is being
created. Reader is an abstract class. One of its concrete subclasses is InputStreamReader, which
converts bytes to characters. To obtain an InputStreamReader object that is linked to System.in,
use the following constructor:
InputStreamReader(InputStream inputStream)
Because System.in refers to an object of type InputStream, it can be used for inputStream.
Putting it all together, the following line of code creates a BufferedReader that is connected to
the keyboard:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
2)Write a program in java to find the factorial value of any given number
3) Write a program in java to find the Fibonacci sequence of any given number
5) explain the Wrapper Classes with example program
6) What is meant by string arrays and string methods? Discuss them with a sample Java Program
7) Discuss clearly about JVM, JDK and Java API
8)What is an anonymous array? Give an example
ans)n array in Java without any name is anonymous array. It is an array just for creating and
using instantly.
We can create an array without name, such type of nameless arrays are called anonymous
array.
The main purpose of anonymous array is just for instant use (just for one time usage) .
Anonymous array is passed as an argument of method
a) text field
b)Menubar
10). What is a socket? How can we read and write information into
the socket? Explain
11) Give brief description about canvases and text areas