0% found this document useful (0 votes)
14 views6 pages

Paper Java

The document outlines the examination details for a Java programming course, including instructions for candidates and a series of questions covering various Java concepts such as exception handling, class design, multithreading, and input/output operations. It specifies the structure of the examination, including the number of questions to attempt and the maximum marks. Additionally, it provides programming tasks that require writing Java code to solve specific problems.

Uploaded by

ayushikundliya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Paper Java

The document outlines the examination details for a Java programming course, including instructions for candidates and a series of questions covering various Java concepts such as exception handling, class design, multithreading, and input/output operations. It specifies the structure of the examination, including the number of questions to attempt and the maximum marks. Additionally, it provides programming tasks that require writing Java code to solve specific problems.

Uploaded by

ayushikundliya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unique Paper Code : 32341201_OC

Name/Title of the paper : Programming in Java-OC

Name of the Course : B. Sc. (H) Computer Science

Semester : II

Year of Admission : 2015, 2016, 2017, 2018

Duration of Examination : 3 Hours

Maximum Marks : 75

Instructions for Candidates

1. Attempt any FOUR out of SIX questions.

2. All questions carry equal marks.

3. All parts of a question must be answered together.


4. The data types of variables/data members/arrays and return types of the methods should be
clearly stated.
Q1. Assuming all necessary packages imported (where required) in the following Java code snippets,
write the output and explain your answer.
i) public class ArrQ {
int v1;
int v2;
public void meth1() {
v1 = 0;
v2 = 0;
int[] v3 = { 0 };
meth2(v2, v3);
[Link](v1+" "+v2+" "+v3[0]+" ");
}
public void meth2(int v2, int[] v3) {
v1 = 1;
v2 = 1;
v3[0] = 1;
}
public static void main(String[] args) {
ArrQ aq = new ArrQ();
aq.meth1();
}
}
ii) public class ExcepHand {
public static void main(String[] args) {
try {
meth1();
} catch (InterruptedException ie) {
[Link]("A");
throw new RuntimeException();
} catch (RuntimeException ie) {
[Link]("B");
return;
} catch (Exception ie) {
[Link]("C");
} finally {
[Link]("D");
}
[Link]("E");
}
static void meth1() throws InterruptedException {
throw new InterruptedException("Demo");
}
}
iii) public class A {
public static void main(String[] args) {
double d=0x27.2p2;
int i=(int)d;
d=d%0b1010;
[Link]("Value of d is " + d);
[Link]("Value of i is " + i);
}
}
iv) public class StaticClass {
static int v1 = 3;
static int v2;
static void methStatic (int x) {
[Link]("x : " + x);
[Link]("v1 : " + v1);
[Link]("v2 : " + v2);
}
static {
[Link]("Static block");
v2 = v1 + 5;
}
public static void main(String args[]) {
methStatic(11);
}
}
public class StaticTest {
static int v1 = 31;
static int v2 = 28;
static void methStatic () {
[Link]("v1 : " + v1);
}
public static void main(String args[]) {
[Link]();
[Link](42);
[Link]("v2 : " + v2);
[Link]("StCls.v2:" + StaticClass.v2);
}
}
v) public class B {
public static void main(String args[]) {
int iarr[][] = {{5, 7}, {6, 8}};
int a, b;
for (a = 1; a > -1; a--) {
for (b = 1; b > -1; b--) {
[Link](iarr[b][a]);
}
}
}
}

Q2. Consider the following table:

Number of units consumed Rate


1-100 Rs 500/- rental charges only
101-200 Rs 1.00 per unit + rental charges of Rs 500
201-300 Rs 1.20 per unit + rental charges of Rs 500
Above 300 Rs 1.50 per unit + rental charges of Rs 500

Write a program in Java that does the following:


 Defines a class ElectricityBill having five private instance variables storing
consumer details: consumerNo, consumerName, consumerAge,
consumerAddress, numUnitsConsumed; a private class variable count (that
keeps the count of total number of instances), all having appropriate data types.
 Defines a parameterized constructor; and three methods- display()for displaying
consumer details; calculate() for computing the monthly bill according to the units
consumed as per the following table and display the total amount to be paid and
displayCount()to display the total number of consumers (instances of the class).
 The program should be able to raise an exception NegativeAgeException if the
age entered by the user is negative. The exception should be handled by displaying a
message "Error: Age cannot be negative!" on the screen and the program
should end.
 Defines a driver class Demo having main() method with appropriate code to show
working of the above classes.

Q3. a. Assuming all necessary packages imported in the following Java code, identify the errors
and rewrite the rectified code. Explain your answer.
public class VarInit {
int v1, v2 = 0;
static int v3;
public static void main(String[] args) {
int v4;
int v5 = 0;
v1++; v2++;
v3++; v4++;
}
}
b. Java implements a controlled version of global methods and global variables with the help
of static methods and variables. Explain the statement.

c. What is multiple inheritance? Why does Java not support it? Explain the workaround with
an example.

Q4. a. Write a multithreaded program in Java to take n integers from the user. Two threads are then
created. The first thread prints even integers on the console and the second thread writes odd
integers to a text file.
b. Using Java AWT, write a program that does the following:
 Creates a frame titled "My_New_Frame" having two fields to add UserId and
Password. If the user types in correct user id and correct password, display a
message "Successful" inside the frame otherwise display "Invalid
Credentials".
 Using appropriate adapter class to display the message "Typed character is:
<typedCharacter>" in the frame window when user types any key.

Q5. Write a program in Java that accepts words through command line and works as follows:
 If the number of arguments is equal to five, it should sort them in alphabetical order and
print it in a file named [Link].
 If the number of arguments is equal to zero, then the program should ask the user to enter
a sentence of five words, sort the words in alphabetical order and print it on console.
 If the number of arguments is neither five nor zero, then the exception raised should be
handled by the program and a message "Error: Number of arguments is
not valid!" is displayed on the screen and the program ends.

Q6. a) Consider the following incomplete Java code snippet.


int index = 10;
for(int x = 2; x <= index; x++) {
...
}
Write Java statements inside the above for loop to generate the formatted output as
mentioned below.
2 4 6 8 10
4 8 12 16 20
6 12 18 24 30
8 16 24 32 40
10 20 30 40 50

b) Write Java statements/prototype for the following tasks:


i) a method that accepts a character, an array of integers and has void as a return type.
ii) a while loop that reads some words from the keyboard till ^z is entered and displays
each word per row.
iii) A single statement that compares values of two numbers stored in variables u and v and
stores incremented value of the greater number in the variable w by making use of unary
increment operator and ternary operator.

You might also like