0% found this document useful (0 votes)
21 views19 pages

Java Thoery

The document is a quick revision sheet for Object-Oriented Programming concepts, covering definitions of classes, objects, and principles such as encapsulation, abstraction, polymorphism, and inheritance. It also includes explanations of various programming constructs, visibility modifiers, and examples of using ternary operators and increment operators in Java. Additionally, it provides practice questions and comparisons between different programming elements.

Uploaded by

advaitcpathak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views19 pages

Java Thoery

The document is a quick revision sheet for Object-Oriented Programming concepts, covering definitions of classes, objects, and principles such as encapsulation, abstraction, polymorphism, and inheritance. It also includes explanations of various programming constructs, visibility modifiers, and examples of using ternary operators and increment operators in Java. Additionally, it provides practice questions and comparisons between different programming elements.

Uploaded by

advaitcpathak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112

TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112


TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112

What is Class?

ANS.: User defined Datatype. Describes characteristics of an object.

What is Object?

ANS.: Particular instance of a class. It holds the characteristics of the Class.

Four principles of Object Oriented Programming?

ANS.: Encapsulation, Abstraction, Polymorphism & Inheritance

Encapsulation?

ANS.: Wrapping of data and function in class. Provides data hiding

Abstraction?

ANS.: Hide the complexity & showing the essential.

Polymorphism?

ANS.: Having many forms. Behaves differently in different situation.

Inheritance?

ANS.: One object acquires the properties of another object

Polymorphism: Overloading

ANS.: Overloading: Same method name with different arguments. Methods within Same class

Explain public static void main(String args[]) method

ANS.: public: Accessible to all

static: No instance of class needed to access the method

void: does not return a value

main(): Entry point for the application


TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
Difference between local variables and instance variables?

ANS.: Local Variable: Declared within a block of code like methods

Instance Variable: Defined at the class level

How to define a constant variable?

ANS.: Declared as static and final. E.g.: static final int PI = 2.14

Compare Application with Applet?

ANS.: Application: Stand Alone & execution starts with main ().

Applet: Run as part of web-browser.

Starts with init () method and executes paint() method

JVM?

ANS.: JVM converts source code to byte code not understandable for humans.

Packages in Java?

ANS.: Collection of similar classes and interfaces.

Built-in packages in java?

ANS.: lang, awt, javax, swing, net, io, util, sql etc.

Q. What is meant by token? Name the tokens available in Java.

The smallest individual unit in a program is called a token.

Keywords, identifiers, literals, operators and punctuators are tokens available in Java.

Q. What are keywords ? Can keywords be used as identifiers ?

Keywords are the words with special meaning associated with them. These are reserved for special purpose and must not be
used as normal identifier names. Some keywords of Java are default, return, if, private etc.No, keywords cannot be used as
identifiers.

Q. What is message passing among objects ?

When the objects need to interact with one another, they pass/request information to/from one-another. This interaction is
known as message passing. Objects interact with one another through messages.

Q. What is information hiding ?

Information hiding is the process of hiding all the secrets of an object that do not contribute to its essential characteristics. The
structure of object is hidden, the implementation of its methods. Only the essential characteristics of object are visible.

Q. What is a method ? Can there be objects without having any methods ?

The behaviour of an object is described through associated functions called methods.In Java, every class is a subclass of Object
class and so, all objects inherit the methods of the Object class. Thus, it is not possible for objects to be without any methods.

Q. What are actual and formal parameters of a function ?

The parameters that appear in the method call statement are called actual parameters.

The parameters that appear in the method definition are called formal parameters.
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
Q. What is the condition of using a function in an expression ?

The condition of using a function in an expression is that it must return some value. The return type of the function should not
be void.

Q. How can objects be initialized with desired values at the time of object creation ?

Objects can be initialized with desired values at the time of object creation by using parameterized constructor and passing the
desired values as arguments at the time of creation of object.

Q. What are wrapper classes?

Wrapper classes are specially designed classes that act as wrappers to primitive data types so that primitive values can be
accessed as objects. For example, Integer is a wrapper class for int data type and Float is a wrapper class for float data type.

Q. What is autoboxing ? What is auto-unboxing ?

The automatic conversion of primitive data type into an object of its equivalent wrapper class is known as Autoboxing.

The automatic conversion of an object of wrapper class into primitive data type is known as Auto-unboxing.

Q. Name various visibility modifiers in a class.

The various visibility modifiers in a class are as follows:

• public — The public members are accessible in all the classes whether a subclass or any other class in same package or
another package.

• private — The private members are accessible only inside their own class and nowhere else.

• protected — The protected members are accessible inside all the classes in their own package as well as in all
subclasses of their class.

• default — The default members are accessible inside all the classes of the same package.

Q. What is meant by index of an element ? How are indices numbered in JAVA ?

Each element in an array is referred to by their subscript or index. The index of an element refers to the position of an element
in the array. In Java, the indices start from 0 and go on till size - 1.

Q. Determine the number of bytes required to store an int array namely A[23].

