1. What is an array? Explain various types of array?
Array: is a sequenced collection of related data items that shares a comman name.
•The elements of an array can be accessed by using index, which always start from 0th index.
There are two types of array:-
i) Single Diamentional
ii) Multi Diamentional
i). Single Diamentional: The array which have only one subscript is called as single Diamentional array or 1D array
Syntax: datatype array_name[ ];
Object of an 1D array
Syntax:
datatype array_name[ ]= new datatype [size];
Example: sum of odd & even numbers, maximum & minimum elements of an array amd many more...
/* Total marks obtained by a student in 3 subjects*/
import java.util.Scanner;
public class Array1D {
public static void main(String[] args) {
int a[], i, total = 0;
System.out.println("Enter your 3 subject marks: ");
Scanner sc = new Scanner(System.in);
a = new int[3];
for (i = 0; i < 3; i++) {
a[i] = sc.nextInt();
total = total + a[i];
}
System.out.println("Total marks: " + total);
}
}
ii). Multidimensional array: The array which have more than one subscript then it is said to be a multidimensional array.
Syntax: datatype array_name [ ] [ ];
Object of an multidimensional array
datatype array_name[][]=new datatype[size][size];
Example: sum of matrix, multiplication of matrix and many more...
/*Sum of matrix*/
public class Array2D {
public static void main(String[] args) {
int a[][] = {{1, 1}, {2, 2}}, i;
int b[][] = {{1, 1}, {2, 2}}, j;
System.out.println("Sum of two matrix");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) System.out.print(a[i][j] + b[i][j] + " ");
System.out.println();
}
}
}
2. Explain about various operators in Java with example.
• Arithmetic operator: is used for numerical calculation like (+,-,*,/,%)
• Relational operator: comparision can be done with relational operator.
• Logical operator: are you type
Logical And(&&)
Logical Or(||)
Logical Not(!)
Logical operator returns boolean value
• Assignment operator: it is used to assign the result of an expression to a variable.
Two types
Symple assignment (a=56)
Compound assignment (a+=56, a-=56...)
• Increment or Decrement operator: is used to increment or Decrement the value by 1
It of two types
i) pre increment/pre decrement
In this the value is first increment/decrement by 1 then the operation is performe.
ii) post increment/post decrement
In this the value is Incremented/decrement after the operation is performed.
• Condition operator:
Ternary operator (?:) is called as condition operator it is used to check condition like if and else.
• Bitwise operator: is used to manipulate the data at bit level or it is used to perform operations on individual bits
Type:
Bitwise and, Bitwise or, left shift (<<),right shift(>>), bitwise complement.
3. Write about control statements with example.
Control statements are of 3 types
i) condition statement: To check the conditions we use conditional statements
If, if else, else if ladder, nested if, switch
• If: syntax: if (condition)
int i=3, j=8;
if(i>j)
System.out.println("i is greater ");
if(j>i)
System.out.println("j is greater");
{ True-block statement;}
• If else
if (condition){
True-block statement;}
else{
False-block statement}
int i=3, j=8;
if(i>j)
System.out.println("i is greater ");
else
System.out.println("j is greater");
• else if ladder
Syntax: if(condition){
Statement 1;}
else if(condition 2){
Statement 2;
}
else if(condition 2){
Statement 2;
}
else
Statement ;
• Switch is a multi conditional checking statement
Syntax: switch(option){
case label 1
Statement 1; break;
case label 2
Statement 2; break;
default statement;
break;}
ii) Looping Statement: to execute a statement for more than one time we use looping statement.
i) for():
for(initialisation; condition; increment/decrement)
is percodition checking
int i;
for(i=0;i<5;i++){
System.out.println(i);
}
ii) while():is also a precondition checking
Syntax: initialisation;
while (condition){
Statement; incre/decrem;
}
Example:
i=5;
while (i<7){
System.out.println(i);
i++;}
iii) do while (): is post condition checking
Syntax: initialisation;
do{
Statement; incre/decr;
} while (condition);
Eg:
int i=5;
do{
System.out.println(i);
i++;
} while (i<5);
3. Jumping statement are
i. break
ii. continue
iii. return
i.break: are used to stop the execution of statement
Example: switch, and in if also we use break statement to stop at certain condition.
ii. Continue: are used for continue the statement after some condition .
iii. Return are used in methods to returns any value.
4.Write about string handling function.
String: is a collection of character which are written in double quotes.
1. charAt: To find the character based on index number.
2. compareTo: it compares 2 string and returns +ve, -ve, 0.
3. equals: compare 2 string and returns boolean value.
4. trim: to concatenate 2 string without space.
5. toLowercase: Convert upper case string to lower case.
6. toUppercase: Convert lower case string to upper case.
7. length: to find the length of string.
5. What is inheritance explain various types of inheritance?
Inheritance: it is a property by which one can acquires the property and functionality of another class.
There are 4 types of inheritance
i. Single inheritance: when sub class get the property of super class is called SI
Example
class A{
Some method(){ ...}
}
class B extends A{
}
ii. Multi level inheritance: A child class or base class has more then 1 parent class.
Example
class A{
Some method (){...}
}
Class B extends A {
Some method 2(){...}
}Class C extends B {
Some method 2(){...}
}
iii. Hierarchical inheritance: More than one class has the property of single class.
Example:
Example
class A{
Some method (){...}
}
Class B extends A {
Some method 2(){...}
}Class C extends A {
Some method 2(){...}
}
iv. Multiple inheritance: sub class inherit the property of two or more then two super class.
6. What is constructor ? And explain various types of constructor.
Constructor: A method with same name of class is called constructor.
It is used to initialised the variable of class
There are 2 types of constructor:-
1. Default constructor are without parameters
2. Parameterized constructor: are with parameters.
• constructor does not have return type.
Example: default
public class pr {
int a,b;
pr() {
a=10;
b=20;
}
public static void main(String[] args) {
pr obj=new pr();
System.out.println(obj.a);
System.out.println(obj.b);
}
}
Parameterized example:
public class pr {
int a,b;
pr(int i, int j) {
a=i;
b=j;
}
public static void main(String[] args) {
pr obj=new pr(10,20);
System.out.println(obj.a);
System.out.println(obj.b);
}
}
7. What is polymorphism? List various types of polymorphism and differenciate between overloading and overriding.
Polymorphism: ability of an object to take multiple form is called polymorphism
There are 2 types of polymorphism:-
• Compile time polymorphism
• Run time polymorphism
In compile time polymorphism overloading occuers are two type
i. Function overloading
ii. Operator overloading
Example of function overloading
class sum1 {
void pra() {
System.out.println("No parameter");
}
void pra(int a);
{
System.out.println("Value of a: "+a);
}
void pra(int a,int b) {
System.out.println("a value: "+a+" b value: "+b);
}
double pra(double a,double b) {
System.out.println("Double a: "+a+"Double b:. "+b);
return a+b;
}
}
public class sum {
public static void main(String[] args) {
sum1 s1=new sum1();
s1.pra();
s1.pra(10);
s1.pra(10,20);
double result=s1.pra(10.1,20.1);
System.out.println("sum of double: "+result);
}
}
Example operator overloading
public class opoverload {
String operator(String s1, String s2) {
return s1 + s2;
}
int operator(int s1, int s2) {
return s1 + s2;
}
public static void main(String[] args) {
opoverload op1 = new opoverload();
int result = op1.operator(10, 20);
System.out.println(result);
String s = op1.operator("Hai", "Hello");
System.out.println(s);
}
}
Overloading
• occurs in same class
• overloading occurs at compile time
• Main method can be overloaded.
• Static method can be overloaded.
• Final method can be overloaded.
• Private method can be overloaded.
Overriding
occurs in same class
• overriding occurs at run time
• Main method cannot be overrided
• Static method cannot be overrided
• Final method cannot be overrided
• Private method cannot be overrided
8. Explain about abstraction class with example?
Abstraction (): A method which does not contain body is called abstraction method
Abstraction class: A class which contain atleast one abstract method is called as abstraction class.
Example:
abstract class demo {
abstract void study();
void display() {
System.out.println("bai");
}
}
public class opoverload extends demo {
void study() {
System.out.println("All the best");
}
public static void main(String[] args) {
opoverload obj = new opoverload();
obj.study();
obj.display();
}
}
9. Explain about various form of inheritance.
Following are the various form of inheritance
1. Specialization inheritance: sub class is a special form of super class
2. Specification inheritance: super class specifies the method/task to sub class and it doesn't provide body of the method (abstraction inheritance)
3. construction inheritance subclass is able to change the behaviour of super classe.(upcasting)
4. Limitation: sub class will keep restrictions on super class
5. Combination inheritance: sub class inherit the properties from more than one class (multiple inheritance)
6. Extension: sub class will add its own property to super class property.
10. Explain about super, final and static keywords.
• super: it represents immediate parents class, and it is used to invoke superclass instance variable.
Super(): is used to invoke super class constructor
Final(): is used to restrict the user and user can't change the value of variable
Static: keyword is ment to call variable and method without using an object.
Whatever answers are there all are my own answer so i also don't know whether all answer are 100% correct only so, i am not responsible if anything happens wrong due to this...
If anything noticed wrong in this post then please let me know...
0 Comments