Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

JAVA & OOP

1.What is OOP?

Object Oriented Programming is a programming paradigm based on the concept of


“objects” and “classes”. it is used to structure a software program into simple reusable
pieces of code blue prints (classes), which are used to create individual instance of
objects.

2.What is a class?

An extensible program-code-template for creating objects.

3.What is an object?

Objects are instances of a class created with specifically defined data.

4.What is Method?

Group of statements that perform some operation on some data and may or may not
return a result.

5.What are the key features in java?

Free & open source


Platform independent
Automatic memory management strategy and garbage collection in the object cycle.

6.What is IDE?

Integrated Development Environment that combines basic tools developers need to write
and test software (editor, compiler, interpreter ,debugger can accessed through single
GUI)

7.What is JVM?

Java Virtual Machine that enables a computer to run java programs as well as programs
written in other languages that are also compiled to java bytecode.

8.What is native code?

Programming code that is compiled to run with a particular processor.

9.What is JIT(Just-In-Time) compiler?

Translates Java byte code to native machine language and execute.

1|Page
10. JDK (Java Development Kit) and JRE(Java Runtime Environment) explain?

JRE is a software layer that runs on top of a computer’s OS system software and provides
the class libraries and other resources.

2|Page
1.0 JAVA Programming

11.Basic program of java.

public class Basic{


public static void main(String args[]){
System.out.println("Hello Word");
}
}
12.Take user input and print.
import.java.util.*;

public class Basic{

public static void main(String args[]){


Scanner input=new Scanner(System.in);
System.out.println("Enter number");
int n=input.nextInt();//input.nextLine,input.nextDouble

System.out.println(n);

}
13.If-else Statement

if(Boolean-expression)//>,<,==,!=
{
Statement-1;//when condition pass

}else{
Statement-2;//when first condition fails
}

3|Page
14.Nested-if Statements
if –else in another if-else

if(Boolean-Expression1){

if(Boolean-Expression2){

Statement-1;

}else{

Statement-2;
}
}else{
Statement-3
}

15.If-Else-If Statements

if(Boolean-Expression1){
Statement1;
} else if(Boolean-Expression2){
Statement2;
} else if(Boolean-Expression3){
Statement3;
}else{
Statement4;
}

4|Page
16.Switch Statements

switch(switch-expression){
case casevalue_1:
statement1;
break;

case casevalue_2:
statement2;
break;

case casevalue_n:
statement n;
break;

default:
statement;
}

17.While Statement

while(condition)
{
Statements;
increment++/decrement--;
}

18.Do-While Statement

do
{
Statements;
increment++/decrement--;
}
while(condition);

5|Page
19.For Statement

for(initialization; loop condition; increment++/decrement--)
{
statement-1;
statement-2;

statement-n;
}

20.Nested Loops

for(initialization; loop condition; increment++/decrement--)
{
for(initialization; loop condition; increment++/decrement--)
{
statement-1;
statement-2;

statement-n;
}
statements;
}

1.1 Objects-Classes

21. Declare a class in java.

Class NameOfClass{
}

22.What are the class declaration components?


 Access modifires
 The class name
 Super class name
 List of interfaces

6|Page
23.Explain Access Modifies.
 Public- Accessible from all classes.
 Private-Accessible only within it’s own class.
 Protected- Accessible to class within same package & for sub classes via inheritance.
 Default- Accessible only to the class within package.
24.What are the field declaration components?
 Access modifiers
 Field Data type
 The field name
25.Method define Format.
<Access Specifier><Return Type><Method Name>(<input parameters>){
}
public static void main(string[]args){
}
26.What are the characteristics of constructor?
 Same name of the class.
 No return type, not even void.
 Can have any number of arguments.
 Can have member initialization lists in order to initialize member data.
27.What are local variables?
 Local variables are declared in methods, constructors or blocks. They are created when
the method constructor is entered and the variable will be destroyed once it exits the
method, constructor or block.
28.What are the characteristics of using this keyword?
 To invoke current class method
 To invoke current class constructor
 To pass argument in the method
 To pass argument in the constructor call
 To return current class instance
 If local and instance variables are different it is not required to use this keyword

7|Page
29.Give example for static method using,
public class methUse{
public static void firstMeth(){
System.out.println(“Hello”);
}
public static void main(String args[]){
firstMeth();
}
}
30.Give example for non-static method using,
public class methUse{
public void firstMeth(){
System.out.println(“Hello”);
}
public static void main(String args[]){
methUse obj=new methUse();
obj.firstMeth();
}
}

8|Page
31.Give example for multiple class using.
public class phone{
public void sendMessage(){
System.out.println(“Hello”);
}
}
public class mobile{
public static void main(String args[]){
phone obj=new phone();
obj. sendMessage();
}
}
*if use notepad should create two java files in same folder.
*net beans; new project>source packages>new package>two java classes

32.Give example for use this keyword

class Employee{
int empId;
String name;

Employee(int empId,String name){


this.empId=empId;
this.name=name;
}
void Display(){
System.out.println(“\nEmployee ID:”+empId”\nEmployee name:”+name);
}
}

class EmpThis{
public static void main(String args[]){
Employee e1=new Employee(2586,”Nisal”);
e1.Display();
}
}
9|Page
1.2 Encapsulaton

33.What are the reasons for use encapsulation?

 It is the process by which data is bundled together with operators.


