0% found this document useful (0 votes)
2 views36 pages

java1

The document provides an overview of programming concepts, focusing on Java, including definitions of programming, its importance, paradigms, and specific features of Java such as object-oriented principles, data types, variables, methods, and exception handling. It also covers tools for programming, types of operators, conditional statements, loops, and the principles of object-oriented programming (OOP) like inheritance, encapsulation, polymorphism, and abstraction. Additionally, it includes sample code snippets for various programming constructs and explanations of access modifiers and interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views36 pages

java1

The document provides an overview of programming concepts, focusing on Java, including definitions of programming, its importance, paradigms, and specific features of Java such as object-oriented principles, data types, variables, methods, and exception handling. It also covers tools for programming, types of operators, conditional statements, loops, and the principles of object-oriented programming (OOP) like inheritance, encapsulation, polymorphism, and abstraction. Additionally, it includes sample code snippets for various programming constructs and explanations of access modifiers and interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

1.what is programming.

a)Programming is way to instruct the computer to perform a


task in a specified manner..
2….why programming is important?
A))now a days so much of our world is automated.it gives
interaction between the human and the machine.
3….programming paradigms?
Programming paradigms are two types
1.imperative
2.declarative
1)imperative divided into sub types major types are
1.functional programming
2.oops
2)declarative divided into sub types they are:
1.structural programming
2.logical programming
4….why we use java?
a)Java is a object oriented programming language.it is used
to develop any kind of applications like desktop
Application and web applications etc.
5….About java?
a))1.java was introduced by james gosling in 1991 but it was
first implemented in 1996.
2.java was introduced by sun micro system.
3.java currently maintained by oracle corporation.
Advantages:
1.simple
2.platform independent
3.secure
4.robust
Etc..
Disadvantages:
1.slower performance than c++.
2.not good ui.
6….what are tools available for write programming.
a)1.elclipse
2.intelij idea
3.netbeans
4.visual studio code.

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:
 +=
 -=
 /=
 %=
 *=
 =

1..Program for Arthmetic operators:


Public class Operators{
Public static void main(String[] args){
Int i=20;
Int j=10;
System.out.println(i+j);
System.out.println(i-j);
System.out.println(i*j);
System.out.println(i/j);
System.out.println(i%j);
}
}
2..program for unary operators.
Public class Operators{
Public static void main(String[] args){
Int i=20;
System.out.println(i) ;
System.out.println(i++);
System.out.println(i--);
System.out.println(i);
System.out.println(++i);
System.out.println(--i);
}
}

3..program for Relational operators:


Public class Operators{
Public static void main(String[] args){
Int i=20;
Int j=10;
System.out.println(i==j);
System.out.println(i!=j);
System.out.println(i<j);
System.out.println(j>i);
System.out.println(i<=j);
System.out.println(i>=j);
}
}

4.program for conditional operators:-


Public class Operators{
Public static void main(String[] args){
Int i=10;
Int j=20;
System.out.println(i<j&&i<=j);
}
}
5.program for Assignment operators:
Public class Operators{
Public static void main(String[] args){
Int i=10;
I+=5;
System.out.println(i);
}
}
13.Types of conditional statements?
a)1.if
2.if-else
3.Switch
Program for if statement:
Public class decisionmaking{
Public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println(“enter a number”);
int number=scanner.nextInt();
if(number<0){
System.out.println("this is negative number ");
}

}
}

2..program for if else statement:


public class Decisionmaking{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println("enter first number");
int number=scanner.nextInt();
if(number%2==0) {
System.out.println("even number");
}
else {
System.out.println("odd number");
}

}
}
3.program for switch statement:
import java.util.Scanner;

public class Decisionmaking{


public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println("enter first number");
int number1=scanner.nextInt();
System.out.println("enter second number");
int number2=scanner.nextInt();
System.out.println("enter choice number");
int choice=scanner.nextInt();
int result;
switch (choice) {
case 1:
result=number1+number2;
System.out.println("the sum is"+result);
case 2:
result=number1-number2;
System.out.println("the difference is"+result);
case 3:
result=number1*number2;
System.out.println("the product is"+result);
}
}
}
14.what is constructor?
a)constructor is a block of code similar to the method.
It is called when object is created.
Ex:Object ob=new Object();
**we are not creating define constructor but we are
calling. That means java will create default constructor.
It is called as implicit constructor.
**otherwise we are creating constructor in a class that
is called explicit constructor.

1.Program for constructor:


import java.util.Scanner;

