Vous êtes sur la page 1sur 11

1. Why is Java known as a platform-neutral language?

Ans.
Java is known as platform-neutral language because Java's bytecodes are
designed to be read, interpreted, and executed in exactly the same manner
on any computer hardware or operating system that supports a Java run-
time.
2. How is Java more secure than other languages?
Ans.
One of the potential terrors of the Internet is the possibility of security
breaches- viruses that infect your computer, or hackers who take advantage
of a software glitch to invade your personal cyberspace and make off with
confidential information.
Applets, which are Java programs, are automatically downloaded when a
Web page is displayed. Java applets, by default, execute within the Java
executing environment and are limited to the environment. This means, an
applet cannot access the local file system or other programs of the system
where it executes. This reduces the chance that simply viewing someone's
page might harm your system or data. No system is absolutely reliable and
none will ever be; but Java represents the state-of-the-art in reducing the
chances of a disaster. 3. What is multithreading? How does it improve the
performance of Java?
Ans.
In a multithreading environment, a thread is the smallest unit of
dispatchable code. This means that a single program can perform two or
more tasks simultaneously. For instance a text editor can format text at the
same time that it is printing.
The benefit of Java's multithreading is that the main loop/polling
mechanism is eliminated. One thread can pause without stopping other
parts of the program. For example, the idle time created when a thread
reads data from a network or waits for user input can be utilized elsewhere.
When a thread blocks in a Java program, only the single thread that is
blocked pauses. All other threads continue to run.
2. What is a bytecode ?
Ans:
Bytecode is a compiled format of a Java program. Once a Java program is
converted into bytecode it can be transferred across the network and
executed by a Java Virtual Machine (JVM).

3. What is an appletviewer?
Ans:
An appletviewer allows you to run an applet without the overhead of
running a Web browser.

4. Define Garbage Collection.


