Professional Documents
Culture Documents
1|Page
vi. Multithreaded:
1. It can handle multiple tasks simultaneously. Java makes this possible with the feature of
multithreading.
2. This means that we need not wait for the application to finish one task before beginning
other.
vii. Dynamic and Extensible:
1. Java is capable of dynamically linking new class library’s method and object.
2. Java program supports function written in other languages such as C, C++ which are
called as native methods.
3. Native methods are linked dynamically at run time.
2|Page
Program:
3|Page
1) State & explain scope of variable with an example.
Ans:
Scope of Variable :
We can declare variables within any block
One block equal to one new scope in Java thus each time you start a new block, you are
creating a new scope.
A scope determines what objects are visible to other parts of your program. It also determines
the lifetime of those objects.
Program:
4|Page
Any attempt to access it outside block will cause compilation error.
If block is written inside main block thus all the variables declared inside main block are
accessible in if block.
e.g.
x << 2 (x=12)
5|Page
Winter 2017
(c) Why java became platform independent language? Explain.
Ans.:
1) Java is a platform independent language. This is possible because when a java program is
compiled, an intermediate code called the byte code is obtained rather than the machine
code.
2) Byte code is a highly optimized set of instructions designed to be executed by the JVM
which is the interpreter for the byte code.
3) Byte code is not a machine specific code. Byte code is a universal code and can be moved
anywhere to any platform.
4) JVM is a virtual machine which exists inside the computer memory and is a simulated
computer within a computer which does all the functions of a computer.
5) Only the JVM needs to be implemented for each platform.
6) Although the details of the JVM will defer from platform to platform, all interpret the
same byte code.
2. It uses all primitive data types except Boolean expression to determine which alternative
should be executed.
General form:
switch(expression)
case value1:
block 1;
break;
case value2:
block 2;
break;
default:
default block;
break;
}
statement n;
Example:
public class SwitchExample
7|Page
int number=20;
switch(number)
break;
break;
break;
default:
break;
Summer 2017
(a) State and explain any four features of Java. (Note: Any four
may be considered)
Ans.:
i. Java is an object oriented language:-
It follows all the principles of object oriented programming namely inheritance, polymorphism
and abstraction. Multiple inheritance is possible with the concept of interface
8|Page
ii. Java is both compiled and interpreted:-
Most of the programming languages either uses a compiler or an interpreter. Java programs are
to be compiled to get an intermediate byte code (a .class file) and then interpreted making it
more secure and platform independent.
adds security by separating the package for the classes of the local file system
from those that are imported from network sources
Security Manager determines what resources a class can access such as reading and writing
to the local disk
iv. Robust:
Java uses strong memory management. The lack of pointers avoids security problem. There is
automatic garbage collection in java. There is exception handling and type checking mechanism
in java
v. Architecture-neutral:
There are no implementation dependent features e.g. size of primitive types is fixed
vii. Distributed:
Distributed applications can be created in java. RMI and EJB are used for creating distributed
applications. We may access files by calling the methods from any machine on the internet
viii. Multithreaded:
A thread is like a separate program, executing concurrently. We can write Java programs that
deal with many tasks at once by defining multiple threads. The main advantage of multi-
threading is that it doesn't occupy memory for each thread. It shares a common memory area.
Threads are important for multi-media, Web applications etc.
9|Page
(c) Explain any two relational operators in Java with example.
Ans.:
The relational operators in java are:
‘<’ :-
This operator is used to check the inequality of two expressions. It returns true if the first
expression is less than the second expression else returns false.
if(Exp1< exp2)
do this
else
do this
‘>’:-
This operator is also used to check the inequality of two expressions. It returns true if the first
expression is greater than the second one else returns false.
if(Exp1> exp2)
do this
else
do this
10 | P a g e
‘<=’:-
This operator returns true if the first expression is less than or equal to the second expression
else returns false.
if(Exp1< =exp2)
do this
else
do this
‘>=’ :-
This operator returns true if the first expression is greater than or equal to the second expression
else returns false.
if(Exp1>= exp2)
do this
else
do this
‘= =’:-
This operator returns true if the values of both the expressions are equal else returns false.
if(Exp1= = exp2)
{
11 | P a g e
do this
else
do this
‘!=’:-
This operator returns true if the values of both the expressions are not equal else returns false.
if(Exp1!= exp2)
do this
else
do this
Example:
class RelationalOps
int a = 10;
int b = 20;
System.out.println("a == b = " + (a == b) );
System.out.println("a != b = " + (a != b) );
(a) Write all primitive data types available in Java with their
storage sizes in bytes.
Ans.:
13 | P a g e
14 | P a g e