Wednesday , October 16 2019
Home / Java Programming / Object Oriented Programming / Sample Questions Part 1

Sample Questions Part 1

Sample Questions and their answers:

1: What will be the output of the following program?

public class Printing
{
  public static void main(String[] args)
  {
    for(int i=1;i<=10;i++)
    {
     for(int j=1;j<=5;j++)
      System.out.print("*");
      System.out.println();
    } //end outer for
  }// end main
}// end class printing

Output is:

 *****

*****

*****

*****

*****

*****

*****

*****

*****

*****

 Result: 10 lines of stars. Every line having 5 stars.

Every line having a single line space

2: Find and correct the errors in each of the following segments of code

(a) 
for( i=100;i>=1;i++)
   System.out.println(i);

Ans of (a)

for( i=100;i>=1;i--)
  System.out.println(i);
(b) The following code should print whether integer value is odd or even:
switch (value%2)
  {
   case 0:
          System.out.println(“Even Integer”);
   case 1:
          System.out.println(“odd Integer”);
  }

Ans of (b)

switch (value%2)
   {
    case 0:
          System.out.println(“Even Integer”);
          break;
    case 1:
         System.out.println(“odd Integer”);
         break;
   }
(c) The following code should output the odd integers from 19 to 1
for(int i=19; i>=1; i+=2)
    System.out.println(i);

Ans of (c)

for(int i=19; i>=1; i-=2)
    System.out.println(i);
 (d) The following code should output the even integers from 2 to 100.
counter=2;
   do
     {
      System.out.println(counter);
      counter+=2;
     }
     While ( counter<100)

Ans of (d)

counter=2;
do
   {
    System.out.println(counter);
    counter+=2;
   }
While ( counter<=100);

3: Give the method header for each of the following methods

(a) Method calcHypo , which takes two double-precision, floating-points arguments side1 and side2 and return a double-precision, floating-point result.

Ans : public double calcHypo (double side1, double side2)

 (b) Method smallest ,which takes three integers x,y and z and return an integer.

Ans: public int smallest (int x, int y, int z)

(c)Method instructions,which does not take any arguments and does not return a value

Ans:  public void instructions ( )

(d) Method centToFloat, which takes integer argument number and returns a float.

Ans: public float centToFloat (int number)

Check Also

What are the set methods and get methods?

What are the set methods and get methods? The set methods are the methods in …

%d bloggers like this: