java1
java1
Program:
Public class Test{
Public static void main(String[] args){
System.out.println(“hello world”);
}
}
7..what is PaCkage?
A)pakage is a container. which is used to segregates the files
in java project.
8..What is class.?
a)Class is a container. which is used to store entire
information of class.
Members of class are:
1.Variables
2.Methods
Program:
Public class Dog{
Public string breed=”german shephard”;
Public int weight=”20kg”;
Public int height=”2 feet”;
Public static void main(String[] args){
Dog dog=new Dog();
System.out.println(“dog.breed”);
System.out.println(“dog.weight”);
System.out.println(“dog.height”);
}
}
9..what are the data types?
Types are:
1.primitive
2.non primitive
1)primitive divided into three types:
1.numbers
2.boolean
3.character
Numbers divided into two types:
1.integer:
1.byte
2.int
3.short
4.long
2.decimal:
1.float
2.double
2)Boolean:
*True or false
Program:
Public class Datatypes{
Public static void main(String[] args){
System.out.println(“Byte.size”\8);
System.out.println(“Byte.MIN_VALUE);
System.out.println(“Byte.MAX_VALUE);
}
}
Non primitive data types are:
1.Arrays
2.Interfaces
3.Collections
4.class
10….what is variable?
A)variable is a container it is used to store values.
Types are:
1.instance variables
2.class variables
3.local variables
4.parameters.
1.Program for instance variables:
Public class Variables{
Int id=”205”;
Public Static void main(String[] args){
Variables va=new Variables();
System.out.println(“var.id”);
}
}
2.program for class variable:
Public class Variables{
Static Int id=”205”;
Public Static void main(String[] args){
System.out.println(“var.id”);
}
}
3.program for local variables:
Public class Variables{
Public Static void main(String[] args){
Int id=”205”;
Variables va=new Variables();
System.out.println(“var.id”);
}
}
4.program for parameters:
Public class Variables{
Public Static void main(String[] args){
Test1(70);
Public Static void test1(int height){
Int height=”200”;
System.out.println(“height”);
}
}
11..what is a method?
a)method is a block of code or group of statements
work together to perform a task and it return caller if
capable.
Types of methods:
1.static method
2.non static method
Program:
Public class Rat{
Public static void weight(){
System.out.println(“half kg”);
}
Public void food(){
System.out.println(“grains”);
}
Public static void colour(){
System.out.println(“black”);
}
Public void main place(){
System.out.println(“holes”);
}
Public static void main(String[] args){
Rat.weight();
Rat f=new Rat();
f.food();
Rat.colour();
Rat p=new Rat();
p.place();
}
}
11..Different types of print satments?
a)1.printf:it is used in formatting strings
2.print:cursor will be place in the same line.
3.println:cursor move to the next line.
12..what is operator?
a)operator is a symbol used to perform operations on
variables or values.
Types of operators:
1.Arthmetic operator:
2.unary operator
3.Relational operator
4.conditional operators
5.Assignment operators
1.Assignment operators:--
Addition
Subtraction
Multiplication
Divison
Modulus
2..unary operators:
Increment
Decrement
3..Relational operators:
>=
<=
==
!=
<
4.conditional operators:
And(&&)
Or(||)
5.Assignment operators:
+=
-=
/=
%=
*=
=
}
}
}
}
3.program for switch statement:
import java.util.Scanner;
System.out.println(Ob.add());
System.out.println(Ob1.add());
System.out.println(Ob2.add());
}
public int add(){
return i+j;
}
}
15.Different types of comments?
a)1.single line comments
2.multi line comments.
3.Documentation comments.
}
System.out.println("sum of all elements "
+ sum)
}
}
}
**program for single dimensional array.
public class Arrays {
for(int i=0;i<arr.length;i++) {
System.out.println(arr[i]);
}
}
}
}
System.out.println(sum);
System.out.println(sum/noofelements);
}
}
17.What is String:
String is a sequence of character and it is a class in java
but it considered as a literal.
**it is used to store “text”.
*string is non primitive data type.
Different ways to create string:
1.String s1=”nandu”;
2.string s2=new String(“hello”);
Assignment:
**program for to count the number of characters in a
string.
package string;
import java.util.Scanner;
}
18.What is static or non-static::
}
Encapsulation:
Encapsulation means binding code and data together in a single unit called encapsulation.
Program :-
public class Student {
int rollnumber;
String name;
boolean Attendence;
public Student() {
this.rollnumber = 502;
}
}
}
stu.setAttendence(true);
//stu.Name;
24)what is abstraction?
a)Abstraction means hiding internal details and
shoeing their functionality.
*through interface we achieve abstraction 100%
before java 1.8 version.
*After 1.8 version may or may not be we achieve 100%
abstraction.
25)What is polymorphism?
A)Ability of an object to take many forms called
polymorphism.
Different types of polymorphism:
1)compile-time polymorphism.
2)Run-time polymorphism.
1)compile -time polymorphism is a type of
polymorphism.it will occur in the compile time.
**it is also referred as static polymorphism.
**compile time polymorphism achieved by the method
overloading concept in java.
2)Run-time polymorphism:
Run-time polymorphism is a type of polymorphism.it
will occur in the Run time.
**it is also referred as dynamic polymorphism.
**Run- time polymorphism achieved by the method
overriding concept in java.
Method overloading:
Method overloading means that allows multiple
methods but different in parameters.
Ex:
System.out.println();
System.out.print();
System.out.printf();
Method overriding:
Method overriding means when a child provides anew
implementation that already defined in theparentclass.
26)What is an Exception?
A)Exception is an event that occur during the execution
of a program that distrupts the normal flow of an
instruction.
27)What is Exception Handling?
A)If an exception occurs means the java stop the
execution. that is handled by ourself means Exception
handling.
Different types of Exception:
1)checked-Exceptions.
2)unchecked-Exceptions.
28))Files in the java?
1.Absolute path.
2.Relative path.
Program:
Public class Files{
Public static void main(String[] args){
File f=new File(“./sample.txt”);
If(!f.exist())
f.createnewfile();
FileInputStream fis=new FileInputStream(f);
Int asciicode;
While(asciicode=fis.read())!=-1){
System.out.println((char)asciicode);
}
fis.close;
}
}
29))What are the blocks?
a)
1.Try-catch
2.Finally.
3.Throw
4.Throws
30)What is pojo and java bean?
a))*pojo means plain old java object.
*It is a simple java object ,which is not bounded by any
special restrictions.
Rules for a class to be called as pojo-class:--
it must be public.
It should not implement any interface.
It should not extend any classes.
It should not have any annotations specified.
It is recommended to make the properties as
private to improve security.
It can have getters & setters in order to access the
properties.
Rules for a class to be called as a java bean class:--
It should implements with serializable interface.
It should have no-args constructor.
All the properties should be private.
It should have getters &setters in order to access
the properties.
31)What is the vector and the ArrayList?
a)Vector: Vector is a re-sizable array.
Major operations in both arrays and the Arraylist:--
Creation of a list.
Addition in a list.
Deletion in a list.
Updation in a list.
Retriving from a list.
Verification in a list.
Introduced :
Vector introduced in 1.0 version.
Collection introduced in 1.2 version.
Generics introduced in 1.5 version.
Vector(important points):--
*100% increment occurs array size.
Ex:
10
20
40
The default capacity is 10.
The initial capacity is 10.
It allow duplicate values.
It allow null values.
It maintains the insertion order.
It does not maintain sorted order.
It follow synchronisation.
It is good at multithreading, data increased
exponentially.
Arraylist(important points):--
*50% increment occurs array size.
Ex:
10
15
20