JAVA pt1 QB
JAVA pt1 QB
JAVA pt1 QB
Chapter 1
3. Define JDK. List the tools available in JDK explain any one in detail.
A Java Development Kit (JDK) is a collection of
1) JRE
2) tools which are used for developing, designing, debugging, executing and running java programs.
Tools of jdk:---
appletviewer: use to run java applets
java(java interpreter): runs applets and applications by reading and interpreting byte code files
javac( java compiler): translate java source code to bytecode files that the interpreter can understand
javadoc(java documentation): create HTML-format documentation from java source code files.
javah(java header): produces header files
javap: (java disassemble): convert bytecode files into a program description
jdb: (java debugger): helps us to find errors in our programs.
4. Write general syntax of any two decision making statements and also give its examples
1. if statement
This is the statement that permits you to do something when a condition is satisfied.
Syntax:
if(condition)
{
statement;
}
Program
class IfStatement
{
public static void main(String[] args)
{
int number = 10;
if (number > 0)
{
System.out.println("Number is positive.");
}
System.out.println("This statement is always executed.");
}
}
2. if -else statement
This is an extension of the if statement. In this statement, you can say what to do if the condition is satisfied and also
what to do if the condition is not satisfied.
Syntax:
if (condition)
{
statement;
}
else
{
statement;
}
Program
class IfElse
{
public static void main(String[] args)
{
int number = 10;
if (number > 0)
{
System.out.println("Number is positive.");
}
else
{
System.out.println("Number is not positive.");
}
System.out.println("This statement is always executed.");
}
}
continue:
It causes continue running a loop, but stop processing the remaining loop body for that particular iteration.
Syntax:
continue;
Program
class ContinueDemo
{
public static void main(String args[])
{
for (int i = 1; i < 10; i++)
{
if (i%2 == 0)
continue;
System.out.print(i + " ");
}
}
}
dot operator
Also known as member operator, separator or period used to separate a variable or method from a reference variable
Used to access the member of a package or a class.
objectName.variableName;
objectName.methodName(argument);
className.MethodName();
Example:
a1.y ;
a1.m2();
Math.sqrt(x);
Question Bank PT-1 Examination 2023-24
Chapter 2
Default Constructor:
In the java programming if we are not providing any constructor in the class then compiler provides default constructor.
Provided by the compiler at the time of compilation.
It is always zero argument constructors with empty implementation.
Page 1
Default constructor is executed by the JVM at the time of execution.
User defined constructors:
When user defines constructor in a class, it is called as user defined constructor.
Zero argument constructor: user defined constructor which doesn’t accept any parameter.
Example:
class Test
{
Test()
{
System.out.println("zero argument constructor");
}
public static void main(String[] args)
{
Test t=new Test();
}
}
Length The length of the String object is The length of the StringBuffer can be
fixed. increased.
4.Define wrapper class. Give the following wrapper class methods with syntax and use:
a. To convert integer number to string.
b. To convert numeric string to integer number.
c. To convert object numbers to primitive numbers using typevalue() method.
Page 2
Wrapper class wraps (encloses) around a data type and gives it an object appearance.
Wrapper classes include methods to unwrap the object and give back the data type.
Each of Java's eight primitive data types has a class dedicated to it which is called as wrapper classes
The wrapper classes are part of the java.lang package, which is imported by default into all Java programs.
Page 4
Question Bank PT-1 Examination 2023-24
Chapter 3
3.Describe the use of final keyword w. r. t. method and the variable with suitable example.
The final keyword in Java indicates that no further modification is possible. Final can be Variable, Method or Class
Final Variable
Final variable is a constant. We cannot change the value of final variable after initialization.
Sample program for final keyword in Java
class FinalVarDemo
{
final int a = 10;
void show()
{
a = 20;
System.out.println("a : "+a);
}
public static void main(String args[])
{
FinalVarDemo var = new FinalVarDemo();
var.show();
}
}
Page 1
Output:
Compile time error
Final method
When we declare any method as final, it cannot be overridden.
Sample program for final method in Java
class A
{
final void put()
{
System.out.println("put method of A class");
}
}
class B extends A
{
void put()
{
System.out.println("put method of B class");
}
public static void main(String args[])
{
B b = new B();
b.put();
}
}
Output:
Compile time error
Page 2
{
B b = new B();
}
}
Example:
interface Results
{
final static float pi = 3.14f;
float areaOf(float l, float b);
}
class Rectangle implements Results
{
public float areaOf(float l, float b)
Page 3
{
return (l * b);
}
}
class Square implements Results
{
public float areaOf(float l, float b)
{
return (l * l);
}
}
class Circle implements Results
{
public float areaOf(float r, float b)
{
return (pi * r * r);
}
}
public class InterfaceDemo
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Square square = new Square();
Circle circle = new Circle();
System.out.println("Area of Rectangle: "+rect.areaOf(20.3f, 28.7f));
System.out.println("Are of square: "+square.areaOf(10.0f, 10.0f));
System.out.println("Area of Circle: "+circle.areaOf(5.2f, 0));
}
}
Output:
Area of Rectangle: 582.61
Are of square: 100.0
Area of Circle: 84.905594
Page 4
class Test2
{
public static void main(String[] args)
{
System.out.println(sqrt(4));
System.out.println(pow(2, 2));
System.out.println(abs(6.3));
}
}
Output:
2.0
4.0
6.3
Program
import static java.lang.Math.*;
import static java.lang.System.*;
class Test3
{
public static void main(String[] args)
{
out.println(sqrt(4));
out.println(pow(2, 2));
out.println(abs(6.3));
}
}
Output:
2.0
4.0
6.3
The keyword used to create a class is “class” The keyword used to create an interface is “interface”
A class can be instantiated i.e, objects of a class An Interface cannot be instantiated i.e, objects cannot be
Classes does not support multiple inheritance. Interface supports multiple inheritance.
It can be inherited by another class using the It can be inherited by a class by using the keyword
Variables and methods in a class can be declared All variables and methods in a interface are declared as
Page 5
using any access specifier(public, private, default, public.
protected)
Variables in a class can be static, final or neither. All variables are static and final.
Page 6
PT-1 PROGRAMS CHAPTER 1-2-3(2023-24)
Ques)Implement following inheritance:
}
interface Loader
{
// public and abstract
public abstract void loadOS();
}
class Mobile1 extends Device implements Loader
{
void getData()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Details for Vendor Name, Version and Ram Size : ");
vendorName=sc.nextLine();
OSVersion=sc.nextDouble();
ramSize=sc.nextInt();
}
public void loadOS()
{
System.out.println("Details:");
System.out.println("Vendor Name:"+vendorName);
System.out.println("Version:"+OSVersion);
System.out.println("Ram size:"+ramSize);
}
// Driver Code
public static void main (String[] args)
{
Mobile1 m= new Mobile1();
m.getData();
m.loadOS();
}
}
Ques)Write a program to find largest between two numbers using‘?:’operator.
ANS)
public class LargestNumber
{
public static void main(String args[])
{
int a,b,c;
a=10;
b=20;
c = (a>b) ? a: b;
System.out.println( "Largest number between "+a+" and "+b+" is : " + c );
}
}
Ques)Write a program to divide any positive even integer by 2 using bitwise shift operator.
ANS)
public class BitwiseDivision
{
public static void main(String args[])
{
int n=Integer.parseInt(args[0]);
// divide by 2
n = n >> 1;
System.out.println("Value after division is: " + n);
}
}
for(int i = 0;i<3;i++)
{
for(int j = 0;j<3;j++)
{
c[i][j] = a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
Write a program to accept a number as command line argument and print the number is even or
odd.
ANS)
public class EvenOdd
{
public static void main(String args[])
{
int x=Integer.parseInt(args[0]);
if (x%2 ==0)
{
System.out.println("Even Number");
}
else
{
System.out.println("Odd Number");
}
}
QUES)Write a program to implement a vector class and its any eight methods
ANS)
import java.util.Vector;
class VectorExample3
{
public static void main(String[] arg)
{
Vector v = new Vector();
v.add(2);
v.add(0, 1);
v.add("thakur");
v.add("poly");
System.out.println("Vector is " + v);
System.out.println(v.contains("poly"));
System.out.println("element at indexx 2 is: " + v.get(2));
System.out.println("index of thakur is: " + v.indexOf("thakur"));
System.out.println(v.isEmpty());
System.out.println("first element of vector is: " + v.firstElement());
System.out.println("Initial capacity: " + v.capacity());
v.clear();
System.out.println("after clearing: "+v);
}
}
QUES)Write a program to define class Employee with members as id and salary. Accept data
for five employees and display details of employees getting highest salary.
ANS)
import java.util.Scanner;
class Employee
{
int empid;
String name;
float salary;
void getInput()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the empid : ");
empid = in.nextInt();
System.out.print("Enter the name : ");
name = in.next();
System.out.print("Enter the salary : ");
salary = in.nextFloat();
}
void display()
{
System.out.println("Employee id = " + empid);
System.out.println("Employee name = " + name);
System.out.println("Employee salary = " + salary);
}
public static void main(String[] args)
{
float max;
int i,d=0;
Employee e[] = new Employee[5];
for(i=0; i<5; i++)
{
e[i] = new Employee();
e[i].getInput();
}
System.out.println("The details of employee with highest salary are: ");
max=e[0].salary;
for( i=0;i<5;i++)
{
if(e[i].salary>max)
{
max = e[i].salary;
d=i;
}
}
e[d].display();
}
}