admin Admin
| Subject: More Computer questions Fri Nov 23, 2007 11:45 am | |
| Source http://www.luckybakshi.blogspot.com Posted by Page for Students -------------------------------------------------- Q1. How are the local scope of variables different from global scope of variables? Ans: Java allows variables to be defined in a block enclosed in curly braces { }, which gives the scope and lifetime to a variable. The variables defined within main class are called global variables, and have scope and lifetime till the class is executing. The variables defined within the method it is local variables, which have scope only till that particular method starts and ends. It also protects that variable from unauthorised access from outside the scope.(i.e. its method).
Q2. What is default and parameterized constructors? Ans : Constructors are the special functions for automatic initilisation of object during creation. Default constructors : If a constructor for a class is not explicitly defined, then java creates default constructor. It does not take arguments or initilisation values from user. For eg. public class circus { int p, int b, int tot; public circus( ) { p = 0; b= 0; tot = 10; } Parameterized constructor : The constructor that take arguments are called parameterized constructors. They accept parameters and initialize the data members based on arguments passed to it. For eg. public class circus { int p, int b, int tot; public circus( int a, int x, int c) { p = a; b= x; tot = c; }
Q3. What is function call and function definition ? Ans: Function call : Once function is written in a program it can be called anywhere and reused any number of times in the program to solve specific problem is called function call. For eg. public class circus { public void mn( ) { int var; public int addi ( int x, int y){ return x+y; } var=addi(7,; System.out.println(“the value is :”+var); }} Calling function function Call public void mn( ) addi ( int x, int y){ { --------- ---------- } return x+y; } var=addi(7,; ------- -------
Function definition : Function is a set of instructions given to computer in order to perform specific task. Function definition is shown in following form. Access specifier type fun_name (parameters) { -------- -------- body of the function; } where as : access specifier is public or private. type specifies the data type of function return. If function does not return any value then void. Function name gives name to function with rules for identifiers. parameter list specifies the list of arguments. body of function are statements for manipulation.
Q4. What is difference between actual parameters and formal parameters? Ans : Actual parameters : An argument passed to a function when it is invoked. For eg. var = addi(7,; here 7 and 8 are the arguments passed in the function when the value of var is to be calculated actually. Formal parameters : The parameters passed to a function which receives value when method is called , is the formal parameter. For eg. public int addi ( int x, int y){ return x+y; } Here function addi takes up the aruguments x and y which are formal parameters.
Q5. What is StringBuffer? Ans : Two string_support classes are available in java.lang. String and StringBuffer.
Class StringBuffer supports the Strings which can be modified. It has operations that can modify the string or change its length which is not available for only string class once declared. StringBuffer class contains following constructors. StringBuffer( ) StringBuffer( int size) StringBuffer( String str) length( ) capacity( )
Q6. Name two ways by which parameters are passed to function? Ans : Parameters are passed to function in two ways like : Call by value Call by reference Call by value : This method copy the entire arguments passed to the function. It copies the value of an argument into the formal parameters of the subroutine. As a copy of arguments is made and passed to the function, any change made to the parameters inside the function do not affect the original arguments. For eg. class extraa { public void mn( ) { int y, int x=20; System.out.println(“the original value is :”+x); y=valuechang(x); System.out.println(“the original value is :”+y); } public int valuechang(int t) { int p=t; return p; } } Call by reference: This method reference to the arguments is passed as parameter. The value of an argument is not passed to the function, so any change made to the object inside the function will be reflected to the object passes as the original arguments. Call by reference used dot operator (.) to call the object. For eg. class extraa{ int x,y; public extraa(int i,int j){ x=i; y=j; } public void tentimes(extraa ob){ ob.x*=10; ob.y*=10; } public void printo() { System.out.println(" x is :"+x); System.out.println("y is :"+y); } public void main() { extraa as1=new extraa(20,30); as1.printo(); System.out.println("the changed values are :"); as1.tentimes(as1); as1.printo(); } } |
|
nirant
| Subject: Help Needed: Call By Reference: Object Declaration Not Clear Tue Mar 03, 2009 10:01 pm | |
| public void tentimes(extraa ob)
In the above line is the object created or what is actually happening?? As the object is no where in prog created using new keyword, I may assume that it is created here. Please help asap.
Thanks in advance. |
|
Anirudh The Conquerer Active member
| Subject: Re: More Computer questions Wed Mar 04, 2009 1:44 pm | |
| nirant we are passing object as parameter here the new keyword is used to create as1 this as1's address is copied to object ob and thus the values will change for x & y
i am a little lazy at typing i am sorry i did not explain in detail |
|
pavan Active member
| Subject: Re: More Computer questions Wed Mar 04, 2009 2:14 pm | |
| ley.............aniroooooooodh thnx |
|
pavan Active member
| Subject: Re: More Computer questions Wed Mar 04, 2009 3:06 pm | |
| heeyyyy.........can ny1 tel me the output of abov progrmz so tht icn undrstd betr |
|
nirant
| Subject: Re: More Computer questions Thu Mar 05, 2009 8:23 am | |
| Thanks a lot Anirudh.....I understood it quite well. And I really didn't mind your laziness, it worked well 4 me. Thanks again. |
|
Arya Nair Active member
| Subject: Re: More Computer questions Thu Mar 05, 2009 8:58 am | |
| thanks |
|
Sponsored content
| Subject: Re: More Computer questions | |
| |
|