Super 25 Java Q&A V2V
Super 25 Java Q&A V2V
1
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
Constructor in JAVA is a special type of method that is used to initialize the object.
JAVA constructor is invoked at the time of object creation.
It constructs the values i.e. provides data for the object that is why it is known as constructor.
A constructor has same name as the class in which it resides.
Constructor in JAVA cannot be abstract, static, final or synchronized.
These modifiers are not allowed for constructor.
There are two types of constructors
1. Default constructor (no-arg constructor)
2. Parameterized constructor
1. Default Constructor
A constructor that have no parameter is known as default constructor.
Syntax of default constructor
<class_name> ( )
{
}
Example of default constructor.
class Bike1
{
Bike1( )
{
System.out.println("Bike is created");
}
public static void main(String args[ ])
{
Bike1 b=new Bike1();
}
}
This will produce the following result Bike is created
Note – If there is no constructor in a class, compiler automatically creates a default constructor.
2. Parameterized Constructor
A constructor that have parameters is known as parameterized constructor.
Parameterized constructor is used to provide different values to the distinct objects.
Example of parameterized constructor.
class Student
{
int id;
String name;
Student(int i, String n)
{
2
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
id = i;
name = n;
}
void display( )
{
System.out.println(id+" "+name);
}
public static void main(String args[ ])
{
Student s1 = new Student4(111,"Ram");
Student s2 = new Student4(222,"Shyam");
s1.display();
s2.display();
}
}
This will produce the following result
111 Ram
222 Shyam
Java provides the feature of anonymous arrays which is not available in C/C++.
Example
class Testarray
{
public static void main(String args[])
{
int a[ ]=new int[5]; //declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
OUTPUT
10
20
70
40
50
2. Two dimensional Array :
4
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
In such case, data is stored in row and column based index (also known as matrix form).
Example to instantiate Multidimensional Array in Java
int[ ][ ] arr =new int[3][3]; //3 row and 3 column
Example to initialize Multidimensional Array in Java
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9;
Example of Multidimensional Java Array
class Testarray2
{ public static void main(String args[])
{
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; //printing 2D array
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(arr[i][j] +" ");
}
System.out.println();
}
}
}
Output
123
245
445
5
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
6
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
Output
This is student
This is teacher
7
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
{
salary_calc(int id, String nm, String dpt, String j1,int bs)
{
super(id,nm,dpt,j1,bs);
}
public static void main(String args[])
{
salary_calc e1=new salary_calc(101,"abc","Sales","clerk",5000);
double gross_salary=e1.calc();
System.out.println("Empid :"+e1.empid);
System.out.println("Emp name :"+e1.ename);
System.out.println("Department :"+e1.dept);
System.out.println("Job :"+e1.job);
System.out.println("BAsic Salary :"+e1.basic_salary);
System.out.println("Gross salary :"+gross_salary);
}
}
6. Explain garbage collection and State use of finalize( ) method with its syntax.
Ans:
Garbage collection
In JAVA destruction of object from memory is done automatically by the JVM.
When there is no reference to an object, then that object is assumed to be no longer needed
and the memory occupied by the object are released.
This technique is called Garbage Collection.
This is accomplished by the JVM.
Unlike C++ there is no explicit need to destroy object.
finalize( ) method
Sometime an object will need to perform some specific task before it is destroyed such as
closing an open connection or releasing any resources held.
To handle such situation finalize() method is used.
finalize()method is called by garbage collection thread before collecting object.
Its the last chance for any object to perform cleanup utility.
Signature of finalize() method
protected void finalize()
{
//finalize-code
}
Some Important Points to Remember
8
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
6. Explain package & Give syntax to create a package and accessing package in java
Ans:
Packages:
Java provides a mechanism for partitioning the class namespace into more manageable parts called
package (i.e package are container for a classes). The package is both naming and visibility controlled
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 exposed to members of the same package.
Creating Packages:- (Defining Packages)
Creation of packages includes following steps:
1) Declare a package at the beginning of the file using the following form.
package package_name
e.g.
package pkg; - name of package
package is the java keyword with the name of package. This must be the first statement in java source
file.
2) Define a class which is to be put in the package and declare it public like following way.
Package first_package;
Public class first_class
{
Body of class;
}
In above example, “first_package” is the package name. The class “first_class” is now considered as a
part of this package.
3) Create a sub-directory under the directory where the main source files are stored.
4) Store the listing of, as classname.java file is the sub-directory created. e.g. from the above package we
can write “first_class.java”
5) Compile the source file. This creates .class file is the sub-directory. The .class file must be located in
the package and this directory should be a sub-directory where classes that will import the package are
located.
Accessing a package:-
To access package In a Java source file, import statements occur immediately. Following the package
statement (if it exists) and before any class definitions.
Syntax:
import pkg1[.pkg2].(classname|*);
9
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
Here, “pkg1” is the name of the top level package. “pkg2” is the name of package is inside the package1
and so on.
“classname”-is explicitly specified statement ends with semicolon.
Second way to access the package
import packagename.*;
10
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
Output:
thread is running...
9. Define error Enlist any four compile time and Runtime errors.
Ans:
The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that
the normal flow of the application can be maintained.
Error may produce
An incorrect output
may terminate the execution of program abruptly
may cause the system to crash
It is important to detect and manage properly all the possible error conditions in program.
Types of Errors
1. Compile time Errors: Detected by javac at the compile time
2. Run time Errors: Detected by java at run time
12
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
Errors which are detected by javac at the compilation time of program are known as compile
time errors.
Most of compile time errors are due to typing mistakes, which are detected and displayed by
javac.
Whenever compiler displays an error, it will not create the .class file.
Typographical errors are hard to find.
The most common problems:
Missing semicolon
Missing or mismatch of brackets in classes and methods
Misspelling of identifiers and keywords
Missing double quotes in strings
Use of undeclared variables
Incompatible types in assignments/ Initialization
Bad references to objects
Use of = in place of = = operator etc.
Other errors are related to directory path like command not found
10. Write a program to check whether the given number is prime or not.
Ans:
public class Main {
public static void main(String[] args) {
int num = 29;
boolean flag = false;
for (int i = 2; i <= num / 2; ++i) {
13
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
14
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
}
Output from above program will be :
a && b = false
a || b = true
!(a && b) = true
Relational Operators :
The relational operators determine the relationship that one operand has to the other.
Following program demonstrate the use of Relational Operator ==, !=, >,= and <=
class RelOptrDemo
{
public static void main(String[] args)
{
int a = 10, b = 15, c = 15;
System.out.println("Relational Operators and returned values");
System.out.println(" a > b = " + (a > b));
System.out.println(" a < b = " + (a < b));
System.out.println(" b >= a = " + (b >= a));
System.out.println(" b <= a = " + (b <= a));
System.out.println(" b == c = " + (b == c));
System.out.println(" b != c = " + (b != c));
}
}
Output from above program will be:
a > b = false
a < b = true
b >= a = true
b <= a = false
b == c = true
b != c = false
Ans:
class Main
{
public static void main(String[] args)
{
int num = 1234, reversed = 0;
// Main class
class GFG {
public static void main(String[] args)
{
// Creating object of class inside main()
GFG object = new GFG();
// Returning instanceof
System.out.println(object instanceof GFG);
16
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
}
}
OUTPUT
True
Dot operator :
It is just a syntactic element. It denotes the separation of class from a package, separation of method
from the class, and separation of a variable from a reference variable. It is also known as separator or
period or member operator.
o It is used to separate a variable and method from a reference variable.
o It is also used to access classes and sub-packages from a package.
o It is also used to access the member of a package or a class.
public class DotOperatorExample1
{
void display()
{
double d = 67.54;
//casting double type to integer
int i = (int)d;
System.out.println(i);
}
public static void main(String args[])
{
DotOperatorExample1 doe = new DotOperatorExample1();
//method calling
doe.display();
}
}
Output
67
17
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
18
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
16.List any four methods of String & StringBuffer class and state the use of each.
Ans:
String :
19
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
StringBuffer:
20
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
21
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
22
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
23
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
24
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
• If we want a thread to relinquish(leave) control to another thread of equal priority before its
turn comes, then yield( ) method is used.
3) Running state :
The processor has given its time to the thread for its execution.
The thread runs until it relinquishes control on its own or it is preempted by a higher priority
thread.
A running thread may change its state to another state in one of the following situations.
1) When It has been suspended using suspend( ) method.
2) It has been made to sleep( ).
3) When it has been told to wait until some events occurs.
4) Blocked state/ Waiting :
• A thread is waiting for another thread to perform a task. The thread is still alive.
• A blocked thread is considered “not runnable” but not dead and so fully qualified to run again.
5) Dead state/ Terminated :
• Every thread has a life cycle. A running thread ends its life when it has completed executing its
run ( ) method.
• It is natural death. However we can kill it by sending the stop message.
• A thread can be killed as soon it is born or while it is running or even when it is in “blocked”
condition.
25
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
22.Write a program to check whether the string provided by the user is palindrome or not.
Ans:
class Main {
public static void main(String[] args) {
String str = "Radar", reverseStr = "";
int strLength = str.length();
for (int i = (strLength - 1); i >=0; --i) {
reverseStr = reverseStr + str.charAt(i);
}
if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
System.out.println(str + " is a Palindrome String.");
}
else {
System.out.println(str + " is not a Palindrome String.");
}
}
}
26
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
Applets are small applications that are accessed on an Internet server, transported over the Internet,
automatically installed, and run as part of a web document. The applet states include:
Born or initialization state Running state Idle state Dead or destroyed state
b) Running state:
Applet enters the running state when the system calls the start() method of Applet class. This occurs
automatically after the applet is initialized. start() can also be called if the applet is already in idle state.
start() may be called more than once. start() method may be overridden to create a thread to control
the applet.
public void start()
{
//implementation
27
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
d) Dead state:
An applet is dead when it is removed from memory. This occurs automatically by invoking the destroy
method when we quit the browser. Destroying stage occurs only once in the lifetime of an applet.
destroy() method may be overridden to clean up resources like threads.
Public void destroy()
{
//implementation
}
e) Display state:
Applet is in the display state when it has to perform some output operations on the screen. This
happens after the applet enters the running state. paint() method is called for this. If anything is to be
displayed the paint() method is to be overridden.
public void paint(Graphics g)
{
//implementation
}
25. Explain the Param tag of applet. Write an applet to accept user name in the form of
parameter and print Hello + username
Ans:
To pass parameters to an applet tag is used. Each tag has a name attribute and a value attribute. Inside the
applet code, the applet can refer to that parameter by name to find its value.
The syntax of tag is as follows
<PARAM NAME = name1 VALUE = value1>
Program for an applet to accept user name in the form of parameter and print „Hello‟
import java.awt.*;
import java.applet.*;
public class hellouser extends Applet
{
28
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
String str;
public void init()
{
str = getParameter("username");
str = "Hello "+ str;
}
public void paint(Graphics g)
{
g.drawString(str,10,100);
}
}
<HTML>
<Applet code = hellouser.class|| width = 400 height = 400>
<PARAM NAME = “username” VALUE = abc>
</Applet>
</HTML>
29
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
Syntax:
void drawArc(int x, int y, int w, int h, int start_angle, int sweep_angle);
fillOval ( )
Draws an oval within a bounding rectangle whose upper left corner is specified by top, left. Width and
height of the oval are specified by width and height.
Syntax-
void fillOval(int top, int left, int width, int height)
. drawOval( )
To draw an Ellipses or circles used drawOval() method can be used.
Syntax:
void drawOval( int top, int left, int width, int height)
drawRect( )
The drawRect() method display an outlined rectangle
Syntax:
void drawRect(int top, int left, int width, int height)
28.Explain Font Class & Write method to set font of a text and describe its parameters.
Ans:
Font class A font determines look of the text when it is painted. Font is used while painting text on a
graphics context & is a property of AWT component.
The Font class defines these variables:
30
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
31
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
29.What are streams ? Write any five methods of character stream classes
Ans:
Definition: The java. IO package contain a large number of stream classes that provide capabilities for
processing all types of data. These classes may be categorized into two groups based on the data type
on which they operate.
4. public boolean ready()throws IOException – Tells whether this stream is ready to be read. An
InputStreamReader is ready if its input buffer is not empty, or if bytes are available to be read from the
underlying byte stream
5. public void mark(int readAheadLimit) throws IOException – Marks the present position in the stream.
Subsequent calls to reset() will attempt to reposition the stream to this point. Not all character-input
streams support the mark() operation.
32.Write a program to copy contents of one file to another file using character stream class.
Ans:
import java.io.*;
class CopyData
{
public static void main(String args[ ])
{
//Declare input and output file stream
FileInputStream fis= null; //input stream
33
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
33.What is the use of ArrayList Class ? State any three methods with their use from ArrayList
Ans:
1. ArrayListsupports dynamic arrays that can grow as needed.
34
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
2. ArrayListis a variable-length array of object references. That is, an ArrayListcan dynamically increase
or decrease in size. Array lists are created with an initial size. When this size is exceeded, the collection is
automatically enlarged. When objects are removed, the array may be shrunk.
35
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
36
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281
V2V EdTech LLP | Java (CO/IT/AIML) (22412) | ALL Board Questions
Try
{
for(int i = 0;i<5;i++){
set.add(count[i]);
System.out.println(set);
TreeSet sortedSet = new TreeSet(set);
System.out.println("The sorted list is:");
System.out.println(sortedSet);
System.out.println("The First element of the set is: "+ (Integer)sortedSet.first());
System.out.println("The last element of the set is: "+ (Integer)sortedSet.last());
}
catch(Exception e)
{}
}
}
Executing the program.
[34, 22, 10, 30, 60]
The sorted list is:
[10, 22, 30, 34, 60]
The First element of the set is: 10
The last element of the set is: 60
37
YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 93268814281