An int data type requires 4 bytes in memory. Therefore, the storage space required by array A[ ] will be 23 x 4 = 92 bytes.

Q. How can you convert a numeric value enclosed in a string format ?

We can convert a numeric value enclosed in a string format into a numeric value by using valueOf() method.

For example, the given statement will return the numeric value of the String argument "15".

int num = Integer.valueOf("15");

Q. Write the return type of the following library functions :

1. isLetterOrDigit(char) 2.replace(char, char)

• boolean

• String
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
Q. What is an Infinite Loop

A sequence of Instruction which loops endlessly.

Q. What is null or empty statement

A for statement that does not include any statement inside the body. Eg for(i=1;i<=10;i++);

Q. What is method prototype or signature

It’s the first line of method access specifier return type, function name and its parameter.

Q. Explain Type Casting

Explicit Conversion of one datatype to another. Eg int x = (int)10.0;

Q. Define Impure function

Function which can change the state of an object are called impure function

Q. Name 2 jumping statement

Break and Continue

Q. Give the precedence of Logica Operator

! (Not) &&(And) and then ||(Or)

Q. What is new operator

It is use to create a new Object or new Array. Eg. Int a[] = new int[10]

Q. When function does return anything what is the keyword.

Void

Q. What is the need of autoboxing?

To pass a primitive data to a function that uses Wrapper object as function argument

Q. What is the need of unboxing

To pass the value of the wrapper object as function argument.

Q. Give uses of wrapper object.

To store primitive value to object. To provide conversion system from String to Primitive and Vice versa

Q. Give the scope of local variables.

Inside the curly brackets of a function

Q. Why do we use a null loop

To create a delay
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
while loop do-while loop

while is an entry-controlled loop. do-while is an exit-controlled loop.

while loop checks the test condition at the do-while loop checks the test condition at the
beginning of the loop. end of the loop.

Object Class

Object is a real world entity such as pen, laptop, Class is a blueprint or template from which
mobile, bed, keyboard, mouse, chair etc. objects are created.

Object is a physical entity. Class is a logical entity

Call by value Call by reference

Actual parameters are copied to formal Formal parameters refer to actual parameters.
parameters.

Any changes to formal parameters are not The changes to formal parameters are
reflected onto the actual parameters. reflected onto the actual parameters.

Parameterised constructor Non-parameterised constructor

Parameterised constructor receives Non-parameterised constructor does not accept


parameters and uses them to initialise parameters and initialises the object's member
the object's member variables. variables with default values.

For example, For example,


Test(int x, int y) Test()
{ {
a = x; a = 10;
b = y; b = 5;
} }
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
Private Protected

Limited to the same class. Accessible within the same class & subclasses

Not inherited by subclasses inherited by subclasses

Protected Public

Accessible within the same class & subclasses Accessible from any class or package

inherited by subclasses inherited by subclasses

Bubble Sort Selection Sort

Bubble Sort involves comparing adjacent Selection sort involves finding appropriate
elements and swapping them. element for every position and swapping it with
existing element at that position.

Its is stable algorithm It is unstable algorithm.

Its best case is O(n) Its best case is O(n2)

One-dimensional array Two-dimensional array

One-dimensional array stores data in a Two-dimensional array stores data in a grid or table,
single row or column. with rows and columns.

It uses one index to access array elements. It uses two indices to access array elements.

equals() compareTo()
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
This function only ckecks whether two This function checks whether a string is equal bigger
strings are identical or not. or smaller than other string or not.

It result in Boolean type value true/false. It result in an integer type value.

String StringBuffer

String type object has fixed length. StringBuffer type object can change the length.

It is a general approach of programming. It is an advanced approach of programming.

If-Else Statement Switch Case Statement

This statement is executed based on the Switch statements execute as per the user decision
condition inside the if-else statement

This statement is used to choose between This statement is used to choose among multiple
two options options.

Constructor Method

It is used to create and initialize an Object. Method is used to execute certain statements.

A constructor cannot have any return type. A method can have a return type.
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112

Ternary Operators:

1.Rewrite the following using ternary operator :


if(x%2 == 0)
c = ‘E’;
else
c = ‘O’;
Ans:
c = (x % 2 == 0) ? ‘E’ : ‘O’;
2.Rewrite the following statements using ternary operator.
if(ch> “C”)
value = 200;
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
else
value =100;
Ans.
value = (ch> “C”) ? 200 : 100 ;
3.Rewrite the following using ternary operator:
if (bill >10000 )
discount = bill * 10.0/100;
else
discount = bill * 5.0/100;
Ans.
discount=(bill >10000 ) ? bill * 10.0/100: bill * 5.0/100;
4.Rewrite the following statement using suitable ternary operator.
if(number > 0.0)
System.out.println(“positive”);
else
System.out.println(“not positive”);
Ans.
System.out.println( (number > 0.0) ? “positive” : “not positive”);
5.Rewrite the following statement using suitable ternary operator.(for greater number)
public class Test
{
public static void main(String[] args)
{
int x = 20;
int y = 10;
int z = (x > y) ? x : y;
System.out.println("Greatest number: " +z);
}}

