Computer Notess
Computer Notess
Computer Notess
Ans . Functions are the modules from where java programs are built. Function body is enclosed in curly braces and
any set of statements known as a block.and have Some declarative statements and executable statements. Methods
implements the behaviour of the objects.
↓ ↓ ↓
A function has two main parts i.e a header and a body. The first line is the function header. It specifies the return type,
the function name and the parameter list of function. It is also known as function definition Public int get
price (int p, int q)
Function Body: -
Function body is enclosed in curly braces and any set of statements known as a block.
. There are atleast three main reasons to use methods in the program:
Syntax :
[public protected private] [final static] { void int float, etc} (function name parameter)
Function Prototype: - Function Prototype is the first line of function definition that tells the program about the type of
the value return by the function and the number and type of arguments.
Function signature: - Function signature refers to the number and types of argument. It is the part of function
prototype, function signature and return type makes function prototype.
Public, private is a access specifiers, it specifies, the visibility attribute of the method.
Call Statement: - Function call statement must provide the argument values being sent and the number and type of
argument values must be the same as in prototype order of argument should be same.
Example: area(x,y)
Only those functions can be used in the expression that have a return value.
Q9 Explain return: -
Return causes the exit from the method. Return is followed by an expression or a variable or the value to be return to
the calling program.
Actual & Formal Parameters: - The parameters that appear in the function definition are called formal parameters.
Example: public int area (int l, int b) Parameter list are the list of values that are passed into the function.
Example: area(x,y)
The function which returns value and do not change the state of there parameters are called pure functions. They
return information to the caller about the state of an object.
A method that obtain information about an object state may also be classified as an accessor. The class string
provides many accessor method
Example: public int getdata (int price )
{ return price; }
Impure functions:-(mutators)
The function which may or may not returns value and change the state of the objects or parameters passed as
a parameters
Example: public int getdata (int price )
{ return( price+10); }
Q19 What is parameter list? Parameter list: - Parameter list are variables that receives the value of the argument
passed to the function when it is called.
Notes on Array
Ans. Array is a collection of same type of variables and are reffered by a common name and stored in a continiuos
location. Array is called as reference data type because object of the array is created.
Ans .Arrays are stored in contigious location. And the lowest address corresponds to the first element and highest to last
element.
Ans . Asubscript is an integer no starting from 0 to the size of the array-1. It is used to represent the index number of the
array.. and a variable with which a subs cript is attached is called as subscripted variable
Ordinary variable contains only one value in the memory whereas array variable contains a number of values in the
memory.
Total bytes required for storing the specified array = 2*17=34, if 17 is the number of elements.
6 What is the data type of elements of an array called?
The data type of the element of an array is called the base of the array
Individual elements of an array are addressed using the name of the array plus the subscripts 0,1, … n-1, where n is the
number of elements in the array.
8 What are out of bound subscripts?
The subscripts other than 0 to n-1 (including both) for an array having n elements are called as out of bound subscripts.
Reference to an out of bound subscript produces an out of bound exception.
Q1.Define constructor?
Ans. A constructor is a special member function of a class that is automatically called for initialization when an object
is created of the class.
2.substring(int start index ,int end index) this function will extract the new string form this string .it will
extract the characters starting from starting index uptill ending index .it will not include the ending index in the
new string. The return type is String.
Eg: String str=”computer”; System.out.println(str.substring(3,6)); =”put”
Substring(int index) if only one index is given then it will extract the string from that index uptill end of the
string.
Eg System.out.println(str.substring(3)); =”puter”
Eg System.out.println(str.length());=8
4. int indexOf(char ch): this method will print the index no of the first occurrence of the character in the
string. The return type of the index of function is int . if the character is not found in the string it will return -1.
Eg System.out.println(str.indexOf(‘m’)=2
5 int lastIndexOf(char ch) this method will print the index no of the last occurrence of the character in the
string. The return type of the index Of function is int . if the character is not found in the string it will return -
1. Eg System.out.println(str.lastIndexOf(‘r’)=7
6.String concat(String s1) this function will add or concat the given string at the end of the current String:
return type is string
7 +(concatenation operator) it is also usedto add two or more String ,character,numbers with a string: return
type is string
8.boolean endsWith(String s) checks whether the current string ends with the specified string or not the
return type is boolean
9.boolean startsWith(String s) checks whether the current string starts with the specified string or not the
return type is boolean
10 int compareTo(String s1) this function compare two string lexicographally in the order in which they
appear in dictionary. Return type is integer and it produces three output 1 positive no 2. Negative no 3. Zero. If
first string is greate r than the second it will give positive value . if first is smaller than it will give negative . for
equal string it will give zero. Eg int x= “Raman”.compareTo(“Arora”)=17
11.boolean equals (String str) will compare two string for equality the return type is boolean .eq
“one”.equals(“ONE”)= false
12 boolean equalsIgnoreCase(String str) will compare two string for equality ignoring the cases (capital and
small) the return type is boolean .eq “one”.equals(“ONE”)= true
13. replace (): This function will a character from the string with another string
Eg: str.replace(‘a’,’e’) will replace all ‘a’ from the string with ‘e’
14. trim() : this function will truncate all the leading and the trailing spaces
15 capacity(): it will return the maximum no of characters that can be entered into the string return
type is int
17 valueOf() will return string representation of the passed arguments return type is string
Character class
Ans: Character class is a class of java.lang package whose instance or object can hold single character data.
1. The object of String class are 1. The object of Stringbuffer class immutable (not change able) are
mutable(changeable) they once initialized its content can be changed and modified . cannot be
modified.
2. The object of string class can 2. The object of stringBuffer class may or maynot use new op- must
use new operator to create erator to create the object the object
1. What are loops? How many types of loops are there in Java? The iteration statements are also
called loops or looping statements. There are three kinds of loops in Java. The are for loop, while
loop and do while loop
a. Loops are of two types what are they? Exit control Loop and Entry Control Loop
2. What is an entry control loop? The looping statements in which the test expression is evaluated
before entering into the body of the loop is known as entry control loop. Eg. While loop and for loop
3. What is an exit control loop? The looping expression in which the test expression is evaluated after
the body of the loop is executed is known as exit controlled loop egg. Do while loop
4. When we will use for loop? For loop is used for a fixed number of iterations. Here the programmer
knows beforehand how many times the loop will be executed.
5. What are multiple initializations and multiple update expression? If in a for loop. More than one
variable are initialized and more than one variable are updated then it is known as multiple
initialization. Eg for(i=1,sum=0;i<=ni;sum+=I,++i)
6. What is an infinite loop and how is it created? The loop which goes on executing without any
stoppage is known as infinite loop. An infinite loop can be created by skipping the condition. Eg
a. int I; for (i=1; ;i++) b. for( ; ; )//all three skipped
7. What do you mean by delay loop? A loop can be used to create a delay loop. This can be done to
pause they control as needed by the user during programming. The body of the loop does not
contain meaningful operation to perform egg. Int i=1; while (i<=10) { i++; }
8. What do you mean by an empty loop? An empty loop is a loop without any statement in its loop
body. The effect of an empty loop is that the control variable gets updated and loop terminates as
13. What is a break statement? A break statement enables a program to skip over part of the code. It is
used in while, do-while, for loops. It skips the rest of the loop and jumps over to the statement
following the loop. Break statement used in switch case terminates a code sequence associated with
case. Break used in a nested loop will only break the loop in which it is contained.
14. How can we use the break statement in Java? In Java, the break statement can be used in three
ways 1. To terminate a statement sequence in a switch statement. 2. To exit a loop 3. As a form of
goto
15. What is continue statement? Continue is one type of jump statement. It skips the rest of the loop
statements and causes the next iteration of the loop.
16. Strike out the difference between Break and Continue statements? A break statement inside a
loop will abort the loop and transfer control to the statement following the loop whereas continue
statement will just abandon the current iteration and let the loop start the next iteration.
17. Which elements are needed to control a loop? The elements that control a loop are 1. The
initialization expression 2. The test expression 3. The update expression. 4. The loop body
18. Write the Syntax of for loop, while loop, do while loop
For loop:
For (initialization; test condition; update expert)
{// body of loop
}
Initialization eg int i=1;
while( test condition ) while( i<=10)
{ update expression ; { i++;}
}
dowhile : initialization
do
{
// body of loop
update expression ;