Menu
Topics Index
...
`

Intermediate Level Test 1 has these questions


Calculate Factorial

Write a program to calculate factorial of the given number.
class Factorial

{    public static void main(String s[])
    {
        System.out.println("Factorial of 5 is " + factorial(5));

    }


public static int factorial(int number)
{
int result = 1;
//Write code here to calculate the factorial and assign it to result
return result;
}
}



Add Two Numbers

Write a program to add two numbers.
class AddNumbers

{    public static void main(String s[])
    {
        System.out.println("Sum of 21 and 18 is " + sum(21, 18));

    }


public static int sum(int a, int b)
{
int result = 0;
//Write code here sum a and b and assign it to result
return result;
}
}



Average of Three Numbers

Write a program to get the average of three numbers;
class ThreeNumbersAverage

{    public static void main(String s[])
    {
        System.out.println("Average of 15, 70 and 27 is " + average(15, 70, 27));

    }


public static int average(int a, int b, int c)
{
int result = 0;
//Write code here to get the average of a, b and c and assign it to result
return result;
}
}



Find Prime Number Or Not

Write a program to check if a given number is prime number or not? A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. For eg. 3, 5, 7, 11, 13 are prime numbers where as 2, 4, 9, 15 are not.
class IsPrime

{    public static void main(String s[])
    {
        System.out.println(prime(37) ? "37 is prime" : "37 is not prime");

    }


public static boolean prime(int input)
{
boolean result = false;
//Write code here to assign true to the variable result, if input is prime otherwise assign false.
return result;
}
}



Relational Operators Output

What will be the output of the following program?
class Test1
{
    public static void main(String s[])
    {
        float f = 75.0f;
        double d = 75.0;
        int i = 75;

        if( f == d )
        {
            if( f == i )
            {
                System.out.println("f, d and i are equal");
            }
            else
            {
                System.out.println("f, d are equal but i is not equal");
            }
        }
        else
        {
            System.out.println("f and d are not equal");
        }
    }
}
A. f, d are equal but i is not equal
B. f, d and i are equal
C. f and d are not equal
D. Program does not compile since data types are different



© meritcampus 2019

All Rights Reserved.

Open In App