1.Rewrite the following program segment using the if-else statements instead of the ternary operator.
String grade = (mark >= 90) ? “A” : (mark >= 80) ? “B” : “C”;
Ans.
String grade;
if(marks >= 90) {
grade = “A”;
} else if( marks >= 80 ) {
grade = “B”;
} else {
grade = “C”;
}
2.Rewrite the following program segment using the if ..else statement.
comm = (sale>15000)?sale*5/100:0;
Ans.
if (sale>15000)
comm=sale*5/100
else
comm=0;
3.Rewrite the following statement using suitable ‘if()’ statement.
int ans = res > 365 ? 180/3:90/ 3;
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
Ans:
int ans;
if( res > 365)
ans= 180/3;
else
ans= 90/ 3;

PRE INCREMENT & POST INCREMENT IN JAVA

• If p=5; find d=++p + 5;


d=++p + 5
=6+5=11
• If a=48; find a=a++ + ++a;
a=a++ + ++a
=48+50=98
• If m=12; then find n=m++ * 5 + --m;
n=m++ * 5 + --m
=12*5+(13-1)
=60+12
=72
• If c=2; then find d=++c + c++ + 4;
d=++c + c++ + 4
=3+3+4
=10
• If y=14; then find z=(++y * (y++ + 5));
z=(++y * (y++ + 5))
=(15*(15+5))
=(15*(20))
=(15*20)
=300
• If a=4,b=3; find c=a++ * 6 + ++b*5 + 10;
c=a++ * 6 + ++b*5 +10;
=4*6 + 4*5 + 10
=24 + 20 10
=54
• If a=8; find a-=++a + a++ + 4;
Ans: =-14
Practice

5*3 = ____ 5 / 2= _____ 12 / 3 = ____ 5.0 / 2 = ____

5 % 3 = ____ 10 % 2 = _____ 15 % 2 =____ 2 % 5 = ____

x= 10; x+=3; x = _____? y= 10; y-=4; y = _____?

x= 10; x*=2; x = _____? y= 10; y/=4; y = _____?

x= 10; ++x; x = _____? y= 10; --y; y = _____?

x= 10; x++; x = _____? y= 10; y--; y = _____?


TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
x= 5; y=10; z = x>y ? 50 :100; z= ____?

x= 5; y=10; z = x>y ? x :y; z= ____?

x= 50; y=10; z = x>y ? 50 :100; z= ____?

x= 50; y=10; z = x>y ? x :y ; z= ____?

true or false

x= 10; y=5; x>y = ________?

x= 10; y=5; x<y =_________?

x= 10; y=10; x==y =_________?

x= 10; y=5; x!=y =__________?

x= 10; y=5; x<=y =__________?

x= 10; y=5; x>=y =__________?

x= 5; y=5; x<=y =___________?

x= 5; y=5; x>=y =___________?

x= 5; y=10; x>=y && x > 0=____________?

x= 10; y=10; x==y && x< 0=____________?

x= 5; y=10; x>=y && x < 0=____________?

x= 5; y=10; x<=y && x < 0=____________?

x= 5; y=10; x>=y || x > 0=____________?

x= 10; y=10; x==y || x< 0=____________?

x= 5; y=10; x>=y || x < 0=____________?

x= 5; y=10; x<=y || x < 0=____________?

x= 10; y=++x; x = _____? ; y = _____?

x= 10; y=x++; x = _____? y = _____?

x= 10; y=--x; x = _____? y = _____?

x= 10; y=x--; x = _____? y = _____?

x=0; x++; ++x; x = _____?

x=5; x--; --x; x = _____?

x=5; x--; --x; x = _____?

x=5; y=++x + ++x; x = _____? y = _____?

x=5; y=--x + --x; x = _____? y = _____?

x=5; y=x++ + x++; x = _____? y = _____?

x=5; y=x-- + x--; x = _____? y = _____?


TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
x=5; y=++x + x++; x = _____? y = _____?

x=5; y=--x + --x; x = _____? y = _____?

x=1; y=2; z=(++y - x--) x = _____? y = _____? z = _____?

x=10; y=5; z=(++y + y++ + x--) x = _____? y = _____? z = _____?

x=10; y=5; z=(++y + ++y + y++ + x--) x = _____? y = _____? z = _____?

x=10; y=5; z=(++y + ++y + y++ + x--) x = _____? y = _____? z = _____?

x=10; y=5; z=(++y + y-- + y-- + x-- + --x) x = _____? y = _____? z = _____?
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
String Functions:

toString() Convert Wrapper classes like Integer, Float , Double or Long to String
parseInt() Convert String to Integer parseFloat() Convert String to Float
parseDouble() Convert String to Double
String s = “Computer” ; s.subsString(0) – returns “Compter” s.subsString(5) – returns “ter” s.subsString(2,5) – returns “mpu”

You might also like