You are on page 1of 14

SUMMER– 18

(c) Explain any four features of java programming. 4M


Ans:
i. Compile & Interpreted:
1. Java is a two staged system. It combines both approaches. First java compiler translates
source code into byte code instruction. Byte codes are not machine instructions.
2. In the second stage java interpreter generates machine code that can be directly executed
by machine. Thus java is both compile and interpreted language.
ii. Platform independent and portable:
1. Java programs are portable i.e. it can be easily moved from one computer system to
another.
2. Changes in OS, Processor, system resources won’t force any change in java programs.
3. Java compiler generates byte code instructions that can be implemented on any machine
as well as the size of primitive data type is machine independent.
iii. Object Oriented:
1. Almost everything in java is in the form of object. All program codes and data reside
within objects and classes.
2. Similar to other OOP languages java also has basic OOP properties such as
encapsulation, polymorphism, data abstraction, inheritance etc.
3. Java comes with an extensive set of classes (default) in packages.
iv. Robust & Secure:
1. Java is a robust in the sense that it provides many safeguards to ensure reliable codes.
2. Java incorporates concept of exception handling which captures errors and eliminates
any risk of crashing the system.
3. Java system not only verifies all memory access but also ensure that no viruses are
communicated with an applet.
4. It does not use pointers by which you can gain access to memory locations without
proper authorization.
v. Distributed:
1. It is designed as a distributed language for creating applications on network. It has ability
to share both data and program.
2. Java application can open and access remote object on internet as easily as they can do in
local system.

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.

(e) What is type casting? Explain its types with proper


syntax and example.
Ans:
Assigning a value of one type to a variable of another type is known as Type Casting

 There are 2 types of type casting:

1. Widening or Implicit type casting

2. Narrowing or Explicit type casting

Implicitly Type casting take place when

 The two types are compatible

 The target type is larger than the source type

2|Page
 Program:

2.Narrowing or Explicit type casting:


 When you are assigning a larger type value to a variable of smaller type. Then

you need to perform explicit type casting.

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:

 n1 is declared in main block thus it is accessible in main block.

 n2 is declared in if block thus it is only accessible inside if block

4|Page
 Any attempt to access it outside block will cause compilation error.

 Nested Block can have access to its outmost block.

 If block is written inside main block thus all the variables declared inside main block are
accessible in if block.

(a) Explain following bitwise operator with an example:


(1) left shift operator
(2) write shift operator
Ans:
 The Left Shift (<<):
the left shift operator, <<, shifts all of the bits in a “value” to the left a specified
number of times specified by “num”

General form : value <<num

e.g.
x << 2 (x=12)

0000 1100 << 2

=> 0011 0000 (decimal 48)

 The Right Shift (>>):


the right shift operator, >>, shifts all of the bits in a “value” to the right a specified
number of times specified by “num”

General form: value >>num

e.g. x>> 2 (x=32)

0010 0000 >> 2

=> 0000 1000 (decimal 8)

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.

(b) Illustrate with example the use of switch case statement.


(Note: Any other example shall be considered)
Ans.:
6|Page
 Switch case statement:
1. The switch statement is used to select among multiple alternatives

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

public static void main(String[] args)

7|Page
int number=20;

switch(number)

case 10: System.out.println("You are in 10");

break;

case 20: System.out.println("You are in 20");

break;

case 30: System.out.println("You are in 30");

break;

default:

System.out.println("Not in 10, 20 or 30");

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.

iii. Java is secure:

ava programs run inside a virtual machine

adds security by separating the package for the classes of the local file system
from those that are imported from network sources

that can violate access right to


objects.

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

vi. Platform independent and Portable:


Java byte code can be carried to any platform

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

public static void main(String args[])

int a = 10;

int b = 20;

System.out.println("a == b = " + (a == b) );

System.out.println("a != b = " + (a != b) );

System.out.println("a > b = " + (a > b) );


12 | P a g e
System.out.println("a < b = " + (a < b) );

System.out.println("b >= a = " + (b >= a) );

System.out.println("b <= a = " + (b <= a) );

(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

You might also like