Java 1stut Qb Ans
Java 1stut Qb Ans
(ANSWERS)
4Marks
1) Explain Four Features of Java.
I. Object Oriented: In Java, everything is an Object. Java can be easily
extended since it is based on the Object model.
II. Platform Independent: Unlike many other programming languages
including C and C++, when Java is compiled, it is not compiled into
platform specific machine, rather into platform independent byte code.
This byte code is distributed over the web and interpreted by the
Virtual Machine (JVM) on whichever platform it is being run on.
III. Simple: Java is designed to be easy to learn. If you understand the
basic concept of OOP Java, it would be easy to master.
IV. Secure: With Java's secure feature it enables to develop virus-free,
tamper free systems. Authentication techniques are based on public-
key encryption.
Conditional Operator:
The Conditional Operator is used to select one of two expressions for
evaluation, which is based on the value of the first operands. It is used to
handling simple situations in a line.
Syntax: expression1 ? expression2:expression3;
The above syntax means that if the value given in Expression1 is true, then
Expression2 will be evaluated; otherwise, expression3 will be evaluated.
Example
class test {
public static void main(String[] args)
{
String result;
int a = 6, b = 12;
result = (a==b ? "equal":"Not equal");
System.out.println("Both are "+result);
}
}
3) Describe Concept of Type Casting and Explain its Types With proper
syntax and example.
I. The process of converting one data type to another is called casting or type
casting.
II. If the two types are compatible, then java will perform the conversion
automatically.
III. It is possible to assign an int value to long variable.
IV. However, if the two types of variables are not compatible, the type conversions
are not implicitly allowed, hence the need for type casting.
There are two types of conversion:
1.Implicit type-casting.
2.Explicit type-casting.
1. Implicit type-casting:
Implicit type-casting performed by the compiler automatically; if there will be no loss of
precision.
Example:
int i = 3;
double f;
f = i;
output: f = 3.0
2. Explicit type-casting:
• Explicit type-casting performed via a type-casting operator in the prefix form of (new-
type) operand.
• Type-casting forces an explicit conversion of type of a value. Type casting is an
operation which takes one operand, operates on it and returns an equivalent value in the
specified type.
Syntax:
new Value = (typecast)value;
class abc{
float pi,radius;
abc(float p, float r)
{
pi=p;
radius=r;
}
void area()
{
float ar=pi*radius*radius;
System.out.println("Area="+ar);
}
void display()
{
System.out.println("Pi="+pi);
System.out.println("Radius="+radius);
}
}
class area
{
public static void main(String args[])
{
abc a=new abc(3.14f,5.0f);
a.display();
a.area();
}
}
Example :
class A {
void display()
{
System.out.println(“In Parent class A”);
}
}
class B extends A //derived class B from A
{
void show()
{
System.out.println(“In child class B”);
}
public static void main(String args[])
{
B b= new B();
b.display(); //super class method call
b.show(); // sub class method call
}
}
Multilevel inheritance:
In multilevel inheritance, a subclass extends from a superclass and
then the same subclass acts as a superclass for another class.
Basically it appears as derived from a derived class.
Example:
class A
{
void display()
{
System.out.println(“In Parent class A”);
}
}
class B extends A //derived class B from A
{
void show()
{
System.out.println(“In child class B”);
}
}
class C extends B //derived class C from B
{
public void print()
{
System.out.println(“In derived from derived class C”);
}
public static void main(String args[])
{
C c= new C();
c.display(); //super class method call
c.show(); // sub class method call
c.print(); //sub-sub class method call
}
}
9.Define an Exception Called “No Match Exception” that is thrown when
the password accepted is not equal to “MSBTE”. Write the program.
import java.io.*;
class NoMatchException extends Exception
{
NoMatchException(String s)
{
super(s);
}
}
class test1
{
public static void main(String args[]) throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in) );
System.out.println("Enter a word:");
String str= br.readLine();
try
{
if (str.compareTo("MSBTE")!=0) // can be done with equals()
throw new NoMatchException("Strings are not equal");
else
System.out.println("Strings are equal");
}
catch(NoMatchException e)
{
System.out.println(e.getMessage());
}
}
}
10.Define Exception. State Built-In Exceptions.
An exception is a problem that arises during the execution of a
program.
Java exception handling is used to handle error conditions in a program
systematically by taking the necessary action
Built-in exceptions:
• Arithmetic exception: Arithmetic error such as division by
zero.
• Class Not Found Exception
• File Not Found Exception: Caused by an attempt to access
a nonexistent file.
• IO Exception: Caused by general I/O failures, such as
inability to read from a file.
• NullPointerException: Caused by referencing a null object.
• SecurityException: Caused when an applet tries to perform
an action not allowed by the browser’s security setting.
• StackOverflowException: Caused when the system runs out
of stack space.
6Marks
2) toUppercase():
Converts all of the characters in this String to upper case
Syntax: s1.toUpperCase()
Example: String s="Sachin";
System.out.println(s.toUpperCase());
Output: SACHIN
3)trim ():
Returns a copy of the string, with leading and trailing whitespace
omitted.
Syntax: s1.trim()
Example: String s=" Sachin ";
System.out.println(s.trim());
Output:Sachin
5. length():
Syntax: int length()
It is used to return length of given string in integer.
Eg. String str=”INDIA”
System.out.println(str.length()); // Returns 5
3) Write a program to define class Emplyoee with members as id and salary .Accept data for
five employees and display details of employees getting highest salary.
Display()
Class:Gross_salary
TA,DA,HRA
Total_sal()