You are on page 1of 5

BCA Semester 4 BC0047 Java programming 4 Credits (Book ID: B0831) Total Marks: 40 Marks

Q.1: What do you mean by Java virtual machine? Explain. Ans: Byte code is a very highly optimized set of instructions designed to be executed by the Java run time system, which is called the Java virtual machine. That is, in its standard form the java virtual machine is an interpreter for byte code. This may come as a bit of a surprise. The fact that a java program is executed by the java virtual machine helps solves the major problems associated with downloading programs over the internet. Only the java virtual machine needs to be implemented for each plate form. The fact that a java program is interpreted also helps to make it secure. Because the execution of every java program is under the control of the java virtual machine, the java virtual machine can contained the program and prevent it from generating side effects outside of the system.

Q.2: Write a program in java to find factorial of a number. How do you compile and execute this java program. Ans:

Compile the java program: In java the program is compiled into byte code that run on the java virtual machine, which can interpret and run the program on any operating system. This makes java programs platform independent. At the command prompt, type Java c<filename>.java Execute the java program: When the code is compiled and error-free, the program can be executed using the command: Java <file name>

Q.3: Write a simple java program to illustrate the use of if else statement. Ans: Public class Result { Public static void main(string arg []) { Int marks=45; If (mark<35) System. Out. Println (the student has failed); Else System. Out. Println (the student has passed); } }

Q.4: What do mean by an array? Explain with an example. Ans: An array is a collection of elements of the same type that are referenced by a common name. Each element of an array can be referred to by an array name and a subscript or index. An array represents a number of variables which occupy contiguous space in the memory. ExampleInt [ ] numbers; The above statement will declare a variable that can hold an array of the int type variables. After declaring variable for the array the array needs to be allocated in the memory. Iteration can be used to access all the elements of the array one by one.

Q.5: Write a program to explain the Exception Handling mechanism in java using the keywords: try, catch and finally. Ans: Try Class NestTry { Public static void main(string args[]) {

try { int a=args.length; int b=42/a; system.out.println(a=+a); try { //nested try block if(a==1) a=a/(a-a);//division by zero if (a==2) int c[]={1}; c[42]=99;// generate an out-of- bounds exception } } catch (ArraylndexOutOfBoundsException e ) { } } catch(ArithmeticException e) { Sysrem.out.println(Divided by 0: +e); } } }

CatchA Try { //statements that may cause an exception } Catch () { //error handling code }

Finally Try { openFile(); writeFile(); //may cause an exception } Catch() { //process the exception } finally { closeFile (); }

Q.6: Define (a) inheritance, (b) package, (c) interface. Ans: (a) inheritance: Inheritance allows the creation of hierarchical classifications because it is one of the cornerstones of object-oriented programming. Using inheritance we can create a general class that define traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it. In java a class that is inherited is called a super class. The class that does the inheriting is called a subclass. Therefore a subclass is a specialized version of a super class. It inherits all of the instance variables and methods defined by a super class and add its own, unique elements. (b) Package: In java a mechanism for partitioning the class name space into more manageable chunks. This mechanism is called package. The package is both a naming and a visibility control mechanism. We can define classes inside a package that are not accessible by code outside that package. We can also define class members that are only expose to other members of the same package. This allows our classes to have intimate knowledge of each other, but not expose that knowledge to the rest of the world.

(c) Interface: The interface is the keyword of java that can fully abstract a class interface from its implementation. Using the keyword interface we can specify what a class must do, but not how it does it. Interfaces are syntactically similar to classes, but they lack instance variables and their methods are declared without any body. Once it is defined any number of classes can implement an interface. To implement an interface a class must create the complete set of methods defined by the interface. Each class is free to determine the details of its own implementations. By proving the interface keyword java allows to fully utilize the one interface, multiple methods aspect of polymorphism.

You might also like