Java - QB
Java - QB
Java - QB
Question - 1
The below array program finds the average of a set of numbers.
1. class Average {
2. public static void main(String args[]) {
3. double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
4. double result = 0;
5. int i;
6. for(___________________)
7. result = _____________
8. System.out.println("Average is " + _________________);
9. ______________
10. }
(i) Complete the Missing line of the above array code
(ii) Identify the default initial value of numeric elements in an array of type int in java
In Java, the default initial value of numeric elements in an array of type int is 0.
(iii) Predict the output of the above code
Average is 12.1
This is because the code calculates the average of the array elements {10.1, 11.2, 12.3,
13.4, 14.5}, which is (10.1 + 11.2 + 12.3 + 13.4 + 14.5) / 5 = 12.1.
Question - 2
_____myNum = 9;
________myFloatNum = 8.99f;
______myLetter = ‘A’;
_____myBool = false;
___________myText = “Hello World”;
ANSWER:
int myNum = 9;
Question - 3
Here is a program that uses an if-else-if ladder to determine which season a particular month is
in.
// Demonstrate if-else-if statements.
class IfElse {
public static void main(String args[]) {
int month = 4; // April
String season;
if(month == 12 || month == 1 || month == 2)
season = "Winter";
else if(month == 3 || month == 4 || month == 5)
season = "Spring";
else if(month == 6 || month == 7 || month == 8)
season = "Summer";
else if(month == 9 || month == 10 || month == 11)
season = "Autumn";
else
season = "Bogus Month";
System.out.println("April is in the " + season + ".");
}
}
(i) Modify the above program using switch case to display the same output mentioned in the scenario.
ANSWER: If there is no else block after an if statement, the program will simply skip
the if block and continue executing the rest of the code. In this specific program, if the
month variable does not match any of the conditions, the season variable will not be
assigned a value, and the program will print a null value when trying to print the
season.
In the switch statement, the compiler creates a jump table that maps each possible
value of the month variable to the corresponding case label. When the program
executes the switch statement, it simply looks up the value of month in the jump table
and jumps to the corresponding case. This is a constant-time operation, regardless of
the number of cases.
Question - 4
Suppose if you want to find the largest among three numbers i.e a=78,b=25,c=87
identify the expression using the ternary operator in a single statement (4),
Here is the expression using the ternary operator to find the largest among three
numbers in a single statement:
This expression uses nested ternary operators to compare the values of a, b, and c and
assigns the largest value to the largest variable.
This expression is a concise way to find the largest among three numbers using the
ternary operator.
UNIT II
Question - 1
Let's consider a scenario as shown in figure-1, Father as parent class and Son as child class.
Son acquires the properties like color, a character from Father. Hence, it is called inheritance.
Question - 2
fill the missing part of the single inheritance code
import java.lang.*;
class Father { // Base class
void work() {
System.out.println(“working…”);
}
}
class Son extends Father { // Derived class
void play() {
System.out.println(“playing…”);
}
}
class Inheritance {
public static void main(String args[]) {
Son s = new Son();
s.play(); // inherited from derived class
s.work(); // inherited from base class
}
}
Question - 3
Consider the code of Multilevel inheritance
import java.lang.*;
class Grandfather
void sleep() {
System.out.println("sleeping...");
}
}
class Father extends Grandfather
void work[ ] {
System.out.println(working);
}
}
class Son extends Son
void play() {
System.out.println("playing");
}
}
class Inheritance{
public static void main(String args[]) {
Son s = enum Son();
s.sleep(); // inherited from base class
s.work(); // inherited from derived class 1
s.work // inherited from derived class 2
}
}
(ii) Find the base class and derived classes from the above code
Base class: Grandfather Derived classes: Father (derived from Grandfather), Son (derived from
Father)
(iii) Based on the scenario, Sketch the representation of multilevel inheritance
Here is the corrected code and a diagram representing the multilevel inheritance:
In this diagram, Grandfather is the base class, Father is the derived class 1, and Son is the
derived class 2. The inheritance relationship is shown by the arrows, where each derived class
inherits from its parent class.
UNIT-III
Question - 1
Java brings various Streams with its I/O package as shown in Figure-1 that helps the user to
perform all the input-output operations. These streams support all the types of objects,
data-types, characters, files etc to fully execute the I/O operations.
(ii) In java, assess how you can check if a file or directory exists using the ‘file’ class?
The File class in Java provides a method called exists() to check if a file or directory
exists. You can use it like this:
It's important to note that OutputStream is not the superclass of all classes representing an
output stream of bytes. Instead, it is the superclass of all byte-oriented output streams. There
are other types of output streams, like Writer, which handles character-oriented data.
Question - 2
Say true or false: Scanner class is used to read text from the console in Java (2m)
True
The Scanner class in Java is used to read text from various sources, including the console, files,
and strings. It provides a way to break the input into tokens, such as integers, strings, and other
types, using regular expressions.
You can use the Scanner class to read text from the console by creating a Scanner object and
passing System.in as an argument,
Question - 3
numChars) constructor creates a Reader from a subset of the character array that begins with
the character at the index specified by start and is numChars long. This means that the
CharArrayReader will read numChars characters from the array starting from the start
index. In the code, input2 is created with start = 0 and numChars = 5, so it will read the
first 5 characters of the array, which are "abcde".
1) int length = tmp.length[]; should be int length = tmp.length; (remove the square brackets)
2) char c[] = new char[width]; should be char c[] = new char[length]; (replace width with length)
3) exp.getChars(0, length, c, 0); should be tmp.getChars(0, length, c, 0); (replace exp with tmp
RECTIFIED CODE:
Question - 4
In Java, any object can be serialized as long as it implements the Serializable interface or its
superclass implements it. If a class does not implement Serializable, it will throw a
NotSerializableException when you try to serialize it.
However, if you want to customize the serialization process, you can implement the Serializable
interface and provide your own implementation of the writeObject() and readObject() methods.
So, while implementing the Serializable interface is not strictly necessary, it is required for an
object to be serializable, and it provides a way to customize the serialization process.
UNIT-IV
Question - 1
public class Main{byte b=5; short s=10; int i=20; long l=30; float f=50.0F; double d=60.0D; char
c='f'; boolean b2=true; }
(i) Convert the above primitives into objects using auto-boxing wrapper class.
Here is the conversion of the primitives into objects using auto-boxing wrapper classes:
Question - 2
class Main {
public static void main(String[] args) {
// create second
String second = "Programming";
System.out.println("Second String: " - second);
1) The string initialization for first is incorrect. It should be String first = "Java"; instead of
String first = Java ;.
2) The System.out.println statements are using the subtraction operator - instead of the
concatenation operator + to concatenate the string literals with the variables. It should be
System.out.println("First String: " + first); and System.out.println("Second String: " +
second);.
3) The concat method is not a valid method for concatenating strings in Java. Instead, you
can use the + operator or the concat method of the String class. It should be String
joinedString = first + second; or String joinedString = first.concat(second);.
CORRECTED PROGRAM:
Question - 3
}
}
The output produced by the program is
Original: This is a test.
Uppercase: THIS IS A TEST.
Lowercase: this is a test.
(i) Fill the block of the above code to get the output given in the above scenario
(ii) Identify the methods that can be used to find the length and capacity of the string
In Java, you can use the following methods to find the length and capacity of a string:
● Length: length() method is used to find the length of a string. It returns the number of
characters in the string.
● Capacity: There is no direct method to find the capacity of a string in Java. The String
class in Java does not have a fixed capacity; it can grow dynamically as you add
characters to it. However, if you're referring to the capacity of a StringBuilder or
StringBuffer, you can use the capacity() method.
Question - 4
(i) Convert the above primitives into objects using auto-boxing wrapper class.
Fig-1-Applet
(i) Identify the missing parts of applet lifecycle from figure-1
(ii) Identify the valid declaration of an applet from the following
1. public class MyApplet extends java.applet.Applet {
2. public Applet MyApplet {
3. public class MyApplet extends applet implements Runnable {
4. abstract class MyApplet extends java.applet.Applet {
ANSWER:
1. public class MyApplet extends java.applet.Applet {
This declaration correctly defines a class named MyApplet that extends java.applet.Applet,
indicating that MyApplet is an applet class.
(iii) Identify the method that will be invoked when an applet window is closed
ANSWER:
The method that will be invoked when an applet window is closed is destroy(). In Java applets,
the destroy() method is called when the applet's window is closed or when the browser
tab/window containing the applet is closed. This method can be overridden in the applet's class
to perform any necessary cleanup or resource releasing operations before the applet is
terminated.
(iv) If you want to assign a value of 88 to the variable year, then which of the following lines can be used
within the <applet> tag.
1. number =getParameter(88)
2. <number=99>
3. <Param=radius value=88>
4. <Param name = number value =88>
ANSWER:
4.<Param name = number value =88>
This line uses the <Param> tag with the name attribute set to "year" and the value attribute set
to "88", which means it will pass the value 88 to the variable year when the applet is initialized.
Question - 2
When an applet is terminated, which of the following sequence of methods calls take place?
a. stop(),paint(),destroy()
b. destroy(),stop(),paint()
c. destroy(),stop()
d. stop(),destroy()
ANSWER: c. destroy(),stop()
Question - 3
Flow : 4->3->5->8->6->1->2->7
ANSWER:
False.
Justification: JDBC Type 2 drivers do not directly connect to the native API of the DBMS
(Database Management System). Instead, they use a native API client library provided by the
DBMS vendor to connect to the database. These drivers convert JDBC calls into calls specific
to the DBMS's native protocol, allowing Java applications to interact with the database using
JDBC. Therefore, while Type 2 drivers do interact with the native API through the native client
library, they do not directly connect to it.
Write the JDBC code to insert the values in the database.
ANSWER:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
Question - 4
What is the Message is displayed in the applet made by the following Java program?
import java.awt.*;
import java.applet.*;
}}
a Simple Applet
b. A Simple Applet 20 20
c. Compilation Error
d. Runtime Error
ANSWER:
c) Compilation Error