OBJECT ORIENTED PROGRAMMING
1. What are the important Features of Object Oriented Programming? Name two OOP languages.
• It gives emphasis on the data items rather than functions.
• It divides the complex task into number of smaller objects.
• It simulates the real world in software.
• All objects are classified under specific classes.
C++ and Java are the examples of OOP languages.
2. What are the basic differences between OOP and POP?
Procedure Oriented Programming Object Oriented Programming
[Link] is given on functions or logical steps 1. Stress is given on the data items.
[Link] values can keep floating from one function to [Link] can be used as a bridge to have data flow
another. from one function to another.
3.E.g-BASIC,COBOL,FORTRAN and C. 3. E.g-C++,Java,Small Talk,Eiffel.
3. What are the basic elements of OOP or the Principles of OOP?
Objects, Classes, Data Abstraction, Encapsulation, Inheritance, Polymorphism, Dynamic Binding and
Data Hiding.
4. Define class and explain why class is called an Object Factory?
A class is defined as the collection of similar objects.E.g: A class of cell phone represents Samsung S4,
Samsung Galaxy Note,[Link]. Each class describes possibly an infinite set of individual [Link],it can be
called a factory of objects.E.g: a class of cell phone can have numerous objects as Nokia,Samsung,[Link].
5. Why is class referred as a user defined data type or a Composite data type.
The predefined data types like int,float,double are already available in Java which provide some preliminary
[Link], a user can create a data type and declare certain characteristics and behaviour within it
which is done by using a class. So, a class is referred as User Defined Data Type.
6. Why is an Object regarded as an Instance of a class?
Each individual unit that is encapsulated is created from a class. These individual units are called objects
which belongs to the class in which it is created Thus an object can be considered as the basic building blocks of
a program or an Instance of a class.
7. What is Encapsulation, Abstraction and Inheritance?
The binding and wrapping up the methods and the data together is called Encapsulation. It keeps
them safe from being misused or mishandled.E.g the activities of a TV set are merged or grouped
with the remote signals of a specific remote control.
Abstraction is a concept of hiding unnecessary details and representing only the very essential
features.E.g the details of the structure and circuit of a TV is hidden and only the essential
components like the power push button,volume,brightness,etc are available for use.
Inheritance is the process by which an object acquires the properties of another object.
E.g Bulldog is a part of the classification dog, which itself is a part of mammal class and this mammal
class is under the class [Link],Bulldog inherits the characteristics of dog, dog inherits the
characteristics of mammal and mammal inherits the characteristics of animal class.
8. Explain the term Polymorphism.
It means the same operation or function may behave differently on different classes.
E.g the operation move behaves differently when applied to a Microsoft Window than when applied
to a chess game.
9. What is meant by Dynamic Binding?
It is the process to link the function call statement with function signature at run time.
10. What is Data Hiding?
Data Hiding is a software development technique specifically used in OOP to hide internal object details (data
members).Data Hiding is also known as data encapsulation or information hiding.
11. What does the following mean?
Animal tiger = new Animal ()-It means that the object named tiger is being created of the class named Animal.
12. State the differences between class and object.
CLASS OBJECT
Set of similar objects. Real and unique entity having some characteristics
and behaviours.
The name of the class cannot be a keyword. It is created with the use of new keyword.
It is known as ‘Object Factory’. It is known as ‘Instance of a Class’
13. What are the advantages of Object Oriented Programming (OOP)?
• Extension of one class is possible through Inheritance.
• Easy to solve complex problems.
• Easy to modify and maintain software complexity.
• Secured programs can be generated using the concept of data hiding.
14. The variables describe the state and the functions describe the behaviour of objects.
15. Creating an object from a class is called Instantiation.
HISTORY AND DEVELOPMENT OF JAVA
1. Define Byte Code and JVM.
Java compiler translates the source code into an intermediate level language called Byte Code which is
independent of the machine on which the program is run.
JVM is an interpreter which translates the program line by line to its own machine language and runs it to give the
desired result.
2. What is a keyword or reserved words?
The reserved words of java which have a predefined meaning are called a keyword.E.g-int, float, break, public.
3. Name the types of Java programs. What are the basic features of Java?
The two types of Java programs are Java Application, Servlets, Packages and Java Applets.
The basic features of Java are as follows:
• It is a pure object oriented programming language.
• Java programs are both compiled and interpreted.
• It is a case sensitive language.
• Java programs are platform independent.
• It is a multithreaded language.
4. Distinguish between Interpreter and Compiler.
INTERPRETER COMPILER
Conversion of high-level language is performed line by Instructions are converted to machine level language
line to machine level language. at once.
Errors are listed after each step or instruction. All the errors encountered are listed together.
It is a slow process It is a fast process.
JVM(Java Virtual Machine) is the Interpreter of Java. javac is the compiler of Java.
5. Mention two different ways of expressing a comment in a program.
The two ways of commenting in a program are:
➢ //Text: It takes only one line of text. For multiple lines, the two slashes should be given in each line.
➢ /*text*/: This is used to make multiple lines comment lines.
[Link] is JAVA APIs?
Java API(Application Program Interface) is a collection of pre-compiled codes grouped into various functional
packages that can be used while creating programs in Java.
[Link],[Link],[Link],[Link].
7. Name the keyword which indicates that a function has no return type.
void is the required keyword.
8. Mention any two attributes for class declaration.
a) class keyword (b) Name of the class.
CONCEPTS OF DATA TYPES IN JAVA
1. Define variable (identifiers) and constant (literals) with an example.
A variable or an identifier is a named memory location which contains a dynamic value.E,g-int x,float s,t.
Literals are the constants in java whose value remains fixed throughout the program.E.g-14,a,”Computer’.
Instance variables: The variables that are defined in a class are called instance variable. The name suggests that all
the instances (objects )will have their own copy of these variables. These variables are visible to all the functions,
hence these can be accessed from anywhere in the class.
Argument variables: An argument is a value that is passed to a function as a run-time input. We use a variable to
retrieve and store the argument during the function call. This variable is called the argument variable or the formal
parameter.
Local variables: These are the variables that are used and declared within a method or a block. These variables are
called and are destroyed when the execution of a function ends.
2. State two kinds of data types.
The two kinds of data types available in Java are: Primitive and Non-Primitive.
3. Differentiate between Primitive data type and Non-Primitive data type.
PRIMITIVE DATA TYPE NON-PRIMITIVE DATA TYPE
Independent of any other data type Dependent on Primitive Data Type
It is also known as predefined data type. It is also called Reference/user defined data type.
E.g-int, [Link], short. E.g-classes, arrays, interface.
4. What are the ASCII codes of the following characters: A-Z ,a-z and 0-9.
The ASCII (American Standard Code for Information Interchange) of the following characters are given below:
A-Z=65-90 a-z=97-122 0-9=48-57
5. Define type conversion? Differentiate between Implicit conversion and Explicit Conversion.
The process of converting the various data type in a mixed expression into a single type is known as Type
Conversion.
IMPLICIT TYPE CONVERSION EXPLICIT TYPE CONVERSION
Conversion to higher data type takes place Conversion to the required data type takes place
automatically. with the user’s intervention.
It is also known as coercion. It is also called forced conversion or type casting.
6. Explain the term type casting with an example.
The process of converting one data type into another data type with user’s intervention is known as type
casting.
E.g: int x,y;
double r=(double)(x+y);
In the given example the result of (x+y) should be int implicitly but double provided by the user before
the expression (x+y) will cause the result forcibly to be converted to double data type.
[Link] are the types of casting shown below:
i) char c=(char)120 – Explicit conversion.
ii) int x=’t’ – Implicit Conversion.
8. State the difference between token and identifier.
TOKEN IDENTIFIER(Variable)
Each individual character in java program. A named memory location which contains a value.
Fundamental unit of java program. Does not contain a fixed value.
E.g-literals,identifiers,operators,seperators, E.g-int m, float s, double x.
assignments and punctuators.
9. State the difference between Boolean and character literal.
Boolean literal stores Boolean values that can be either true or false whereas character literals can be any
character enclosed within single quotes.
10. Explain instance variables .Give an example.
A variable declared within a class is an instance variable.
E.g: class Variable
{
int var; //instance variable
}
11. What is a Token?
The smallest individual unit in a program that is meaningful to the compiler is known as Token.
TOKEN
S
Keywords Constants Identifier Operators Punctuators
s
[Link] uses the 16 bits Unicode character set to represent the character data.
[Link] is an Escape Sequence?
Escape sequences are some non-graphic characters that cannot be typed directly from the keyboard.
An escape sequence is represented by a backslash (\) followed by one or more characters.
E.g:\a-audible bell \t-horizontal tab \n-new line.
14.
TYPE INITIAL VALUE
Byte 0 of byte type
Short 0 of short type
Int 0
Long 0L
Float 0.0f
Double 0.0d
Char null character i.e: ‘\u0000’
Boolean False
All reference types Null
15. Define static and dynamic initialisation with an example.
The process where we assign values to the variables at the time of declaration is known as Static initialisation.
E.g-int a=5;
The process of assigning values to the variables during the execution of a program is known as Dynamic
Initialisation.E.g: int c=a+b;
OPERATORS AND EXPRESSIONS IN JAVA
1. Define Operators. What are the different types of operators?
An operator is a symbol or token that is used to perform arithmetical or logical operations to give meaningful
results. The three types of operators are: Arithmetic, Logical and Relational.
2. Differentiate between Arithmetic Expression and Arithmetic Statement.
ARITHMETIC EXPRESSION ARITHMETIC STATEMENT
It may contain constants, variables and arithmetical It is a arithmetic expression assigned to a variable.
operators .
E.g:a+b,x+10,a*a-10. E.g: r = a+b,c=x+10.
3. Explain arithmetic, relational, logical and ternary operator with an example.
Arithmetic-It is used to perform arithmetic calculations in a program.E.g-+,-,/,*,%.
Relational-It is used to show the relationship among the operands. It compares the values of the variables and
results in terms of ‘True ‘or ‘False’.E.g- <,>,= =,!=,<=,>=.
Logical-It is used to perform logical operations which yields 1 or 0 depending upon the outcome of different
expressions.E.g-AND (&&), OR (||) and NOT(!).
Ternary-This operator deals with three operands. It is also called conditional assignment statement because the
value assigned to a variable depends upon a logical expression.
E.g- min=(x>y)?x:y;
In the above example the variable min will store the value of x if the expression x>y evaluates to true otherwise
min will store the value of y.
4. Differentiate between / and % operator.
/ OPERATOR %OPERATOR
Returns the integral part or the quotient. Returns the remainder.
E.g-22/5 gives 4. E.g-22/5 gives 2.
5. State the difference between = and == operator.
= OPERATOR == OPERATOR
It is an assignment operator It is a relational operator.
It is used for assigning values to a variable. It is used to check for equality of values.
E.g-int a=5,float c=20.9. if(a==b)
[Link](“a IS EQUAL TO b”);
6. Differentiate between postfix and prefix operator.
PREFIX POSTFIX
It is applied before the operand. It is applied after the operand.
It works on the principle ‘CHANGE BEFORE ACTION’. It works on the principle ‘CHANGE AFTER ACTION’.
E.g:++z. E.g:z++.
7. What is meant by Precedence and Associativity of Operators?
Operator Precedence are set of rules that determine the order in which the expressions are evaluated.
For e.g y=6+4/2 in which division is evaluated before addition giving the result as 8.
Associativity rules determine the grouping of operands and operators in an expression with more than one
operator of the same precedence..
8. Write the significance of . and new operator.
The dot(.) operator accesses instance members of an object or class members of a class.
The new operator is to create an object or a new [Link] is used to allocate the space created for the new object
9. Precedence of Operators:
Unary ----> Arithmetic---->Comparison/Relational---->Logical---->Assignment.
Higher Precedence Lower Precedence
10. What is a Compound statement?
A sequence of statements enclosed within a pair of braces is known as a Compound statement.
GENERAL PROGRAMMING AND DECISION MAKING
1. The following table gives the syntax and function of certain mathematical functions:
FUNCTION NAME FUNCTION EXAMPLE
[Link]( ) It is used to find the square root of double n=[Link](25.0);
a positive number. The value returned is 5.0
[Link]( ) It is used to find the maximum of int n=[Link](2,4);
two given arguments. The value returned is 4.0
[Link]( ) It is used to find the minimum of int n=[Link](3,6);
two given arguments. The value returned is 3.0.
[Link]( ) It is used to find the power raised to double n=[Link](2,3);
a specified number. The value returned is 8.0.
[Link]( ) It returns the value in rounded int n=[Link](8.25);
form. The value returned is 8.0.
[Link]( ) It also performs the rounding off. It double n=[Link](7.83);
returns a number down to the The value returned is 7.0.
nearest integer.
[Link]( ) It gives the rounded value to the double n=[Link](7.83);
next higher integer. The vale returned is 8.0.
[Link]( ) It returns a random number double n=[Link]();
between 0 and [Link] return type The value returned is between 0 and 1.
value is a fractional number.
2. Define package with an example? Write its advantages.
Package is a collection of classes and interfaces to perform various functions.
[Link],[Link],[Link].
➢ Packages group related classes, so that they can be managed effectively.
➢ We can declare multiple classes with the same name but in different packages.
➢ Encapsulation can be implemented by using packages.
3. Explain with an example the use of default in a switch statement.
The default section handles all the values that are not explicitly handled by one of the case section.
int c=[Link]();
case 1:[Link](“Number one”);
case 2:[Link](“”Number two”);
default:[Link](“Invalid option”);
The output of the above switch construct would be Number one for choice 1 and Number two for choice 2.
The output would be Invalid option for all other integral values.
4. State the significance of the keyword import.
The import keyword is used to inform the compiler to include a package, which is a collection of classes.
5. Distinguish between if and switch statement.
if statement switch statement
It is used to select among two alternatives. It is used to select among multiple alternatives.
It uses a Boolean expression to decide which A numeric, character or String data types can be used
alternative should be executed. to control the transfer in a switch statement.
6. What is the default initial value of a Boolean variable data type?
The default initial value of a Boolean data type is false.
7. Explain the different types of errors that take place during the execution of the program.
The different types of errors that occur during the execution of the program are as follows:
➢ Syntax Error: It occurs when the rules or the grammar of the programming language are violated.
E.g.-Missing semicolon, incorrect punctuations, incorrect word sequence or misuse of terms.
➢ Logical Error: A logical error is an error that occurs in planning the logic of the program.
E.g.: In a program to add two numbers if the user uses division or any other operator other than plus (+)
operator, than it is a logical error.
➢ Run-Time Error: The error which is generated after the compilation of the program that is at run time is known
as Run-Time Error.
E.g:Dividing a number by zero, finding the square root of a negative [Link] a=20/0;
8. Name the type of error in each case given below:
i) [Link](36-45) – Logical error as it is not possible to find the square root of a negative number.
ii) int a;b;c; - Syntax error as declaration of variable should be separated by comma not semi-colon.
9. Define Compound Statement with an example.
A compound statement can be defined as a block of statements that are a part of single programming statements
which are enclosed in a pair of parenthesis { }.
E.g-if statement: if(x==y)
{
[Link](“x and y are equal”);
}
10. Name the class used for different mathematical function.
[Link]
11. Explain with an example the if-else-if construct.
It is a logical situation when more than two actions are to be performed depending upon the conditions with a
keyword ‘ else if ‘.
E.g: if(condition 1)
Statement 1;
else if(condition 2)
Statement 2;
else
Statement 3;
If condition 1 is true then Statement 1 will be executed, else if condition 2 is true Statement 2 will be executed
and so on. If none of the conditions are true then else Statement will be executed.
12. Name a package and the class that is invoked by default.
A package that is invoked by default is [Link].
A class that is invoked by default is String.
13. Name a keyword to use classes defined in a package.
A keyword to use classes defined in a package is import.
14. What is dangling else or if-else ambiguity?
The nested if-else statement introduces a source of potential ambiguity referred to as dangling-else problem.
This problem arises when in a nested if statement, number of ifs is more than the number of else clauses.
E.g: if(expr1)
if(expr2)
Statement 1;
else
Statement 2;
Explanation: In the above statement else statement goes with the immediately preceding if statement that
does not already have an else statement. Here statement 2 is executed if expression 2 evalutes to false.
However if you have a code as follows:
if(expr1)
if(expr2)
statement 1;
else
statement 2;
else
statement 3;
The inner else goes with immediately preceding unmatched if which is the inner if. The outer else goes with
immediately preceding unmatched if which is now outer if as the inner if is unmatched. Thus statement 3 gets
executed if expr1 is false.
Note: If you want the else to go with the outer if (by default it will go with inner if).
One method of overriding the default dangling else matching is to place the last occurring unmatched if in a
compound statement as it is shown below:
if(expr1)
{
if(expr2)
statement 1;
}
else
statement 2;
Now the else clause matches with the outer if as intended.
15. What do you mean by Fall Through?
The fall of control to the following cases of matching case is called Fall Through.
16. Features of switch statement:
• A switch statement can only work for equality comparisons.
• No two case labels in the switch statement can have identical [Link] in case of nested switch
Statements the case constants of the inner and outer switch can contain common values.
• Switch statement works with integral types(byte,short,int, long)only.
ITERATION THROUGH LOOPS
1. State the similarity and difference between while loop and do-while loop.
Similarity: Both loops depend upon some control variable(s) and are generally used when number of iterations are
not known beforehand.
while loop do-while loop
The condition is checked before the statements within The condition is checked after the statements within
the block are executed. the block are executed.
It is also called entry controlled loop. It is also called exit controlled loop.
The statement within the block is not executed if the The statement within the block is executed is
condition is false. executed at least once even if the condition is false.
2. Explain loop with an example.
It is a construct that iterates or repeats over a range of values where the initial value and the final value is known.
E.g: for (int i=0; i<5; i++)
{
[Link](i);
}
Output:01234
3. Explain the meaning of break and continue statements.
A break statement branches the control to another part of the program.
E.g: for (int i =1 ; i<11;i++)
{
if(i>5)
break;
[Link](i);
}
Output: 12345
In the above example ,break transfers the control to statements outside the loop if i is greater than 5.
A continue statement stops the sequential execution of statements and resumes the update expression and
condition check in a looping statement.
E.g: for(int i=1;i<11;i++)
{
if (i==5)
continue;
[Link](i);
}
Output: 1234678910
4. Name two jump statements and their use.
Two jump statements are break and continue. Use – same as No. 3.
5. What is an infinite loop?
An infinite is a non-terminating loop or a loop that never ends. E.g: for(i=11;i>10;i++)
6. What is a null loop or empty loop? Write its significance.
A loop without any statements in its body is called an empty loop or null loop. It is used to delay the execution
of the program.
for(int i=2;i<=500;i++)
{
}
LIBRARY CLASSES
1. Define Library classes with an example.
The predefined classes of java which provide an effective support to the programmers in developing their program
logic is known as Library classes. They are also known as standard java classes or built-in java classes.
E.g: [Link],java,lang,[Link],[Link],java,applet,[Link].
2. What is a wrapper class? What is the use of Wrapper class? Give examples.
A collection of classes used to wrap variables of simple data type. It wraps the primitive data values in object.
The uses of Wrapper class are as follows:
➢ The primitive values can be stored in objects.
➢ It provides conversion systems from characters/string type to other primitive types.
E.g: Character,Byte,Short,Integer,Long,Float and Double.
3. Name a class that is used for different mathematical functions. Give an example of mathematical function.
A class that is used for different mathematical functions is [Link].
E.g:[Link],[Link],[Link]
4. What is an Exception? Name two Exception handling blocks.
An abnormal situation that arises during the execution of a program is called an Exception.
The two exception handling blocks are try and catch block.
5. What is the use of Exception handling in Java?
Exception handling is a process of handling an unwanted situation that arises during the run time of a source code.
The situation arises due to errors that violate java rules or the limitations brought about by java execution
environment.
6. What is the significance of the keyword throws?
A possibility may arise that a method is not able to handle an exception and an error may be generated. To avoid
the errors throws clause should be written.
7. What is the use of the clause throws IOException?
To allow main method to handle input output errors, the clause throws IOException must be written.
8. Name a keyword that distinguishes between instance variable and class variables.
static is a keyword that distinguishes between instance variable and class variables.
9. Write a statement to convert a string to numeric type value.
String n=”101”;
int n=[Link](n);
10. How are static data members different from ordinary data members of a class?
A static data member is different from ordinary data members of a class in various respects:
➢ There is only one copy of static data used by entire class which is shared by all the objects of that class.
➢ It is visible only within the class.
[Link] is a Stream?Explain.
A continuous flow of data is known as [Link] three IO streams are used in Java.
i) [Link] ii) [Link] and iii)[Link]
Streams can be one of the following two types:
a) Byte Oriented: In these streams there is no notion of data type or encoding scheme such as [Link]
we can read data of any data type with them as bytes are read.
These streams are known as data streams-DataInput streams and DataOutput streams.
b) Character Oriented: In these streams a character encoding scheme is used.
Theses streams are often referred to as Reader and Writer streams.
12. Name a package that is imported by default in Java.
[Link].
STRING MANIPULATION IN JAVA
1. Distinguish between String and StringBuffer.
STRING STRINGBUFFER
The objects of this class are of fixed length. It creates strings whose length can be changed.
Insertion of characters or sub-strings in the middle or Modifications are possible in the same string objects.
at the end of that string object is not possible.
It is a general approach of programming. It is an advanced approach of prpgramming.
2. Differentiate between == and equals () operator.
== operator equals() operator
It tests for equality between two expressions, It is used to check the equality between two strings.
constants or variables.
It has nothing to do with case sensitivity. It is a case sensitive operator.
3. Differentiate between toLowerCase() and toUpperCase() function.
toLowerCase() toUpperCase()
It returns the characters of the string in lower case It returns the characters of the string in upper case
letters. letters.
4. Differentiate between compareTo() and equals() method.
compareTo() equals()
It compares two strings and not only checks the It compares two strings but only checks whether they
equality of two strings but also checks whether a are identical or not.
string is bigger or smaller than the other or not.
It returns an integer type value. It returns a Boolean value.
5. What will be the output for the following code?
String x=new String (“computer”);
[Link]([Link]());
The output of the above code will be COMPUTER.
6. Give the prototype of a function search which receives a sentence sent and a character c and returns 1 or 0
int search(String sent,char c)
7. Write a statement to find the position of the last space in a string named str.
int position=[Link](“ ”);
8. Write the difference between length and length() function.
Length length()
It is an instance variable that all arrays have. It is a string function.
It contains the number of elements that an array It returns the number of characters in a String.
holds.
9. Give the prototype of a function check which receives a character ch and an integer n and returns true or
false.
boolean check(char ch,int n)
10. Write a statement to store a number 323 as a string.
String n=”323”;
11. If String x=”Computer”,String y=”Application”.What will the following functions return?
i) [Link]([Link](1,5));
ii) [Link]([Link]([Link](4)));
iii) [Link](y+[Link](5));
iv) [Link]([Link](y));
Answer:
i) ompu
ii) 4
iii) Applicationter
iv) false
12. Give the output of the following string functions:
i) “MISSISSIPPI”.indexOf(‘S’) + “MISSISSIPPI”.lastindexOf(‘I’);
ARRAYS
1. What is an array? What is the need of implementing the array concept in programming?
An array is a finite set of homogenous elements stored in contiguous memory locations.
A single variable can store only one value in its single memory locations. If it is attempted to store another value
the previous value gets overwritten by the new value. In programming situations where it is required to store more
than one values in the locations, the solution is to use more than one variable. But if it is required for very lengthy
list of values, using so many variables in the program is not advisable as it reduces the quality of the program. The
best solution is to use an array variable by which more than one memory locations can be accessed by the same
variable name and different subscripts.
2. What is the difference between subscript and a subscripted variable?
A subscripted variable is an array variable that provides access to more than one memory locations and their
elements.A subscript is an index or a number that specifies the data elements of an array being accessed.
3. What is the difference in the statements “Declaring an array “and “Accessing an array element”.
Declaring an array means specifying the data type of the elements that an array will contain and also specifying the
name of the array. For example, int a[].
Accessing an array element means to refer array elements by specifying the complete address of the location as:
array name followed by subscripts enclosed in square brackets. For example, a [2];
4. What is the difference in the two java statements given as:
int x[]; and x=new int[10]
int x[]:This statement just declares the array identifier as x which will contain data elements all of integer type.
x=new int[10]=This statement allocates 10 memory locations to the array x.
5. Write a statement to declare and create an integer array of 25 numbers stored in 5 rows and 5 columns.
int a[][]=new int[5][5];
6. What are the different operations that can be performed on an array?
The different operations which can be performed on an array are Searching,Sorting,Insertion,Deletion and
Merging.
7. Differentiate between Linear Search and Binary Search.
LINEAR SEARCH BINARY SEARCH
It takes places in an unarranged array of values. It takes place in an array arranged in ascending or
descending order.
The search begins at the start of an array that is from An array is divided into halves and the desired data
the 0th position. item is searched either in the first half or in the second
half.
It is a time consuming process if the size of array is This process takes less time as the number of
large. comparisons is reduced.
8. Name the search or sort that
i) Makes several passes through the array selecting the next smallest item in the array each time placing it
where it belongs in the array.
ii) At each stage compares the sought key values with the key value of the middle element of the array.
Ans:i) Selection Sort
ii) Binary Search
9. Write the advantages and disadvantages of an array.
Advantages:
i) Easy to Specify: The declaration, allocation of memory space, initialisation can all be done in one
line of code.
ii) Random access of elements: Arrays allows random access to any elements via its index number.
iii) Fast Sequential Access: It is usually faster to sequentially access elements due to contiguous
storage locations.
Disadvantages:
i) Need to know the size: To know the size at the time of allocation is necessary in arrays.
ii) Not suitable for situations demanding varying memory sizes: For situations wherein the memory
requirements are not known beforehand arrays are not suitable.
10. State conditions under which Binary Search is applicable.
i) The list must be sorted.
ii) The lower bound the upper bound and the sort order of the list must be known.
SCANNER AND PRINTER CLASSES
1. What are the advantages of using Scanner class?
The advantages of using Scanner classes are as follows:
➢ InputStreamReader and BufferedReader are not required to be mentioned.
➢ Program logic is simple.
➢ String manipulation is quite easy as each word can be obtained as a token and handled separately.
➢ It provides an easier approach to read records from a data file.
METHODS RETURNS
int nextInt() Returns the next token as an int.
long nextLong() Returns the next token as a long.
float nextFloat() Returns the next token as a float.
double nextDouble() Returns the next token as a double data type value.
String next() Finds and returns the next complete token from the
scanner object as a string.
String nextLine() Returns the rest past of the current line, excluding
any line separator at the end
void close() Closes the Scanner.
METHODS RETURNS
boolean hasNextInt() Returns true if the next token in the scanner object
can be interpreted as an int value.
boolean hasNextLong() Returns true if the next token in the scanner object
can be interpreted as a long value.
boolean hasNextFloat() Returns true if the next token in the scanner object
can be interpreted as a float value.
boolean hasNextDouble() Returns true if the next token in the scanner object
can be interpreted as a double value.
boolean hasNext() Returns true if the scanner object has another token
in its input,false otherwise.
boolean hasNextLine() Returns true if the scanner object has another line in
its input ,false otherwise.
2. Write the significance of using PrintWriter class .
PrintWriter is a very useful class using which we can write on the terminal [Link] use PrintWriter class an
object has to be created as shown below:
PrintWriter pw=new PrintWriter([Link]);
Functions of PrintWriter class:
i) print() : The function print() prints the statement.
ii) println(): The function println() places the cursor on the next line after printing the statement.
3. What is meant by IndexOutofBoundsException and name the Exception family it belongs to?
IndexOutofBoundsException occurs when an array or string index exceeds the size of the array/string.
It belongs to RunTimeException family.
FUNCTIONS AND METHODS
1. What are the two ways of invoking functions?
The two ways of invoking functions are Call by value and Call by Reference.
2. Define a Function? What is meant by function prototype?
A function is defined as a block of statements that performs a specific task. A function prototype is the first line of
a function definition which includes access specifier, return type, method name and the list of parameters.
E.g: public float Fraction(float a, float b)
3. Differentiate between Pure and Impure Functions.
PURE FUNCTION IMPURE FUNCTION
It returns a value to its caller module. It may not return a value to its caller module.
It doesn’t change the state of an object. It changes the state of an object.
4. What is Function Signature?
The function name and the parameter list from the prototype of the function forms the function signature.
E.g: Sum (int a, int b)
5. Explain the function of return statement in Java programming? How many values can a function return?
The return statement in Java programming is used to send back the value or the result from a method to the caller
program.
A function can return only one value.
6. Differentiate between Formal parameters and Actual parameters.
FORMAL PARAMETER ACTUAL PARAMETER
It is described in the function definition. It is described in the function statement.
7. Which OOP principle implements function overloading?
Polymorphism.
8. What is the use of this keyword?
A function of a class is invoked by its object. Now, there may be more than one objects of a single class and each
object may invoke the same function of the class. For a function to differentiate its working for both objects, this
keyword is required. this refers to the current object that invoked the function.
9. How is the following data type passed to a function?
➢ Primitive types
➢ Reference types
Primitive types are passed by value and reference types are passed by reference.
8. What is the role of keyword void in declaring function?
The significance of the keyword void when used in declaring the return type of the function denotes that the
function is not returning any value.
9. Write two advantages of using functions in a program.
The two advantages of using functions in a program are:
➢ Functions enable reuse of code across multiple programs.
➢ Functions reduce the usage of duplicate code in a program. If a block of program is needed several times in a
program, it can be written only once and be called as a function at various areas of the program.
10. If a function contains several return statements how many of them will be executed?
Only one return statement will be executed since an immediate exit from the function is caused as soon as the
return statement is encountered.
CLASS AS A USER DEFINED DATA TYPE
1. Differentiate between user defined data type and primitive data type.
USER DEFINED DATA TYPE PRIMITIVE DATA TYPE
It is created by the user. It is a predefined data type.
These data types are available in accordance to their These data types are available within the program.
visibility modes throughout the program.
2. What is a static data member?
A static data member is an instance variable which is global in the class and used commonly by all the objects of
that class type.
3. Mention any two attributes required for class declaration.
The two attributes required for a class declaration is <Access Specifier> and <Class Name>
4. Explain the three types of access specifiers.
➢ private-The data members or member methods declared as private can be used only within the scope of a class.
These members are not accessed outside the class.
➢ public-The class members declared as public can be used even outside the visibility of a class.
➢ protected-The protected members are used in the class as private members which can only be applied to
another class during inheritance OR The protected members are accessible by all the classes that exist in the
same [Link] subclasses of that class can also access the protected members even if they lie outside the
program.
5. Enlist all primitive data types, the space occupied by them and their smallest and largest values.
➢ byte-storage space is 1 byte ,smallest value is -128 and largest value is 127
➢ short-storage space is 2 bytes, smallest value is -215 and largest value is 215 – 1.
➢ int-storage space is 4 bytes, smallest value is -231 and the largest value is 231 – 1.
➢ long-storage space is 8 bytes, smallest value is -263 - 1.
➢ char –storage space is 2 bytes.
➢ boolean-storage space is 1 byte.
[Link] is the significance of default accesss specifier?
The members with a default access specifier can be accessed by all the subclasses and non subclasses that are in
same program but cannot be accessed by a class or subclass lying outside the program.
CONSTRUCTORS
1. What is a constructor?
A constructor is special member function with a name same as that of the class name used to initialise the instant
variables of the objects.
2. What are the features of a constructor?
➢ Constructor is defined with the same name as class name.
➢ Constructor is automatically called while creating an object.
➢ Constructor doesn’t have a return type.
➢ Constructor is always public.
➢ Constructor is overloaded automatically.
➢ Constructor is only created to initialise the data members/instant variables.
3. State the difference between constructor and method.
CONSTRUCTOR METHOD/FUNCTION
Constructor has the name same as the class name. Function name cannot be same as the class name.
It doesn’t return any value since it doesn’t have a Function may or may not return a value.
return type.
Constructors are usually public. Method may be public or private.
Constructor doesn’t require to be called from the Method is called from the main class.
main class.
4. Explain the different types of constructors.
The different types of constructors are as follows:
➢ Default constructor or Non-Parameterised constructor-A default constructor doesn’t accept any parameter’s
values from the object when it gets created.
➢ Parameterised constructor-These constructors have arguments. The values to these arguments are passed
from the object when it is created.
➢ Copy constructor-It is a constructor with one parameter of the same type as the class.
5. What is constructor overloading?
The process of defining more than one constructor for a class where they differ in the parameter types or number
of parameters passed to the constructor is called constructor overloading.
6. Which unit of class gets called when the object of the class is created?
Constructor.
7. When is a constructor called?
A constructor is called during object creation.
ENCAPSULATION AND INHERITANCE
1. What is Encapsulation?
Encapsulation is the process of wrapping of data and functions together as a unit.
2. What is Inheritance?
Inheritance is the process by which an object acquires the properties of another object.
3. What is meant by Base class and Derived class?
A base class is a class which is inherited by another class whereas a derived class is one which inherits a base class.
FLOWERS Base class
ROSE TULIP
Derived class
4. What are the different types of Inheritance? Explain.
i) Single inheritance: When a class is derived from one base class.
Base
Subclass
ii) Multiple Inheritance: When a subclass is inherited from multiple base classes.
Base 1 Base 2
Subclass
iii) Hierarchical Inheritance: When many subclasses are inherited from single base class.
Base
Subclass 1 Subclass 2 Subclass 3
iv) Multilevel Inheritance: When a subclass is inherited from a class that itself is inherited from another class.
It exhibits the transitive nature of inheritance.
Base
Subclass
Subclass
v) Hybrid Inheritance: It is the combination of multiple and hierarchical inheritance.
Base
Subclass/Base Subclass/Base
Subclass
5. What is the significance of Inheritance?
➢ Inheritance divides program into economical, useful and reusable set of classes.
➢ Changes made in the original class are reflected in all the inherited classes.
➢ The transitive nature of inheritance ensures the closeness with the real world objects.
➢ Using inheritance we can add new features to an existing class and modify the features in a class.
OPERAION ON FILES
1. What is a Stream? What are the two types of stream classes in Java?
A stream is a continuous flow of data. There is two types of stream classes in Java:
i) Bytes stream classes used for Byte oriented IO.
ii) Character stream classes used for Character oriented IO.
The Byte stream classes can be categorised into: Input Stream classes and Output Stream classes.
The Character stream classes can be categorised into: Reader classes and Writer classes.
The operations that can be performed on Files are:
File Input – Obtaining input from a file.
File Output – Writing output to a file.
2. What do you mean by Buffer? Expain the different types of Buffer.
A Buffer is temporary storage used to hold data until enough has been collected that is worth
transferring.
The two types of Buffer are:
i) Input Buffer: An input buffer is used for reading a large chunk of data from a stream.
ii) Output Buffer: An output buffer is used to store up data to be written to a stream.
3. What is the difference between text files and binary files?
In text files data is stored as per specific character encoding scheme.E.g-ASCII text or Unicode text is
stored.
In binary files data are stored in the form of bytes that are machine readable form.
4. Which streams are used for performing IO in text files?
FileReader,FileWriter,BufferedReader,BufferedWriter,PrintWriter.
THE END