PROGRAMMING PARADIGM

2 MARK QUESTIONS WITH ANSWERS

UNIT-I

1. What is JAVA?

Java is a object oriented programming language, which has derived C syntax and C++ object oriented programming features.

It is a compiled and interpreted language and is platform independent and can do graphics, networking, multithreading. It was initially called as OAK.

2. What are the four cornerstones of OOP?

a. Abstraction

b. Encapsulation

c. Inheritance:

d. Polymorphism

3. What is Java Interpreter?

It is Java Virtual Machine. ie., a java program compiles the Unicode to intermediary code called as Byte code which is not an executable code, that is executed by Java interpreter.

4. What are the Java Buzzwords?

•Simple

•Secure

•Portable

•Object-oriented

•Robust

•Multithreaded

•Architecture-neutral

•Interpreted

•High performance

•Distributed

•Dynamic

5. What is meant by Object Oriented Programming?

OOP is a method of programming in which programs are organised as cooperative collections of objects. Each object is an instance of a class and each class belong to a hierarchy.

6. Write about the keywords used in main method().

The main method is of the form public static void main(String args[])

The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared.The keyword static allows main( )to be called without having to instantiate a particular instanceof the class. This is necessary since main( ) is called by the Java Virtual Machine before any objects are made. The keyword void simply tells the compiler tha tmain( )does not return a value. String args[] receives any command line arguments during runtime

7. List the primitive data types in java

Java defines eight primitive types of data: byte, short, int, long, char, float, double, and boolean. The primitive types are also commonly referred to as simple type. These can be put in four groups:

 Integers: This group includes byte, short, int, and long, which are for whole-valued

signed numbers.

 Floating-point numbers: This group includes float and double, which represent numbers with fractional precision

 Characters: This group includes char, which represents symbols in a character set, like

letters and numbers.

 Boolean: This group includesboolean, which is a special type for representing true/false values.

8. Define array.

An array is a group of like-typed variables that are referred to by a common name. Arrays of any type can be created and may have one or more dimensions. A specific element in an array is accessed by its index. Arrays offer a convenient means of grouping related information.

Syntax:

Array_var= newtype[size];

Example:

month_days = new int[12];

9. What is the use of break and continue statement?

In Java, the break statement has three uses. First, as you have seen, it terminates a statement sequence in a switch statement. Second, it can be used to exit a loop. Third, it can be used as a “civilized” form of goto. When a continue statement is executed in a loop, the remainder of the code in its body is skipped for the particular iteration and starts with the next iteration.. The continue statement performs such an action. In while and do-while loops, a continue statement causes control to be transferred directly to the conditional expression that controls the loop. In a for loop, control goes first to the iteration portion of the for statement and then to the conditional expression. For all three loops, any intermediate code is bypassed.

10. List the escape sequence characters available in java

11. Give the precedence of operators in java

12. Why java is said to be as robust?

Java frees you from having to worry about many of the most common causes ofprogramming errors. Because Java is a strictly typed language, it checks your code at compile time. However, it also checks your code at run time. Many hard-to-track-down bugs that often

turn up in hard-to-reproduce run-time situations are simply impossible to create in Java.

Knowing that what you have written will behave in a predictable way under diverse conditions is

a key feature of Java that makes it robust.

13. What is byte code?

Bytecode is a highly optimized set of instructions designed to be executed by the Java runtime

system, which is called the Java Virtual Machine (JVM). Translating a Java program into

bytecode makes it much easier to run a program in a wide variety of environments because only

the JVM needs to be implemented for each platform.

14. What is a for each loop? Give its syntax

A for-each style loop is designed to cycle through a collection of objects, such as an array, in

strictly sequential fashion, from start to finish. The for-each style of for is also referred to as the

enhanced for loop.

The general form of the for-each version of the fo is shown here:

for(type itr-var : collection)

