Constructor and Function

 

Constructor and Function

 

         

Question:

1.      Write the prototype of a function check which takes an integer as an argument and returns a character.

 

Answer: char check(int n)           

 

2.      Write two characteristics of a constructor.

 

Answer: Constructor has same name as their class name. Constructor has no return type, not even void.

 

 

3.      What are the two ways of invoking functions?

 

Answer: Call by value and call by reference.

 

 

4.      Write the function prototype of the following:

 

Answer: A function posChar which takes a string argument and a character argument and returns an integer value. int posChar(String s, char ch)

 

5.      What is a parameterized constructor?

 

Answer: Parameterized constructor are the constructors with atleast one parameter and initialize instance variable with received values

 

 

6.      Name the two types of constructors.

 

Answer: Parameterized constructors and non-parameterized (default) constructors.

 

7.      Name the mathematical function which is used to find sine of an angle given in radians.

 

Answer: Math.sin()

 

8.      Name a string function which removes the blank spaces provided in the prefix and suffix of a string.

 

Answer: trim()

 

9.      What is a constructor? When is it invoked?

 

Answer: A constructor is a member method that has same name as its class. It is used toinitialize the new objects created in the class with initial or default values. It is invoked automatically as the object is created.

 

10.  Name the methods of Scanner class that:

 

               (i) is used to input an integer data from the standard input stream.

               Answer: nextInt()

 

               (ii) is used to input a String data from the standard input stream.

               Answer:  nextLine()

 

 

11.  Create a class with one integer instance variable. Initialize the variable using:

               (i) default constructor

 

               (ii) parameterized constructor

 

Answer:

class Example{

    int num;

    public Example(){

        num = 0;

    }

    public Example(int n){

        num = n;

    }

}

 

12.  Question: State the method that:

 

(i) Converts a string to a primitive float data type.

Answer: Float.parseFloat()

 

(ii) Determines if the specified character is an uppercase character.

Answer: Character.isUpperCase()

 

 

13.  What are the two ways of invoking functions?

 

Answer:  The two ways of invoking functions are: Call by value and Call by reference

 

14.  Which unit of the class gets called, when the object of  1 marks the class is created?

 

Answer: Constructor

 

15.  Explain the concept of constructor overloading with an example.

 

Answer: In constructor overloading, we have one or more constructor in the same class with either different number of parameter or different type of parameter.

Example:

class operation{

    int number;

    public operation(){

        number=6;

    }

    public operation(int n){

        number = n;

    }

}

 

16.  Give the prototype of a function search which receives a sentence sentnc and word wrd and returns 1 or 0.

 

Answer:  int search(String sentnc, String wrd)

 

17.  Write two advantages of using functions in a program.

Answer: Code Complexity is reduced codes can be reused

 

18.  Give the prototype of a function check which receives a character ch and an integer n and returns true or false.

 

Answer: boolean check(char ch, int n)

 

19.   State two features of a constructor.

 

Answer:  Constructor has same name as their class name. Constructor has no return type, not even void.

 

No comments:

Post a Comment