Input (String) | Printed Output |
---|---|
antartica | a |
GLOBAL | G |
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.
}
}
Input (Integer) | Printed Ouput |
---|---|
4 | * |
5 | * |
6 | * |
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.
}
}
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.
}
}
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.
}
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
|
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. |