{//statement-block}

Here, type specifies the type and itr-var specifies the name of an iteration variable that will

receive the elements from a collection, one at a time, from beginning to end. The collection

being cycled through is specified by collection.

15. What is a class?

A class can be defined as a template that describes the behaviors and states that object of its

type support. A class is a collection of objects of similar type. A class can have any number of

objects

16. Give the general form of a class

Class classname {

type instance-variable1;

type instance-variable2;

// ...

type instance-variableN;

type methodname1(parameter-list) {

// body of method

}

type methodname2(parameter-list) {

// body of method

}

// ...

type methodnameN(parameter-list) {

// body of method

}

}

17. What is the use of this keyword?

Java defines this keyword to refer to the object that invoked the method .this can be used

inside any method to refer to the current object. That is, this is always a reference to the object

on which the method was invoked. You can use this anywhere a reference to an object of the

current class’ type is permitted.

Box(double width, double height, double depth) {

this.width = width;

this.height = height;

this.depth = depth;

}

18. Define encapsulation

Encapsulation is the mechanism that binds together code and the data it manipulates, and

keeps both safe from outside interference and misuse. Encapsulation is as a protective wrapper

that prevents the code and data from being arbitrarily accessed by other code defined outside the

wrapper. Access to the code and data inside the wrapper is tightly controlled through a welldefined

interface.

19. What is meant by abstraction?

Abstraction defines the essential characteristics of an object that distinguish it from all other

kinds of objects. Abstraction provides crisply-defined conceptual boundaries relative to the

perspective of the viewer. It is the process of focussing on the essential characteristics of an

object. Abstraction is one of the fundamental elements of the object model.

20. What is an applet?

An applet is a special kind of Java program that is designed to be transmitted over the

Internet and automatically executed by a Java-compatible web browser. Furthermore, an applet is

downloaded on demand, without further interaction with the user. If the user clicks a link that

contains an applet, the applet will be automatically downloaded and run in the browser. Applets

are intended to be small programs. They are typically used to display data provided by the server,

handle user input, or provide simple functions, such as a loan calculator, that execute locally,

rather than on the server. In essence, the applet allows some functionality to be moved from the

server to the client.

21. What is meant by garbage collection?

Java handles deallocation for you automatically. The technique that accomplishes this is

called garbage collection. When no references to an object exist, that object is assumed to be no

longer needed, and the memory occupied by the object can be reclaimed. There is no explicit

need to destroy objects. Garbage collection only occurs sporadically (if at all) during the

execution of your program. It will not occur simply because one or more objects exist that are no

longer used. Furthermore, different Java run-time implementations will take varying approaches

to garbage collection

22. What does a finalize method() do?

Sometimes an object will need to perform some action when it is destroyed. To handle such

situations, Java provides a mechanism called finalization. By using finalization, you can define

specific actions that will occur when an object is just about to be reclaimed by the garbage

collector. The Java run time calls that finalize() method whenever it is about to recycle an object

of that class. Inside the finalize( ) method, you will specify those actions that must be performed

before an object is destroyed. The garbage collector runs periodically, checking for objects that

are no longer referenced by any running state or indirectly through other referenced objects.

Right before an asset is freed, the Java run time calls the finalize( )method on the object.

Thefinalize( )method has this general form:

protected void finalize( )

{

// finalization code here

}

23. Define method overloading.

When two or more methods within the same class that share the same name, as long as their

parameter declarations are different, the methods are said to be overloaded, and the process is

referred to as method overloading. Method overloading is one of the ways that Java supports

polymorphism.

class OverloadDemo {

void test() {

System.out.println("No parameters");

}

// Overload test for one integer parameter.

void test(int a) {

System.out.println("a: " + a);

}

}

24. What is a constructor?

Java allows objects to initialize themselves when they are created. This automatic

initialization is performed through the use of a constructor. A constructor initializes an object

immediately upon creation. It has the same name as the class in which it resides and is

syntactically similar to a method. Once defined, the constructor is automatically called

immediately after the object is created, before the new operator completes.

25. List the types of constructors

Default constructors- The constructor that does not pass any arguments

class Box {

double width;

double height;

double depth;

// This is the constructor for Box.

Box() {

System.out.println("Constructing Box");

width = 10;

height = 10;

depth = 10;

}

}

Parameterized constructor- The constructor that accepts one or more arguments

class Box {

double width;

double height;

double depth;

// This is the constructor for Box.

Box(double w, double h, double d) {

width = w;

height = h;

depth = d;

}

}

26. What is constructor overloading?

If a class has more than one constructors with different set of parameters(constructor name

will also be same) then it is said to be overloaded.

class Box {

double width;

double height;

double depth;

Box(double w, double h, double d) {

width = w; height = h; depth = d;

}

Box() {

width = -1; height = -1; depth = -1;

}

Box(double len) {

width = height = depth = len;

}

}

27. What is the use of static member?

Normally, a class member must be accessed only in conjunction with an object of its class.

However, it is possible to create a member that can be used by itself, without reference to a

specific instance. To create such a member, precede its declaration with the keyword static.

When a member is declared static, it can be accessed before any objects of its class are created,

and without reference to any object.

28. What are the restrictions of static methods?

•They can only call other static methods.

•They must only access static data.

•They cannot refer to this or super in any way.

29. What is a final variable?

A variable can be declared as final. Doing so prevents its contents from being modified.

This means that you must initialize a final variable when it is declared. Variables declared as

final do not occupy memory on a per-instance basis. Thus, a final variable is essentially a

constant. For example:

final int FILE_NEW = 1;

30. Write about various access specifiers in java.

Java’s access specifiers are public, private, and protected

When a member of a class is modified by the public specifier, then that member can be accessed

by any other code.

When a member of a class is specified as private, then that member can only be accessed by

other members of its class.

If you want to allow an element to be seen outside your current package, but only to classes that

subclass your class directly, then declare that element protected.

31. Explain the usage of the keyword transient?

This keyword indicates that the value of this member variable does not have to be serialized

with the object. When the class will be de-serialized, this variable will be initialized with a

default value of its data type (i.e. zero for integers).

32. What is an Object and how do you allocate memory to it?

Object is an instance of a class and it is a software unit that combines a structured set of data

with a set of operations for inspecting and manipulating that data. When an object is created

using new operator, memory is allocated to it.

33. What is static variable and static method?

Static variable is a class variable which value remains constant for the entire class static

method is the one which can be called with the class itself and can hold only the static variables.

34. Mention some of the separators available in java

( ) -Parentheses

{ } Braces

[ ] Brackets

; Semicolon.

, Comma

. Period

35. List the types of comments available in java

The contents of a comment are ignored by the compiler. Instead, a comment describes or

explains the operation of the program to anyone who is reading its source code.

There are three types of comments available in java

1. Single line comment

A single-line comment begins with a // and ends at the end of the line

2. Multiline comment

It must begin with/*and end with*/. Anything between these two comment symbols is

ignored by the compiler. As the name suggests, a multiline comment may be several lines long.

3. Documentation Comment

This type of comment is used to produce an HTML file that documents your program. The

documentation comment begins with a/**and ends with a*/.