Menu
Topics Index
...
`

Intermediate Level Test 5 has these questions


Print Character Formation

Write a program to print the following character formation using the input String. Spaces in between characters are important. Use System.out.println or System.out.print for printing.
Input (String) Printed Output
antartica a
a n
a n t
a n t a
a n t a r
a n t a r t
a n t a r t i
a n t a r t i c
a n t a r t i c a
GLOBAL G
G L
G L O
G L O B
G L O B A
G L O B A L
class PrintFormation

{    public static void main(String s[])
    {
        printFormation("antartica");

    }


public static void printFormation(String name)
{
//Write code here to print the required formation depending upon the size of name. Use System.out.println or System.out.print for printing.
}
}



Print Symbol Number Formation

Write a program to print the following formation depending upon size. Spaces at the starting of the line and spaces in between are important. Use System.out.println or System.out.print for printing.
Input (Integer) Printed Ouput
4       * 
    @ @ 
  3 3 3 
@ @ @ @
5         * 
      @ @ 
    * * * 
  4 4 4 4 
* * * * *
6           * 
        @ @ 
      * * * 
    @ @ @ @ 
  5 5 5 5 5
@ @ @ @ @ @
class PrintFormation

{    public static void main(String s[])
    {
        printFormation(5);

    }


public static void printFormation(int size)
{
//Write code here to print the required formation depending upon the size. Use System.out.println or System.out.print for printing.
}
}



Print reverse of the string

Write a program to print the reverse of given input string. Use System.out.println or System.out.print for printing.
Input (Integer) Printed Output
queue eueuq
gangnam mangnag
malayalam malayalam
class PrintReverse

{    public static void main(String s[])
    {
        printReverse("gangnam");

    }


public static void printReverse(String input)
{
//Write code here to print reverse of input.
}
}



Define Book Class With Multiple Constructors

Define Book class with multiple constructors such that the program compiles and runs without any error.
class BookWithMultipleConstructors

{    public static void main(String s[])
    {
        Book book1 = new Book("Java, The Complete Reference");
        Book book2 = new Book("Java, The Complete Reference", "Herbert Schildt");
        Book book3 = new Book("Java, The Complete Reference", "Herbert Schildt", 8);
        
        System.out.println("Book 1 = " + book1.name);
        System.out.println("Book 2 = " + book2.name + " - " + book2.author);
        System.out.println("Book 3 = " + book3.name + " - " + book3.author + " - " + book3.edition);

    }


}
class Book
{
String name;
String author;
int edition;
//Write code here to create three constructors - one which takes only name, second which takes name and author and the third which takes name, author and edition as parameters.
}



Method Overloading235

What will be the output of the following program?
class MethodOverloading
{
    public static void main(String s[])
    {
        print();
        print(8);
        int j = print(9);
    }

    public static void print()
    {
        System.out.println("Called print with no parameters");
    }

    public static void print(int i)
    {
        System.out.println("Called print with int parameter with return type void");
    }

    public static int print(int i)
    {
        System.out.println("Called print with int parameter with return type int");
    }
}
A. Called print with no parameters
Called print with int parameter with return type void
Called print with int parameter with return type int
B. Compilation Error - since there are two methods with int parameter but return type is different.
C. Compilation Error - since there are three methods with the same name print.
D. Runtime Error - It fails with a runtime error, since it does not know which print method to call.



© meritcampus 2019

All Rights Reserved.

Open In App