Ans:
Garbage Collection is a process that automatically frees the memory of
objects that are no longer in use. There is no specification of a technique
for garbage collection. The implementation has been left to vendors1.
What is object-oriented programming?
How is it different from the procedure-oriented programming?
Ans.
Object- oriented programming is a method of implementation in which
programs are organized as co-operative collection of objects, each of which
represents an instance of some class and whose classes all members of a
hierarchy of classes united in inheritance relationships.
With procedural programming you are able to combine returning sequences
of statements into one single place. A procedure call is used to invoke the
procedure. After the sequence is processed, flow of control proceeds right
after the position where the call was made
8. Why is the main() method in Java declared static?
Ans.
The keyword static helps to specify that the main() method can be called
without instantiating an object of a class. This is necessary because the
main() method is called by the Java interpreter before any objects are
created.
public static void main (String args[])
{//code}
After specifying the keyword static, you specify the void keyword. This
keyword indicates to the compiler that, the main() method does not return a
value.
9. What is an applet?
Ans.
Applets are small Java programs developed for Internet applications. An
applet located on a distant computer (Server) can be downloaded via
Internet and executed on a local computer (Client) using a Java-capable
browser.
10. Explain with a simple example how to create, compile and run a
program in Java.
Ans.
Java program involves two steps:
1. Compiling source code into bytecode using javac compiler
2. Executing the bytecode program using java interpreter
For example, consider the following program
class ProgramDemo
{
public static void main (String args[])
{
System.out.println("Here is your string");
}
}
First, save the program as ProgramDemo.java. Then, to compile this
program type the following command at command prompt:
C:\Dir1> javac ProgramDemo.java.
Once the program compiles successfully run this program by typing the
following command at command prompt:
C:\Dir1> java ProgramDemo 5. What is type casting? Why is it required in
programming?
Ans.
The process of converting one data type to another is called casting.
Type variable1 = (type) variable2;
Examples:
int m = 50;
byte n = (byte)m;
long distance = (long)m;
Type casting is required in programming when you want to assign the
value of one variable to another variable of different data type.
10. Write a program to convert the given temperature in Fahrenheit to
Celsius using the following conversion formula
F-32
C = 1.8 and display the values in a tabular form.
Ans.
public class Celsius
{
double celsius;
double fahrenheit;
public Celsius()
{
fahrenheit=98.4;
}
public void convert()
{
celsius=(fahrenheit-32)/1.8;
System.out.println("Temperature in celsius: "+celsius);
}
public static void main(String a[])
{
Celsius object=new Celsius();
object.convert();
}
}
1. Compare in terms of their functions, the following pairs of statements:
(a) while and do........while.
(b) while and for.
(c) break and continue.
Ans.
(a) The difference between the do-while statement and the while statement
is that in the while statement, the loop body is executed only when the
condition stated in the statement is true. In the do-while loop, the loop body
is executed at least once, regardless of the condition evaluating to true or
false. The while loop is also called the top tested loop whereas the do-while
loop is also called the bottom tested loop.
(b) In the for loop, three sections, initialization, test condition and
increment/decrement are placed in the same line whereas in the while loop,
all three sections are placed in three different places in a program. In the for
loop, more than one variable can be initialized, tested and incremented at a
time.
(c) The continue statement stops the current iteration of a loop and
immediately starts the next iteration of the same loop. When the current
iteration of a loop stops, the statements after the continue statement in the
loop are not executed. The break statement immediately terminates the
loop, bypassing the conditional expression and any remaining code in the
body of the loop. When a break statement is encountered inside the loop,
the loop is terminated and program control resumes the next statement
following the loop. 2. Why are arrays easier to use compared to a bunch of
related variables?
Ans.
An array is a sequence of logically related data items. It is a kind of row
made of boxes, with each box holding a value.
Arrays have following advantages over bunch of related variables:
1 Arrays of any type can be created. They can have one or more dimensions.
2 Any specific element can be indexed in an array by its index.
All like type variables in an array can be referred by a common name

2. Distinguish between Multiprocessing and Multithreading.


Ans.
Multiprocessing: Refers to a computer system's ability to support more than
one process (program) at the same time. Multiprocessing operating systems
enable several programs to run concurrently Multiprocessing systems are
much more complicated than single-process systems because the operating
system must allocate resources to competing processes in a reasonable
manner.
Multithreading : The ability of an operating system to execute different
parts of a program, called threads, simultaneously. The programmer must
carefully design the program in such a way that all the threads can run at
the same time without interfering with each other.
4. What is Synchronization? Why do we use it?
Ans.
Multithreading introduces a synchronous behavior in programs. There is a
need to enforce synchronicity when it is necessary. For example, if two
threads are to communicate and share a data structure, there is a need to
avoid conflict between them. That is, a thread must be prevented from
writing data while the other thread is reading the data.
To overcome this problem, Java implements a model of interprocess
communication called monitor. The monitor is a control mechanism that
can hold only one thread at a time. Once a thread enters a monitor, all other
threads have to wait until that exits from the monitor

1. Why is Java known as platform neutral language and how


is Java more secured than other languages?
Ans. Java is the first programming language that is not tied to any
other particular hardware or operating system. Programs developed
in Java can be executed anywhere on any system. We can call Java
as a revolutionary technology because it has brought in a
fundamental shift in how we develop and use programs. Nothing
like this has happened to the software industry before. Thus, Java
known as “platform neutral language”.
Applets, the Java programs which automatically downloaded when a
web page is displayed, are subject to a number of limitations that
are designed to reduce the chance that simply viewing someone’s
page might result in harm to user’s system or data. No such system
is absolutely reliable non will ever be; but Java represents the state-
of-the-art in reducing the chances of a disaster. So, Java is more
secured than other languages.
5.List the eight basic data types used in Java. Give
examples.

The eight basic data types used in java are:


byte: It is the smallest integer type. This is a signed 8-bit type and
has a range from -128 to 127. For example, the following
declaration declares two variables B and C of type byte.
byte b,c;
b =2;
c = -114;
short: It is a signed 16-bit type and has a range from -32,768 to
32,767. For example, the following declaration declares variable K
of type short.
short k;
k = 2;
int: It is a signed 32-bit type and has a range from
-2,147,483,648 to 2,147,483,647.
For example,
int x = 10;
int j = 98;
long: This is signed 64-bit type and has a range from -263 to 263
-1.
For example,
long ds = 1000;
long se;
se =ds * 24 * 60 * 60;
double: It uses 64 bits to store a value.
For example,
double P, R;
P = 10.8;
R =3.14215;
float: It uses 32 bits to store a value.
For example,
float x;
x = -1111; int : It uses 32 bits to store a value.
For example,
Int score;
Score=90;
char: this data type is used to store characters. It is 16-bit type and
has a range from 0 to 65,536.
For example,
char c1,c2;
c1 =84;
c2 ='g';
boolean: it can have only one of two possible values, true or
false.
For example,
boolean flag;
flag= false;
11. What is an array?
Ans.
An array is a sequence of logically related data
items. It is a kind of row made of boxes, with
each box holding a value. The number
associated with each box is the index of the
item. Each box can be accessed by, first box,
second box, third box, and so on, till the nth
box. The first box, or the lowest bound of an
array is always zero, which means, the first item
in an array is always at position zero of that
array. Position in an array is called index. So the
third item in an array would be at index 2 (0, 1,
2).
1. Compare in terms of their functions, the following pairs of statements:
(a) while and do........while.
(b) while and for.
(c) break and continue.
Ans.
(a) The difference between the do-while statement and the while statement is that in the while
statement, the loop
body is executed only when the condition stated in the statement is true. In the do-while loop,
the loop body is
executed at least once, regardless of the condition evaluating to true or false. The while loop is
also called the top
tested loop whereas the do-while loop is also called the bottom tested loop.
(b) In the for loop, three sections, initialization, test condition and increment/decrement are placed in
the same line
whereas in the while loop, all three sections are placed in three different places in a program. In
the for loop,
more than one variable can be initialized, tested and incremented at a time.
(c) The continue statement stops the current iteration of a loop and immediately starts the next
iteration of the same
loop. When the current iteration of a loop stops, the statements after the continue statement in
the loop are not
executed. The break statement immediately terminates the loop, bypassing the conditional
expression and any
remaining code in the body of the loop. When a break statement is encountered inside the loop,
the loop is
terminated and program control resumes the next statement following the loop.
3. What are objects? How are they created from a class?
Ans.
An object in java is a block of memory that contains a space to store all the instance variables. As
with real-world
objects, software objects have state and behavior. In programming terms the state of an object is
determined by its
data (variables); the behavior by its methods. Thus a software object combines data and methods into
one unit.
Creating an object is referred to as instantiating an object. The creating object to a
class is two-step process:
8) Compare in terms of their functions, the following pairs of statements:
a. while and do……….while

Ans.
While Do-While
1. The loop body is executed only 1.The loop body is executed at least
when the condition stated in the once, regardless of the condition
statement is true. evaluating to true & false.
2. It is also called top tested loop. 2. It is also called bottom tested loop.
3. Before the loop body executed, the 3. After the loop body is executed once, the
condition of the while statement condition in the do-while statement
evaluated. checked.

While For
i) In this you can’t Initialized more than one variable
i) You can
at Initialized more than one
a time. variable at a time.
ii) Like initialization section the increment section
ii) increment
also section also not have more
not have more than two parts. than two parts.
iii) It is not permissible to use expression iii) It is permissible to use expression in the
in the initialization and the Increment initialization and the Increment parts.
parts.

Continue Break
a) The continue statement stops the current a) We can force immediate termination of
iteration of a loop and immediately starts the loop, by passing any remaining code in the
next iteration of the same loop. body of the loop.
b) When the current iteration of a loop stops the b) Break statement is encountered inside the
statement after the continue statement in the loop, the loop is terminated and program
loop are not executed. control resumes the next statement.
c) You can use the continue statement in the
c) You can use the continue statement in the while, do-while and for statements and also
while, do-while and for statements but not in the use in the switch statement.
switch statement.

Vous aimerez peut-être aussi