 Hiding or limiting of access to data to ensure security.

34.Give example for encapsulation.


class EncapTest{
private String name; //fields are private
private int age;

public int getAge(){


return age;
}

public String getName(){


return name;
}

public void setAge(int newAge){


age=newAge;
}

public void setName(String newName){


name=newName;
}
}
public class RunEncap{
public static void main(String args[]){

EncapTest encap=new EncapTest();

encap.setName(“David”);
encap.setAge(22);

System.out.println(“Name: “+encap.getName()+” Age is: ”+encap.getAge());


}
}

10 | P a g e
1.3 Inheritance
35.What is inheritance?
 Enables to reuse the functionalities and capabilities of the existing class by extending a
new class from the existing class and adding new features to it.
36.What are sub and super classes?
 Sub class- the class that inherits the data members and methods from another class.
 Super class-The class from which the subclass inherits.
37.Give example for use of super keyword.
*super keyword is used to invoke the constructor of super class
public class Car{
private String color;
private int noSeats;
public Car(String color,int noSeats){
this.color=color;
this.noSeats=noSeats;
}
}
public class Premio extends Car{
private String model;
public Premio(String color,int noDeats,String model){
super(color,noSeats);
this.model=model;
}
}

11 | P a g e
38.Give example for inheritance(implement super & sub classes).
class Calculator{
int z;
public void addition(int x,int y){
z=x+y;
System.out.println(z);
}
public void subtraction(int x,int y){
z=x-y;
System.out.println(z);
}
}
public class MyCalculator extends Calculator{
public void multiplication(int x,int y){
z=x*y;
System.out.println(z);
}
public static void main(String args[]){
int a=20,b=20;
MyCalculator my1=new MyCalculator();
my1.addition(a,b); //40
my1.subtraction(a,b); //0
my1.multiplication(a,b); //400
}
}

12 | P a g e
1.4 Polymorphism
39.What is polymorphism?
 Programming language’s ability to process objects differently depending on their data
type or class, ability to redefine methods for derived classes.
40.What are the polymorphism types?
 static/compile-time
 dynamic
41.What is method overloading?
 Implements multiple methods within the same class that use the same name but
different set of parameters.
42.Describe method overloading criteria with examples.
 different number of parameters

float dimensions(int a,int b,int c){


//calculate with 3 dimensions
}

float dimensions(int a,int b){


//calculate with 2 dimensions
}

 different types of parameters

void displayInfo(String name){


}

void displayInfo(long payment){


}

 different order of parameters

void displayInfo(String name,long payment){


}

void displayInfo(long payment,String name){


}

13 | P a g e
43.What is method signature?
 Ability to write methods that have the same name but accept different parameters.
Compiler can separate the difference through this.
44.What is the different of dynamic polymorphism?
 In this compiler doesn’t allow to determine the executed method. It happens in run-
time.
45.Give example for dynamic polymorphism.
class Calculator{
int z;
public void addition(int x,int y){
z=x+y;
System.out.println(“super”+z);
}
public void subtraction(int x,int y){
z=x-y;
System.out.println(“super”+z);
}
}
public class MyCalculator extends Calculator{
public void multiplication(int x,int y){
z=x*y;
System.out.println(“sub”+z);
}

public void addition(int x,int y){


z=x+y;
System.out.println(“sub”+z);
}

public static void main(String args[]){


int a=20,b=10;
MyCalculator my1=new MyCalculator();//Object to access sub class
Calculator my2=new Calculator();//Object to access super class
my1.addition(a,b); //sub30
my1.subtraction(a,b); //super10
my1.multiplication(a,b); //sub200
my2.addition(a,b);//super30
}
}
14 | P a g e
1.5 Refactoring, Abstraction
46.What is refactoring?
 Restructuring code in a disciplined way.
47.What are the advantages of refactoring?
 Improve code readability.
 Reduce complexity
 Provide more expressive internal architecture.
48.What are the code smell compromise ways?
 Duplicated Code
 Long method
 Large class
 Liskov substitution principle
 Excessive use of literals
49.What is abstraction?
 Process of reducing the object to its essence. Only necessary characteristics are exposed
to users.
50.What are the Refactoring techniques?
 Encapsulate Field.
 Type generalization.
 Breaking code to logical pieces.
 Rename Method/Field.
 Extract class/method.
1.6 Error & Exception Handling, Debugging
51.What are the two main flows of code executions in java?
 Normal main sequential code execution-program doing what it meant to be accomplish.
 Exception handling code execution-prevent the continuation of the main program
because of error or interruption.
52.What are the errors in program?
 Syntax error-when the code doesn’t follow the syntax rules.
 Runtime error-errors take place in program running.
 Logic errors-error in the way of program works.

15 | P a g e
53.What is an exception?
 An event, which occurs during execution of the program, that disrupts the normal flow
of the program instructions.
54.Give example for exception handling.
int a=10;
int b=0;
try{
int div =a/b;
System.out.println(div);
}catch(ArithmaticException e){
System.out.println(“cannot devided by 0”);
}catch(Exception e){
System.out.println(“some other error occured”);
}finally{
System.out.println(“Good bye”);//always executed
}

55.Describe exception handling.


No exception is thrown:
 Code in the try block.
 Final block code will be executed.
 Code after try catch block.
Exception is thrown and match is found among the catch blocks:

 Code in try block until exception occurred


 Matched catch block is executed
 Final block code will be executed.
 Code after try catch block.
Exception is thrown & no match found:

 Code in try block until exception occurred


 Final block code will be executed.
 NO Code after try catch block.
16 | P a g e
56.Give examples for exceptions.

 ArithmaticException
 RuntimeException
 IOException
 FileNotFoundException
 StringIndexOutOfBoundsException

57.What is debugging?

 Process of detecting and removing of existing and potential errors.

For more about Java(Sinhala):


https://www.youtube.com/watch?v=q0RFGwMkpMI&list=PL495mke12zYANEM9p7JT5-
99Yx8Z7z_ib

17 | P a g e

You might also like