What is a wrapper class? Give few examples of wrapper classes.
Ans. The wrapper class encapsulate or wrap the primitive data-types within a class to form an object representation of it.
Examples of wrapper classes
2. What are access specifiers?
Ans. Access specifiers are keywords which is used to declare which entity cannot be accessed from where. Its effect has different consequences when used on a class, class member (variable or method), and constructor. Java offers four access specifiers, listed below in decreasing order of accessibility:
public
protected
default/friendly
private
3. Name the wrapper class function that converts an integer to a:
i) Binary Number
ii) Octal Number
iii) Hexadecimal Number
Ans. i) Integer.toBinaryString( ) ;
ii) Integer.toOctalString( );
iii) Integer.toHexString( );
4. State the difference between parse...( ) and valueOf() method.
Ans. The parse...( ) method of the wrapper class converts a String to its corresponding primitive data type and returns it whereas valueOf() method converts a String to the corresponding wrapper object and returns it.
5. State the similarity difference between private and default access apecifier.
Ans. Similarity between private and default access specifier is that both allows accessibility within the class.
Difference between private and default is that private access specifier allows accessibility only within the class and default access specifier allows accessibility to all classes within a package.
6. What is a package? State its significance.
Ans. Package is a folder that contains compiled classes having both a name and a visibility control mechanism for using its function in another class.
A package gives you an organized way of managing classes in Java. You can group related classes into a package thus making it more organized. Similarly it is possible to define classes inside a package that are not accessible by code outside the package. There may also be classes that are only exposed to other members of the same package. This allows the classes to have intimate knowledge of each other, but do not expose it to the entire world.
7. Why can’t a class be declared as private?
Ans. A class when defined as private, it will be impossible to access its members and thus making it unusable.
8. Write the statement to import all classes from the Simple package.
Ans. import Simple.*;
9. What is a method?
Ans. Function or Method is a block of code containing executable lines of code that represents the behavioural aspect of an object.
10. Write two advantages of using functions in a program.
Ans. The Advantages of using functions are:
a) Reduces Complexity: Breaking a large task into smaller tasks automatically reduces complexity, thereby increasing the maintainability of the program.
b) Reusability: Once a method is defined, it can be invoked again and again from different segments of a program thus reusing the method whenever required.
11. Explain the function of a return statement.
Ans. The return statement is used to return-back or exit from a function. It comes in two variants:
1. return; It is generally used with function having void return-type and is used to explicitly force an exit from the function.
2. return <value>; It is used with function which have to return a value to the place where it is called from. Thus it is used with functions having return-type anything other than void. This statement not only forces an exit from the function but also returns a value.
12. If a function contains several return statements, how many of them will be executed?
Ans. If a function contains several return statements, only the first one which JVM comes across is executed. Thus forcing an exit from the function and therefore the remaining return statements are not executed.
13. Name the keyword that causes the control to transfer back to the method call.
Ans. The keyword that causes the control to transfer back to the method call is –return.
14. What is the role of the keyword void in declaring functions?
Ans. The void return-type of a function is used to ensure that a function do not return any value.
15. Classify functions depending upon the value it returns.
Ans. Depending on the value that is returned by a function, it is classified as:
a) Computational Function
b) Manipulative Function
c) Procedural Function
16. Differentiate between Formal Parameter and Actual Parameter.
Ans. The difference between Actual Parameters and Formal Parameters are:
17. How are functions called? How does it return a value?
Ans. A function may be called or executed in Java by simply writing the function name followed by the parameter-list within parenthesis. For example, if a function prototype contains the following:
void totalSurfaceArea(float l, float b, float h);
when calling the function it should be specified as:
totalsurfaceArea(x,y,z);
where x, y and z are variables of float type and are assigned to l, b and h respectively.
After a function computes a value, it can be returned to the place where it is invoked using the general syntax:
return <value>;
18. What is call by value?
Ans. During call by value a copy of the actual parameters is made by the formal parameters and therefore any changes made to the formal parameters is not reflected by the actual parameters.
19. State the difference between Call by Value and Call by Reference.
Ans. The difference between Call by Value and Call by Reference is that:
20. How are the following passed?
i) Primitive types
ii) Reference types
Ans. i) Call by Value
ii) Call by Reference
21. Classify functions which accepts objects as parameters.
Ans. i) Pure Functions
ii) Impure Functions
22. Define an impure function.
Ans. Impure functions are such methods which change the state of an object.
23. Differentiate between pure and impure functions.
Ans. Difference between Pure and Impure functions:
24. Explain function overloading with an example.
Ans. Multiple functions with the same name but different parameter list is termed as function overloading. When an overloaded function is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call.
25. Which OOP principle implements function overloading?
Ans. The principle of Polymorphism is implemented using Function Overloading.
26. When there are multiple definitions with the same function name, what makes them different from each other?
Ans. When there are multiple definitions with the same name, the parameter-list makes them different from each other.
27. What are the different access specifiers available in Java?
Ans. The different access specifiers in Java are: default, public, protected and private.
28. What is the function of main( ) method?
Ans. In conventional Java the main( ) function is from where the program execution begins and is therefore also called the driver function. In BlueJenvironment main() do not have any relevance at all it behaves exactly like any other function in Java.
29. How are static methods of one class called by methods in other classes?
Ans. Methods which are static are called by other classes using the class name of which it is a content followed by the method call statement.
Enter your comments
ReplyDeleteThanks man. I suck at computers and these r very helpful!
ReplyDeletethanks
Delete