Theory Question ICSE - SET I
Theory Question ICSE - SET I
1. What is a bytecode?
Ans: The Java development kit comes with a collection of tools that are used
for developing and running java programs.
Ans: 1. Write Once Run Anywhere 2. Light weight code 3. Security 4. Built in
Graphics 5. Object Oriented Language 6. Support Multimedia 7. Platform
Independent. 8. Open Product.
1. How you create, compile and execute a program in Java or BlueJ? Explain
your answer?
Ans: Create: Click on new class button from BlueJ editor, then type the class
name a program icon will be created. double click on it, a program editor will
be open, erase the code and type your program coding. Compile: click the
compile button on the left of the window or right click on the class icon and
select compile from the menu options. Execute: Right click on the class icon
and select new class name option. A dialogue box appears type the name of
the object. A object icon will be created at the bottom. Right click on the object
icon and select the method we want to execute.
Ans: The two types of Java Applications are ‘Internet Applets’ and ‘Stand
alone application’.
1. State the distinguishing features of Java and C++?
Ans: The compiler can only translate a program if the program is syntactically
correct; otherwise the compilation fails and you will not be able to run your
program. Syntax refers to the structure of your program and the rules about
that structure.
The second type of error is a run-time error, so-called because the error does
not appear until you run the program. In Java, run-time errors occur when the
interpreter is running the byte code and something goes wrong.
1. iv) java is case sensitive language, i.e. it distinguished upper and lower case
letters.
2. Differentiate between Compiler and Interpreter.
Ans: Java compiler converts Java source code to byte code. This byte code is
further converted into machine code to make it applicable for the specific
platform by using interpreter.
Ans: Source code is the program developed in Java Language, which is input
to a computer through the keyboard. Compiler converts source code to byte
code for interpretation.
REPORT THIS AD
OR
Ans: The Object Oriented Programming Paradigm is the latest in the software
development and the most adopted one in the programming development.
The Paradigm means organising principle of a program. It is an approach to
programming. The concepts of OOP’s are: (1) Data Abstraction (2) Data
Encapsulation (3) Modularity (4) Inheritance (5) Polymorphism.
Ans: (1) Elimination of redundant coding system and usage of existing classes
through inheritance. (2) Program can be developed by sharing existing
modules. (3) Possibilities of multiple instance of an objects without any
interference. (4) Security of data values from other segment of the program
through data hiding.
Ans: Classes in Java are needed to represent real-world entities, which have
data type properties. Classes provide convenient methods for packing
together a group of logical related data items and functions that work on them.
In java the data items are called fields & the functions are called methods.
1. What is an abstraction?
Ans: It mean the ability to take more than one form. For example, an
operation, many types of data used in the operation.
1. What is Data hiding?
Ans: Data Hiding means restricting the accessibility of data associated with an
object in such a way that it can be used only through the member methods of
the object.
Ans: It is possible to define a class within another class, such classes are
known as nested classes. A nested class has access to the members
including private members of the class in which it is nested. However the
enclosing class not have access to the members of the nested class.
Ans: BASE CLASS – A class from which another class inherits (Also called
SUPER CLASS)
Ans: Keywords are the words that convey a special meaning to the language
compiler. No, keywords can never be used as identifiers.
Ans: Identifiers are names given to different parts of a program e.g. variables,
functions, classes etc. The identifiers in Java.
Ans: Keywords are predefine sets of words that have a special meaning for
the Java compiler. Identifiers on the other hand are created by Java
programmers in order to give names to variables, function, classes etc.
1. What are literals? How many types of integer literals are available in Java?
Ans: Integer constants are whole numbers without any decimal part. The rule
for forming an integer constants is: An integer constant must have at least one
digit and cannot contain a decimal point. It may contains + or – sign. A
number with no sign is interpreted to be positive.
1. What do you mean by Escape sequence and name few escape sequences in
Java?
1. How many integer constants are allowed in Java? How are they written?
REPORT THIS AD
Ans: Java allows three types of integer constants: Octal (base 8), Decimal
(base 10), and Hexadecimal (base 16). An Octal integer must be started with
a zero ‘0’, a Hexadecimal integer starts with a ‘0X’, all others are treated as
decimal integer constant.
1. What is meant by a floating constant in Java? How many ways can a floating
constant be represented into?
Ans: Floating constants are real numbers. A floating constant can either be a
fractional or in exponent form.
Ans: Integer constants are the whole numbers (without decimal points). e.g.
1231. Floating point constants are fractional numbers (number with decimal
points). e.g. 14.2356
1. Write the following real constants into fractional form: 0.113E04, 0.417E-04,
0.4E-05, 0.123E02
Ans: Primitive data types are those that are not composed of other data types.
Numeric Integral, Fractional, character and boolean are different primitive data
types.
Ans: The primitive data types are: byte, short, int, long, float, double, char and
Boolean. The non-primitive/reference data types are: class, array and
interface.
1. How many bytes occupied by the following data types: byte, short, int, long,
float, double, char, boolean.
Ans: char-2 byte, byte-1 byte, short-2 bytes, int-4 bytes, long-8 bytes, float-4
bytes, double-8 bytes, boolean-Java reserve 8 bits but only use 1 bit.
1. What is the range of the following data types: byte, short, int, long, float,
double, char, boolean.
REPORT THIS AD
REPORT THIS AD
1. What is the largest and smallest value for floating point primitive data types
float?
Ans: The smallest value is -3.4E+38 and largest values is 3.4E+38 of floating
point data type.
Ans: Operators are special symbols that represent operations that can be
carried out on variables, constants or expressions.
1. What do you mean by operator and write the name of all operators given in
your textbook.
Ans: The operations are represented by operators and the object of the
operations are referred to as operands. The types of Operators available in
Java are: 1. Arithmetic 2. Increment/Decrement 3. Relational 4. Logical 5.
Shift 6. Bitwise 7. Assignment 8. Conditional 9. [] operator 10. new operator
11. (type) cast Operator 12. () operator. 13. dot operator.
Ans: The operators that acts on one operand are referred to as Unary
Operator. There are two Unary operators Unary + operator and Unary –
operator. The operators that acts upon two operands are referred to as Binary
Operator. The Binary Operators are Addition(+), Subtraction (-), Multiplication
(*), Division (/) and Modulus (%).
1. What is increment operator? What are postfix and prefix increment operators?
Ans: The ‘++’ operator is called increment operator. The increment operators
add 1 to its operand. These are two types (i) Prefix and (ii) Postfix The prefix
version comes before the operand for e.g. ++a, where as postfix comes after
the operand e.g. a++
1. Find the value of x after evaluating x += x++ + –x + 4 where x=3 before the
evaluation. Explain your answer.
Ans: Result is 13, because x++ is 3, –x is 2 + 4 the answer is 9 add this with x
that is 3 it becomes 12 and due to pre increment of x++ the result becomes
13.
Ans: The logical operators combine the result of or more then two
expressions. The mode of connecting relationship in these expressions refers
as logical and the expressions are called logical expression. The logical
expression returns 1 if the result is true otherwise 0 returns. The logical
operators provided by Java are && Logical AND, || Logical OR, ! Logical NOT.
1. What do you man by Assignment Statement or Assignment Operator?
Ans: Assignment operator is represent by symbol ‘=’. It takes the value on the
right and stores it in the variable on the left side. for example x = y + 30
Ans: A Shift operators performs bit manipulation on data by shifting the bits of
its first operand right to left. The shift operators available in Java are:
REPORT THIS AD
Ans: Shift LEFT (<<) operatr shifts the bit pattern of the operand towards left
by defined number of bits. Shift RIGHT (>>) operator shifts the bit pattern of
the operand towards right by defined number of bits.
e.g. 13>>2 is 3
Ans: We can use new operator to create a new objects or new array.
x = 5 + 4 *6;
1. What is operands?
Ans: The memory variables/locations whose values can not be changed within
the program is called constants. The keyword final makes a variable as
constants.
1. Write down the equivalent expression for the mathematical expression (a)
(cos x/tan-1 x)+x (b) |ex – x|
1. What is the difference between these two function Math.ceil() and Math.rint(),
explain with example.
Ans: Math.ceil() this function returns the smallest whole number greater then
or equal to the given number. e.g. Math.ceil(12.85) gives output 13
and Math.ceil(12.35) also gives output 13. Where as the Math.rint() returns
the roundup nearest integer value. e.g. Math.rint(12.85) gives output 13
but Math.rint(12.35) gives output 12.
Ans: The process of converting one predefined type into another is called
Type Conversion.
1. What do you mean by type casting? What is the type cast operator? [2007]
The print() method sends information into a buffer. This buffer is not flushed
until a new line (or end-of-line) character is sent. As a result print() method
prints output on one line.
The println() method by contrast takes the information provided and displays it
on a line followed by a line feed.
REPORT THIS AD
Ans: The operations are represented by operators and the object of the
operations are referred to as operands. The expression is any valid
combination of operators, constant and variables.
for(int i=1;i<=5;i++)
System.out.println(“Hello”);
System.out.println(“How”);
System.out.println(“are you?”);
}
Use of Constructor
1. What is constructor?
Example:
class ant
int i;
the line new ant() creates an object and calls the default constructor, without it
we have no method to call to build our objects. once you create a constructor
with argument the default constructor becomes hidden.
Example:
int per;
int tot;
per=percentage;
tot=0;
int roll;
float marks;
roll=r;
marks=m;
roll=r;
marks=0;
{
roll=0;
marks=0;
(i) Constructors should be declared in the public section of the class. (ii) They
are invoked automatically when an object of the class is created. (iii) They do
not have any return type and cannot return any values. (iv) Like any other
function, they can accept arguments. (v) A class can have more than one
constructor. (vi) Default constructor do not accept parameters. (vii) If no
constructor is present in the class the compiler provides a default constructor.
Ans: The function has a return type like int. but the constructor has no return
type. The function must be called in programs where as constructor
automatically called when the object of that class is created.
1. Enter any two variables through constructor parameters and write a program
to swap and print the values. [2005]
class swap
{
int a,b;
swap(int x,int y)
a=x;
b=y;
int t=a;
a=b;
b=t;
class xyz
int a.b;
xyz(int x,int z)
a=x;
b=y;
xyz(xyz p)
a=p.x;
b=p.y;
Ans: The function prototype is the first line of the function definition that tells
the program about the type of the value returned by the function and the
number and types of arguments.
Ans: void data type specifies an empty set of values and it is used as the
return type for functions that do not return a value. Thus a function that does
not return a value is declared as follows. void <functions name> (parameter
list)
Ans: The main() function is invoked in the system by default. hence as soon
as the command for execution of the program is used, control directly reaches
the main() function.
Ans: The function prototype is the first line of the function definitions, that tells
the program about the type of the value returned by the function and the
number and type of the arguments. Function signature basically refers to the
number and types of the arguments, it is the part of the prototype.
Ans: The return statement is useful in two ways. First an immediately exit from
the function is caused as soon as a return statement is encountered and the
control back to the main caller. Second use of return statement is that it is
used a value to the calling code.
Ans: (i) functions lessen the complexity of programs (ii) functions hide the
implementation details (iii) functions enhance reusability of code
Ans: The parameter that appears in function call statement are called actual
argument and The parameter that appears in function definition are called
formal parameter.
1. What are static members?
Ans: The members that are declared static is called static members. These
members are associated with the class itself rather then individual objects, the
static members and static methods are often referred to as class variables
and methods.
Ans: (i) They can only call other static methods. (ii) They can only access
static data. (iii) They can not refer to this or super in any way.
Ans: (i) In call by value, the called functions creates its own work copy for the
passed parameters and copies the passed values in it. Any changes that take
place remain in the work copy and the original data remains intact.
Ans: In passed by reference, the called function receives the reference to the
passed parameters and through this reference, it access the original data. Any
changes that take place are reflected in the original data.
Ans: In call by value, the called functions creates its own work copy for the
passed parameters and copies the passed values in it. Any changes that take
place remain in the work copy and the original data remains intact. In call by
reference, the called function receives the reference to the passed parameters
and through this reference, it access the original data. Any changes that take
place are reflected in the original data.
1. Define an impure functions? [2006]
Ans: Impure Function change the state of the object arguments they have
received and then return. The following functions is the example of an impure
function:
time.seconds+=secs;
return(Time);
Ans: Pure Function: These functions takes objects as an arguments but does
not modify the state of the objects. The result of the pure function is the return
value. Impure Function: These functions change the state of the object
arguments they have received.
Ans: A Function name having several definitions in the same scope that are
differentiable by the number or type of their arguments, is said to be an
overloaded function. Function overloading not only implements polymorphism
but also reduce the number of comparisons in a program and there by makes
the programs run faster.
Ans:- A function name having several definitions that are differentiable by the
numbers or types of their arguments is known as function overloading. For
example following code overloads a function area to computer areas of circle
rectangle and triangle.
return (length*breadth);
float area (float side1, float side2, float side3) //area of triangle
{
float s = (side1 + side2 + side3)/2;
return (ar);
Ans: The this keyword is used to refer to currently calling objects. The
member functions of every objects have access to a sort of magic keyword
name this, which points to the object itself. Thus any member function can find
out the address of the object of which it is a member. The this keyword
represents an object that invokes a member function. it stores the address of
the object that invoking a member function and it is an implicit argument to the
member function being invoked. The this keyword is useful in returning the
object of which the function is a member.
Ans: When a method is called inside its own definition the process is known
as functions recursion and this function called recursive function.
Ans: The major difference between methods and functions is that methods
called by the reference variables called objects where as the functions do not
having any reference variables.
Ans: Data types are means to identify the type of data and associated
operations of handling it.
1. What is composite (user define) data type? Explain with an example? [2007]
[2009]
class Date
public Date()
dd=1;
mm=1;
yy=2005;
Ans: A user defined datatype is a data type that is not a part of the language
and is created by a programmer.
Ans: Yes, we can refer to a class not having a main() method as user-defined
datatype.
Ans: (i) primitive datatypes are built-in datatypes. Java provides these
datatypes. User-defined datatypes are created by users. (ii) The size of
primitive datatypes are fixed. The size of user-defined datatypes are variable.
(iii) Primitive datatypes are available in all parts of Java programs. The
availability of user-defined datatypes depends upon their scope.
Ans: In Java, all functionality is enclosed in classes. But in order for a class to
be user-defined datatype, it should be act different from that of an application.
i.e. it should not include main() method in it. Although we can create instance
of classes containing main method, they should not be referred to as used-
defined datatype. Such classes (containing main() method) are more
analogues to application than a datatype.
(d) Default (friendly) access: members with default (friendly) access can be
used within the package where the class is defined.
Ans: Private members of a class are accessible in the member function of the
class only, where as public members are accessible globally.
1. How are protected members different from public and private members of a
class.
Ans: Protected members of a class are accessible in all the classes in the
same package and subclass in the other packages. private members of a
class accessible in the member functions in the class only. Where as public
members are accessible globally.
Decision Making
1. What is a statement?
Ans: Statements are the instructions given t the computer to perform any kind
of action, as data movements, making decision or repeating action.
Statements form the smallest executable unit and terminated with semi-colon.
Ans: The three constructs that governs statement flow are: Sequence,
Selection and Iteration constructs.
Ans: A selection statement is the one that is used to decide which statement
should be execute next. This decision is based upon a test condition. The
selection statements provided by Java are: if-else and switch. The conditional
operator ?: can also be used to take simple decision.
if(expression) statement
if(marks>=80)
System.out.println(“Grade A”);
Ans: It is the test condition of an if statement that decides whether the code
associated with the if part or the one associated with the else part should be
executed. The former is executed if the test condition evaluates to true and
the latter works if the condition evaluates to false.
OR
Ans: A nested ‘if’ is an statement that has another ‘if’ in its body or in it’s
appearance. It takes the following general form.
if(ch>=’A’)
if(ch<=’Z’)
++upcase;
else
++other;
1. What is the problem of dangling-else? When does it arise? What is the default
dangling-else matching and how it be overridden?
if(ch>=’A’)
if(ch<=’Z’)
++upcase;
else
++other;
The indentation in the above code fragment indicates that programmer wants
the else to be with the outer if. However Java matches an else with the
preceding unmatched if. One method for over-riding the default dangling-else
matching is to place the last occurring unmatched if in a compound statement,
as it is shown below.
if(ch>=’A’)
if(ch<=’Z’)
++upcase;
else
++other;
Ans: (i) Compare to IF sequence, ?: offer more concise, clean and compact
code, but it is less obvious as compared to IF. (ii) Another difference is that
the conditional operator ?: produces an expression, and hence a single value
can be assigned, for larger expression If is more flexible. (iii) When ?: operator
is used in its nested form, it becomes complex and difficult to understand.
The expression is evaluated and its values are matched against the value of
the constants specified in the case statements. When a match is found, the
statements sequence associated with that case is executed until the break
statement or the end of switch statement is reached.
Ans: A control variable in switch case is one which guides the control to jump
on a specified case. e.g. switch(x), here ‘x’ is the control variable.
Ans: The switch statement is more advantageous then the if statement when
the test expression whose data type is either of byte, short, character, integer
or long is to be tested against a set of constants. The reason being that the
switch statement evaluates the expression once whereas the equivalent if
statement evaluates the expression repeatedly.
1. Explain, with the help of an example, the purpose of default in a switch
statement. [2005]
Ans: The default section is an optional part of the switch statement and the
statement written under default clause are executed when no matching case
is found.
switch(n)
Utilization of loops. Fixed number of Iteration. The for Loop, unknown number
of Iteration – while, do-while loop, continue, break. Nested Loops.
1. What is the difference between entry controlled and exit controlled loop? or
Ans: while loop is known as entry controlled loop and do-while loop is known
as exit-controlled loop. The differences between these two loops are: (1) In
while loop the test expression is evaluated at the beginning where as in do-
while loop test expression is evaluated at the bottom, after the body of the
loop. (2) In while loop if the test expression is false loop does not continued
but in do-while what ever the test expression the loop execute at least once.
1. Explain the difference between break and continue with an example. [2005]
[2008]
int i=0;
while(i<=10)
i++;
if(i==5)
break;
System.out.println(i);
}
e.g. of Continue Statement
int i=0;
while(i<=10)
i++;
if(i==5)
continue;
System.out.println(i);
Ans: (i) The for loop should be preferred if number of iteration is known
beforehand. (ii) The while loop should be preferred if the number iteration is
dependent upon some control variable. (iii) The do-while loop should be
preferred if the number of iterations is dependent upon user response.
Ans: In Java the ‘for’ statement is the most common iterative statement. the
general syntax of the for loop is,
for(int i=1;i<=10;i++)
System.out.println(i);
1. State one similarity and one difference between while and do-while loop.
[2005]
OR
eg: for(;;)
System.out.println(“java”);
Ans: Fixed type of iterative loop is created when the process is to be repeated
for defined number oft imes. variable iterative loop repeats the process till a
given condition is true.
Ans: A Null loop does not contains any statement to repeat where as infinite
loop repeats execution of the statements for endless iterations.
Ans: A null loop is also called delay loop which does not repeat the execution
of any statement but keeps the control engaged until the iterations are
completed.
1. What is the difference between byte oriented IO and character oriented IO?
How are these two performed in Java?
OR
Ans: Bye oriented IO reads bytes of data or binary where there is no notation
of datatypes. Character oriented IO on the other hand performs IO which is
specially character oriented. In Java byte oriented IO is performed through
data streams where as character oriented IO is performed through Readers
and Writers.
1. What is an Exception?
Ans: During program development there may be some cases where the
programmer does not have the certainty that this code-fragment is going to
work right, either because it accesses resources that do not exist or it goes
out of range. These types of anomalous situations are generally called
exception and the way to handle then is called exception handling.
(iii) Processing exceptions for widely used components that should not
process their own exception.
1. What do you mean by try block? How do you define it, give an example.
Ans: The try block is the one that contains the code that is to be monitored for
the occurrence of an exception. A try block is defined by enclosing the
statements that might possible raise an exception in. For example if the
formatting exception are to be handled while an integer is being read from the
keyboard, then the following try block can be used:
int inData;
try
{
inData=Integer.parseInt(br.readLine());
1. What do you mean by catch block? How do you define it, give an example.
Ans: The catch block is the one that contains the code handle an exception. It
must follow the try block. i.e. there should be no statement between the try
and the catch blocks. If the catch block is written for the above try block then
we may do it as follows:
int inData;
try
inData=Integer.parseInt(br.readLine());
catch(NumberFormatException nfEx)
Ans: The finally block is one of the exception handling blocks. The code
written in this block is always executed irrespective of whether an exception
was reported or not, or even if it was handled successfully or not. The purpose
of this block is to do cleaning up tasks, e.g. closing files etc.
Ans: EOFException: Signals that an and of the file or end of the stream has
been reached unexpectedly during input.
Ans: Wrapper classes are the part or Java’s standard library java.lang and
these convert primitive datatypes into an object. to be more specific, a
wrapper class wraps a value of primitive types in an object. Java provides the
following wrapper classes: Boolean Integer, Float, Double, Character etc.
Ans: A data type starts with lowercase letter and wrapper class starts with
uppercase letter.
1. Define String?
Ans: A string is a set of two or more then two characters, a set of characters
with the digit or a statement written with in double quotes. e.g. “Happy New
Year”, “Computer Application” etc.
Ans: The String object of Java is immutable, i.e. once created they can not be
changed. if any change occurs in a String object, then original object string
remains unchanged and a new String is created with the changed String.
StringBuffer objects are mutable, on the other hand. That is these objects can
be manipulated and modified as desired.
Ans: The purpose and syntax of the following string functions are:
toLowerCase(): This function converts all the characters of the string in lower
case.
for example:
String n=”AMITABH”;
n=n.toLowerCase();
System.out.println(n);
toUpperCase(): This function converts all the characters of the string in upper
case.
for example:
String n=”amitabh”;
n=n.toUpperCase();
System.out.println(n);
replace(): This function replace all the occurrence of a characters with another
one.
String n=”DAD”;
n=n.replace(‘D’,’G’);
System.out.println(n);
trim(): This function is used to remove all the white spaces at the beginning
and end of string.
String n=”AMIT “;
n=n.trim();
System.out.println(n);
equals(): This function is used to compare two string and give true or false if
they are equal.
String s1=”AMIT”;
String s2=”amit”;
System.out.print(s1.equals(s2));
length(): This function return the length characters present in the string.
String s=”AMITABH”;
System.out.print(s.length());
String s=”AMITABH”;
System.out.print(s.charAt(2));
String s2=”BANERJEE”
System.out.print(s1.concat(s2));
substring(): This function returns the substring starting from the nth character
of the string.
String s=”AMITABH”;
System.out.print(s.substringt(3));
This function also returns the substring starting from the mth character upto
the nth character without including the nth character of the string.
String s=”AMITABH”;
System.out.print(s.substringt(2,4));
indexOf(): This function returns the position of the first occurrence a character
in the string.
String s=”AMITABH”;
System.out.print(s.indexOf(‘A’));
This function also returns the position of the character from the nth position of
the string.
String s=”AMITABH”;
System.out.print(s.indexOf(‘A’,2));
compareTo(): This function returns negative if first string is less then second
string, positive if greater and zero if equals.
String s1=”AMIT”;
String s2=”SUMIT”
System.out.print(s1.compareTo(s2));
Ans: Both the functions is used to compare strings, the difference being that
equals() distinguishes between upper case and lower case version of a
character, where as equalsIgnoreCase() carries out comparison ignoring the
case of characters.
Ans: Both the functions is used to comparing two strings, the difference being
that (i) equals() method only comparing two string and gives they are equal or
not, where as compareTo() methods also gives whether first string is greater
or smaller then second one. (ii) equals() methods returns a boolean value,
where as compareTo() methods return integer value.
Ans: The given two string method’s change the case of the current string. The
toLowerCase() method change the current string object to its equivalent
Lower Case, where as toUpperCase() method change the current string
object to its equivalent Upper Case.
1. What is the difference between the length() and capacity() string function.
Ans: The function length() returns the number of character contains in a string.
Where as capacity() returns the maximum number of character that can be
stored in a string objects.
Ans: The members that are declared static are called static members. These
members are associate with the class it self rather than individual objects.
Ans: Static variables are used when we want to have a variable common to all
instances of a class.
Ans: Java API packages provide a large number of class grouped into
different packages according to functionality.
Ans: The packages which are organised in hierarchical structure are referred
as system packages.
Ans: To import a member of package into the current file, put an import
statement at the beginning of the file before any class definitions but after the
package statement, if there is one .
(ii) Exist at class level and can be used even if no instance of class exist in
memory.
(v) Can be accessed using either the class name or name of any instance of
the class. (i) Declare without the static keyword.
(ii) Exist at instance level i.e. can not be used if there are no instance of class
exist in memory.
(v) Can be accessed using the name of the instance only to which they
belong.
Ans: A data member that is created for every objects of the class.
Ans: ==: 1. It is a relational operator. 2. it tests the value on the right side with
value on the left side.
Encapsulation
private, public, scope and visibility rules. packages and package level access.
Ans: The wrapping up to data and methods into a single units (called class) is
known as encapsulation. For example an engine of car or any vehicle
contains many small parts, which enables the entire machinery system to
work. Encapsulation property hides the inner working of objects from the real
world.
Ans: A java program contains many classes. But one class in a Java program
contains the main() method. This class is called initial class.
Ans: A data member that is declared once for a class. All objects of that class
type, share these data members, as there is single copy of them available in
memory. Keyword ‘Static’ in the variable declaration makes a class variable.
Ans: A data member that is created for every objects of the class.
Ans: The program parts in which a particular data value (e.g., variable) can be
accessed is known as variable’s scope.
Ans: Visibility is a related term which refers to whether one can use a variable
from a given place in the program.
Ans: (i) Data declared at the class level can be used by all methods in the
class.
(ii) Data declared within a method can be used only in the method.
(iv) Variable that are declared in block i.e., local variable are available to every
method inside of the block.
(v) Variable declared in interior blocks are not available outside of that block.
(vi) Variable declared in exterior blocks are visible to the interior blocks.
(a) PUBLIC: It means that any one can call this method.
(b) PRIVATE: It means that only the methods in the same class are permitted
to use this method.
(c) PROTECTED: It means that methods in this class and methods in any
subclass may access this method.
Ans: Member variables are also known as Instance variables. These member
variables are used to store value in the class. It may be public, private and
protected, where private and protected members remains hidden from outside
world and there by support data.
Ans: PRIVATE visibility of a Method means that only the methods in the same
class are permitted to use this method.
Arrays
Ans: An Array is a collection of variables of the same data type that are
referenced by a common name. Array can be declared by the following
statements: int n[]=new int[10];
(i) Single Dimensional Arrays: A list of items can be given one variable name
using only one subscript and such a variable is called a single subscripted
variable or a one or single dimensional array.
(ii) Multi Dimensional Arrays: This type of arrays are actually arrays of arrays.
Ans: The Advantages or Arrays are: (i) Easy to Specify. (ii) Free from run-time
overload. (iii) Random access of elements. (iv) Fast Sequential Access.
Ans: Array can be initialized at the time of declaration by providing the value
list at the same time.
Ans: The subscripts other than 0 to n-1 for an array having n elements are
called out-of-bounds subscripts.
1. What do you mean by Binary Search?
Ans: This search technique searches the given ITEM in minimum possible
compression. The Binary search requires the array must be sorted in any
order. The search ITEM is compared with middle element of the array. If the
ITEM is more then the middle element later part of the arrays becomes the
new array segment. The same process is repeated until either the ITEM is
found or the array segment is reduce to single element.
Ans: In linear search each elements of the array is compared with the given
item to be searched for one by one while binary search searches for the given
item in a sorted array. The search segment reduces to half at every
successive stage.
Ans: For Binary Search The List must be sorted, lower bound upper bound
and the sort order of the list must be known.
Ans: The Linear search compares the search item with each element of the
array, one by one. If the search item happens to be in the beginning of the
array, the compressions are low, however if the element to be searched for is
one of the last elements of the array, this search technique proves the worst
as so many comparisons take place. The Binary search on the other hand,
tries to locate the search item in minimum possible comparisons, provided the
array is sorted. This technique proves efficient in nearly all the cases.
1. What do you mean by sorting?
Ans: In selection sort the smallest ( or largest depending upon the desired
order) key from the remaining unsorted array is searched for and put in the
sorted array. The process repeats until the entire array is sorted.
Ans: In bubble sort the adjoining values are compared and exchanged if they
are not in proper order. This process is repeated until the entire array is
sorted.
Ans: 10th element. Because the first index number/subscript value of an array
is 0. So 9th element is treated as the 10th element in an array.
Operations on File
Stream – byte and character stream, files and operation on files. token and
String Tokenizer and stream Tokenizer classes.
[Note: no questions shall be asked in the theory paper from Operation on
Files]
1. What is File?
Ans: All programs need to handle data flow to or from a file, a pipe or an I/O
device. Stream are object representation of this flowing data and are used by
java to carry out data interchanged in a program.
Ans: InputStream class provides the basis for classes used to read binary
data from a file, a pipe or an input device.
Ans: Reader class provides the basis for classes used to read character data
from a file or an input device.
Ans: OutputStream class provides the basis for classes used to write binary
data to a file, a pipe or an output device.
1. What is the role of a Writer Class?
Ans: Writer class provides the basis for classes used to write character data to
a file, a pipe or an output device.
1. Name the most commonly used classes for handling Byte oriented IO.
Ans: When an existing file is to be opened for writing all the data of the file is
truncated unless it is opened in the append mode using the following syntax:
Ans: A buffer represent a section of memory used as a staging area for input
or output data? Buffered I/O is an input or output operation that uses
temporary storage called a buffer to hold data before it is transferred from the
source to the destination.
Ans: A String Tokenizer can identify and parse (segregate) token in a string.
String Tokenizer class is found in java.util package. String Tokenizer class
also provides two methods that are immediately used for processing strings.
(i) countToken() (ii) nextToken()
Ans: A Stream Tokenizer takes an input stream and parses it into tokens,
allowing the tokens to be read one at a time. When reading an input string
supplied by a user we like to be able to analyze it token by token. To isolate
such tokens, we use the String Tokenizer class. We can also use the string
Tokenizer to process input from a file line by line.
Ans: In text file data are stored as per a specific character encoding scheme.
In binary files, data are stored in the form of bytes that are machine readable
form.
Ans: The package java.io provides classes that encapsulate input and output
stream in various forms. This package provides API that can be used to
perform character/byte based data input/output in Java program.