Course Content‎ > ‎Session 3‎ > ‎

Exercises

Answer the following questions

1. When if statements are nested, the last else gets associated with the nearest if without an else. (True / False)

2. One ifcan have more than one else clause. (True / False)

3. A switch statement can always be replaced by a series of if.......else statements. (True / False)

4. A switch expression can be of any type. (True / False)

5. A program stops its execution when a break statement is encountered. (True / False)

6. The following is a segment of a program:

    x = 1;
    
    y = 1;
    
    if(n > 0)
        x = x + 1;
    y = y - 1;

    What will be the value of x and y if n assumes a value of 1? And n = 0?

8. Java has no stand-alone functions. (True /False)

9................................  means you can have different methods that have the same name but a different argument list.

10. State whether the following statements are true or false:

    a. main is itself a method.
    
    b. The values of formal arguments can be changed inside the method.
    
    c. Every method should have a return statement.
    
    d. A method in Java should have at least one argument.
    
    e. We can pass any number of arguments to a method.
    
    f. A function can call another function or can call itself.
    
    g. A method always returns an integer value.
    
    h. Objects are passed by value to the methods

    i. When parameters are passed by value to a method, then whatever occurs 
       to these variables inside the function, will have no effect on the 
       variables used in the actual argument list.

11. Given a list of marks ranging from 0 to 100, write a program to compute and print the number of students who have obtained the following scores:

    in the range of 81 to 100
    
    in the range of 61 to 80
    
    in the range of 41 to 60
    
    in the range of 0 to 40  

12. Admission to a professional course is subject to the following conditions:

    Math score >=60
    
    Physics score >=50
    
    Chemistry score >=40
    
    Total in all three subjects >=200

    - OR - 

    Total in Math and Physics >= 150  

    Given the scores in the three subjects, write a program to process the applications to list the eligible candidates.

14. Determine the output of the following program:

    class product
    {
        public static void main(String args[])
        {
            int x, y;
            int result, result1;
             
            product p1 = new product();
             
            x = 12;
            y = 20;
             
            result = p1.prod(x, y);
            result1 = p1.prod(result, p1.prod(x,2));
             
            System.out.println("Result is " + result);
            System.out.println("Result1 is " + result1);
        }
        int prod(int p, int q)
        {
            return (p * q);
        }
    }
    
15. Which of the following function headers are invalid and why?

    Average(int x, int y, int z);

    power(int x, int (y-2))

    product(int m, 10)

    double minimum(float a; float b;)

16. Write a function that will return integer part of the floating-point number.

17. Write a program, which uses functions to perform the mathematical operations like addition, subtraction, multiplication and division. Allow the user to enter the numbers and specify the operation to be performed.

18. Analyze each of the program segments that follow and determine how many times the body of each loop will be executed.

a)  
    x = 5;
    y = 50;
    
    while(x <= y)   
    {   
        x = y / x;
        
        ...
    }

b) 
    m = 1;
    
    do
    {
        ...
        
        m =  m + 2;
    } while(m < 10)

c) 
    int i;
    
    for(i = 0; i <= 5; i = i + 2 / 3)
    {   
        ...
    }
    
d)  
    int m = 10; 
    int n = 7;
    
    while(m % n >= 0 )
    {
        ...
        
        m = m + 1;
        n = n + 2;
        
        ...
    }

19. What is an empty statement? Explain its usefulness.

20. Find errors, if any, in each of the following looping segments.  Assume that all the variables have been declared and assigned values:

a) 
    while(count != 10)
    {
        count = 1;
        sum = sum + x;
        count = count + 1;
    }
    
b) 
    name = 0;
    do
    {
        name = name + 1;
        System.out.println("My name is John");
    } while(name = 1)

c)
    for(x=1,x>10;x=x+1)
    {
        .................
        .................
    }
    
d) 
    m = 1;
    n = 0;
    for(; m + n < 19; ++n)
        System.out.println("Hello \n");
        m = m + 10;

21. Write a program to display the following output using for loop:
    $$$$$
    $$$$
    $$$
    $$
    $

22. The numbers in the sequence 1 1 2 3 5 8 13 21 ................................ are called as Fibonacci numbers.  Write a program using do......while loop to calculate and print the first m Fibonacci numbers. (Hint :  After the first two numbers in the series, each number is the sum of the two preceding numbers).
 
23.  Given a number, write a program using while loop to reverse the digits of the number.  For example if a number is
12345  then, it should be displayed as  54321. (Hint: Use modulus operator to extract the last digit and integer division by
10 to get the n-1 digit number from the n digit number)
 
24.   Write a program to compute sum of the digits of a given integer number.
 
25.  Find errors, if any, in the following code segment:

    int m;
    int x[ ] = int[10];
    int[ ] y = int[11]; 
    for(m = 1; m <= 10; ++m)
        x[m] = y[m] = m;
    x = y = new int[20];
    
    for(m = 0; m < 10; ++m)
        System.out.println(x[m]);

26.  Write a statement to declare and instantiate an array to hold marks obtained by students in different subjects in a class.
Assume that there are up to 60 students in a class and there are 8 subjects.
 
27. Given are two one-dimensional arrays A and B which are sorted in ascending order.  Write a program to merge them
into a single sorted array C that contains every item from arrays A and B, in ascending order. 

Comments