0% found this document useful (0 votes)
100 views

Java Sample

The document contains code snippets and questions related to Java programming concepts like inheritance, polymorphism, constructors, overloading, arrays etc. It tests the understanding of output of given code snippets by providing multiple choice answers for each code snippet.

Uploaded by

Prashanna Prabu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Java Sample

The document contains code snippets and questions related to Java programming concepts like inheritance, polymorphism, constructors, overloading, arrays etc. It tests the understanding of output of given code snippets by providing multiple choice answers for each code snippet.

Uploaded by

Prashanna Prabu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1 | Page

public class FizzBuzzTest{


public static void main(String args[]){
for(int i = 1; i <= 50; i++) {
if(i % (3*5) == 0) System.out.println("FizzBuzz");
else if(i % 5 == 0) System.out.println("Buzz");
else if(i % 3 == 0) System.out.println("Fizz");
else System.out.println(i);
}
}
}

2 | Page

3 | Page

4 | Page

public class TestEvenOdd {


public static void main(String arg[]){
int num=6;
int result=num;
while(result>=2){
result=result-2;
}
if(result==1){
System.out.println("The number is odd");
}else{
System.out.print("The number is even");
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14

What is the o/p of following program?


public class Widening1 {
public void f1(Object o1){
System.out.println("Inside f1 with object as argument");
}
public void f1(String s){
System.out.println("Inside f1 with String as argument");
}
public static void main(String[] args) {
new Widening1().f1(null);
}
}
a) Inside f1 with String as argument
b) Inside f1 with object as argument
c) Compilation error
d) Runtime error

What is the o/p of following program?


public class MyClass{
public static void main(String[] args) {
int[] dest = new int[]{0,1,2,3,4,5};
System.out.println(dest[0]+ dest[5]+dest[2]);
}
}

5 | Page

What is the o/p of following program?


public class Overloading {
public void f1(Integer i){
System.out.println("inside 1");
}
public void f1(int i){
System.out.println("inside 2");
}
public static void main(String args[]){
new Overloading().f1(8);
}
}
a) inside 1
b) inside 2
c) Compilation error
d) Rutime error

What is o/p of following program


public class Parent {

public static void staticMethod(){


System.out.println("Inside Parent static");
}
public static void main(String[] args) {
Parent p = new Child();
p.staticMethod();
}
}
class Child extends Parent {
public static void staticMethod(){
System.out.println("Inside Child static");
}
}

a) Inside Parent static


b) Inside Child static
c) Compilte time error.

6 | Page
1.
2.

public class CloseTo100 {

3.
4.
5.
6.

public static int calculate(int input1, int input2) {

7.
8.

//compute the difference. Negative values are allowed as well

9.
10.

int iput1Diff = Math.abs(100 - input1);

11.
12.

int iput2Diff = Math.abs(100 - input2);

13.
14.
15.
16.

//compare the difference

17.
18.

if (iput1Diff < iput2Diff) return input1;

19.

else if (iput2Diff < iput1Diff) return input2;

20.

else return input1;

21.

//if tie, just return one

22.
23.

public static void main(String[ ] args) {

24.

//+ve numbers

25.

System.out.println("+ve numbers=" + calculate(50,90));

26.
27.

//-ve numbers

7 | Page
28.

System.out.println("-ve numbers=" + calculate(-50,-90));

29.
30.

//equal numbers

31.

System.out.println("equal numbers=" + calculate(50,50));

32.
33.

//greater than 100

34.

System.out.println(">100 numbers=" + calculate(85,105));

35.
36.

System.out.println("<100 numbers=" + calculate(95,110));

37.
38.

}
}

public class Car {


public static void main (String [] args) {
Car a = new Car();
Car b = new Ferrari(); //Car ref, but a Ferrari object
a.start(); // Runs the Car version of start()
b.start(); // Runs the Ferrari version of start()
}
}
class Car {
public void start() {
System.out.println("This is a Generic start to any Car");
}
}
class Ferrari extends Car {
public void start() {
System.out.println("Lets start the Ferrari and go out for a cool Party.");
}

8 | Page

public class Boss{


String name;
Boss(String input) { //This is the constructor
name = "Our Boss is also known as : " + input;
}
Boss() {
name = "Our Boss is a nice man. We dont call him names.;
}
public static void main(String args[]) {
Boss p1 = new Boss("Super-Man");
Boss p2 = new Boss();
}
}

You might also like