public class Object{


int i;
int j;
public Object() {
i=20;
j=10;
}
public Object(int i,int j) {
this.i=i;
this.j=j;
}

public static void main(String[] args) {


Object Ob=new Object();
Object Ob1=new Object(24,24);
Object Ob2=new Object(24,27);

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.

****Shortcut for single line comments:


Ctrl+7
****Shortcut for multi line comments:
Ctrl+shift+/
****Shortcut for documented comments:
Shift+alt+j
16.what is looping statements and its types.
a)Definition: looping statments is a statements execute
one or more statements repeatedly several number of
times.
Types:
1.while
2.do-while
3.for
4.foreach
1..program for while loop.
Public class Loop{
Public static void main(String[] args){
While(i<10){
System.out.println(“i”);
i++;
}
}
}
**Program for two dimensional array to finding sum:
public class Arrays {

public static void main(String[] args) {


int[][]arr=new int[3][3];
arr[0]=new int[] {1,8,4};
arr[1]=new int[] {9,7,2};
arr[2]=new int[] {7,6,4};
int sum=0;
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr[i].length;j++) {
sum+=arr[i][j];

}
System.out.println("sum of all elements "
+ sum)
}
}
}
**program for single dimensional array.
public class Arrays {

public static void main(String[] args) {


int[]arr=new int[3];
arr[0]=5;
arr[1]=8;
arr[2]=4;

for(int i=0;i<arr.length;i++) {
System.out.println(arr[i]);

}
}
}

**program for for-each loop:


public class Loop{
public static void main(String[] args) {
int[]arr= {1,2,7,8,2,3,4,1};
for(int value:arr) {
System.out.println(value);
}
}
}
Program for for each using two dimensional array:
public class Loop{
public static void main(String[] args) {
int[][]arr= {{2,4,5},{2,7,3},{1,3,5}};
int sum=0;
int noofelements=0;
for(int[] array:arr) {
for(int value:array) {
sum+=value;
noofelements++;
}

}
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;

public class Strings {

public static void main(String[] args) {


Scanner scanner=new Scanner(System.in);
System.out.println("enter a word");
String s1=scanner.nextLine();
System.out.println(s1.length());
System.out.println(s1.trim());
System.out.println(s1.lowercase());
}

}
18.What is static or non-static::

*normally allocation of memory two types they are:


1.object creation.
*it takes more space in memory. when each object
creation separate memory is allocated.
2.static
**it has shared memory.
20.What is oops?
*oops means object oriented programming system.
Principles of oops:
1.Inheritance.
2.Encapsulation.
3.Polymorphism.
4.Abstraction.
Programming language that follow oops:
1.c
2.c++
3.python
4.simula:it is a first object oriented programming
language.
5.smalltalk:-it is a truly object oriented programming
language.
****1.Inheritance:Inheritance in java means aquires
properties from one class to the another class.
Program:-
Public class Admin extends Developer{
Public void read{
Sytem.out.println(“read code”);
}
Public void Write{
System.out.println(“read code”);
}
Public code Manage{
System.out.println(“manage code”);
}
}
Program:
Public class Admin Extends Developer{
Public void manage(){
System.out.println(“manage code”);
}
}

}
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;
}

public boolean isAttendence() {


System.out.println("teacher assigned attendence");
return Attendence;

public void setAttendence(boolean attendence) {


Attendence = attendence;
System.out.println("teacher accessed student attendence");

}
}

public class Teacher {


public static void main(String[] args) {
Student stu=new Student();
stu.Attendence=true;
//stu.setStudentAttendence(true);
stu.isAttendence();

stu.setAttendence(true);
//stu.Name;

21.what is access modifiers and its types?


a)Definition:the access modifiers is a scope of the
members of a class.
Types:
1.public access modifiers.
2. private access modifiers.

3.protected access modifiers.


4. Default access modifiers.
22.What is an interface?
A)An interface is the blueprint of a class.
*if you we want to implement interface use keyword
implements.
Program:
Public class Laptop{
Public void copy();
Public void paste();
Public void cut();
Public void camera();
}
Public class Lenovo{
Public void copy{
System.out.println(“Lenovo copy code”);
}
public void paste{
System.out.println(“Lenovo paste code”);
}
Public void cut{
System.out.println(“Lenovo cut code”);
}
Public void camera{
System.out.println(“Lenovo camera code”);
}
}

Public class user{


Public static void main(String[] args){
Lenovo Lenovo=new Lenovo();
lenovo.copy();
Lenovo.paste();
Lenovo.cut();
Lenovo.camera();
}
}
23)abstract class:
a)abstract class extends class with abstract name.
program:

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

 The default capacity is 0.


 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 not follow synchronisation.
Stack(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 used in undo,redoetc.

You might also like