Write Output of Java Program - Exam Sample Questions
Answers are given at end.
1 public class StringTest {
public static void main(String args[]){
String t1=new String("Text");
String t2=new String("Text");
System.out.println(t1==t2);
String t3="Text";
String t4="Text";
System.out.println(t3==t4);
System.out.println(t1==t4);
System.out.println(t1.equals(t3));
}
}
2 public class ExceptionTest {
public static void main(String[] args) {
try {
System.out.println("Welcome to Java");
int[] a = new int[10];
a[10] = 1;
System.out.println("Welcome to Python");
} catch (Exception ex) {
System.out.println("There is an exception”);
}
System.out.println("Bye.");
}
}
3 class A{
public int x = 1;
public A(int i){
x += i;
}
}
class B extends A {
public B(int i){
super(i);
x += i;
}
}
public class Test{
public static void main(String args[]) {
A arr[] = new A[2];
arr[0] = new A(1);
arr[1] = new B(2);
System.out.println(arr[0].x + arr[1].x);
}
}
4 class A {
public String toString() {
return "class A";
}
public void method1() {
System.out.println("method1 of class A");
}
public void method2() {
System.out.println("method2 of class A");
}
public static void method3(){
System.out.println("method3 of class A");
}
}
class B extends A {
public String toString() {
return "class B";
}
public void method2() {
System.out.println("method2 of class B");
}
public static void method3(){
System.out.println("method3 of class B");
}
}
class C extends B { }
class D extends C {
public void method1() {
System.out.println("method1 of class D");
}
public static void method3(){
System.out.println("method3 of class B");
}
public String toString(){
return "Class D";
}
}
public class Test {
public static void main(String args[]){
A[] elements = {new A(), new B(), new C(), new D()};
for (A obj : elements) {
System.out.println(obj);
obj.method1();
obj.method2();
obj.method3();
}
}
}
5 class Nest{
public static void main(String args[]){
//Parent try block
try{
//Child try block1
try{
System.out.println("Inside block1");
int b =45/0;
System.out.println(b);
} catch(ArithmeticException e1){
System.out.println("Exception: e1");
}
//Child try block2
try{
System.out.println("Inside block2");
int b =45/0;
System.out.println(b);
} catch(ArrayIndexOutOfBoundsException e2){
System.out.println("Exception: e2");
}
System.out.println("Just other statement");
} catch(ArithmeticException e3){
System.out.println("Arithmetic Exception");
System.out.println("Inside parent try catch block");
} catch(ArrayIndexOutOfBoundsException e4){
System.out.println("ArrayIndexOutOfBoundsException");
System.out.println("Inside parent try catch block");
} catch(Exception e5){
System.out.println("Exception");
System.out.println("Inside parent try catch block");
}
System.out.println("Next statement..");
}
}
6 class X {
int product;
public X(int x, int y) {
product = x * y;
}
void multiplication() {
System.out.println("Multiplication of x and y is: " + product);
}
}
class Y extends X {
int productAll;
public Y(int x, int y, int z) {
super(x, y);
productAll = x * y * z;
}
void multiplication() {
super.multiplication();
System.out.println("Multiplication of x, y and z is: " + productAll);
}
}
// Class with main entry point
public class Question {
public static void main(String args[]) {
Y y = new Y(2,3,4);
y.multiplication();
}
}
7 public enum Gender {
MALE, FEMALE;
public static void main(String[] args) {
Gender gender = Gender.MALE;
Gender[] genders = Gender.values();
if (gender == genders[1]) System.out.print("A-");
if (gender == Gender.MALE && Gender.FEMALE != null)
System.out.print("B-");
if (genders.length > 1)
System.out.print(Gender.MALE + "-" + (genders[1]));
}
}
8 public class Box {
public Box() {
System.out.print("D");
}
public Box(int n){
this();
System.out.print("T");
System.out.print(m1());
}
String m1(){
System.out.print("N");
return "S";
}
public static void main(String[] args) {
Box b = new Box(8);
b.m1();
}
}
9.
public enum E1 {
ONE, TWO;
public static void main(String[] args) {
E1 e1 = E1.ONE;
E1[] arr = E1.values();
if (e1 == arr[1]) {
System.out.print("X");
}
if (e1 == E1.ONE && E1.TWO != null)
System.out.print("W");
if(arr.length > 1)
System.out.print(E1.ONE + "-" + (arr[1]));
}
}
10.
public class Arr {
static int[] n = {1,2,3};
static void m1(int n){
Arr.n[0]++;
n++;
}
static void m2(int[] m){
m[0]++;
}
public static void main(String[] args) {
int n[] = {4,5,6};
m1(n[0]);
System.out.println(n[0]);
n = Arr.n;
m2(n);
System.out.println(n[0]);
}
}
11.
public class Arr2 {
public static void main(String[] args) {
int[][] nums = new int[2][];
nums[0] = new int[]{1, 2};
nums[1] = new int[]{3, 4, 5};
for(int i=0; i<nums.length; i++)
for(int j=0; j<nums[0].length;j++){
System.out.print(nums[j][i] + "-");
}
}
}
12.
public class Student {
static int n = 0;
public Student() {
n++;
}
void m1(int n) {
n = Student.n - 1;
}
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
new Student().m1(Student.n);
}
System.out.println(Student.n);
}
}
13.
public class Box {
public Box() {
System.out.print( m1() + "T" );
}
public Box(int n){
m1();
}
String m1(){
System.out.print("N");
return "S";
}
public static void main(String[] args) {
Box b = new Box(1);
b.m1();
b = new Box();
}
1 public class StringTest {
public static void main(String args[]){
String t1=new String("Text");
String t2=new String("Text");
System.out.println(t1==t2);
String t3="Text";
String t4="Text";
System.out.println(t3==t4);
System.out.println(t1==t4);
System.out.println(t1.equals(t3));
}
}
2 public class ExceptionTest {
public static void main(String[] args) {
try {
System.out.println("Welcome to Java");
int[] a = new int[10];
a[10] = 1;
System.out.println("Welcome to Python");
} catch (Exception ex) {
System.out.println("There is an exception”);
}
System.out.println("Bye.");
}
}
3 class A{
public int x = 1;
public A(int i){
x += i;
}
}
class B extends A {
public B(int i){
super(i);
x += i;
}
}
public class Test{
public static void main(String args[]) {
A arr[] = new A[2];
arr[0] = new A(1);
arr[1] = new B(2);
System.out.println(arr[0].x + arr[1].x);
}
}
4 class A {
public String toString() {
return "class A";
}
public void method1() {
System.out.println("method1 of class A");
}
public void method2() {
System.out.println("method2 of class A");
}
public static void method3(){
System.out.println("method3 of class A");
}
}
class B extends A {
public String toString() {
return "class B";
}
public void method2() {
System.out.println("method2 of class B");
}
public static void method3(){
System.out.println("method3 of class B");
}
}
class C extends B { }
class D extends C {
public void method1() {
System.out.println("method1 of class D");
}
public static void method3(){
System.out.println("method3 of class B");
}
public String toString(){
return "Class D";
}
}
public class Test {
public static void main(String args[]){
A[] elements = {new A(), new B(), new C(), new D()};
for (A obj : elements) {
System.out.println(obj);
obj.method1();
obj.method2();
obj.method3();
}
}
}
5 class Nest{
public static void main(String args[]){
//Parent try block
try{
//Child try block1
try{
System.out.println("Inside block1");
int b =45/0;
System.out.println(b);
} catch(ArithmeticException e1){
System.out.println("Exception: e1");
}
//Child try block2
try{
System.out.println("Inside block2");
int b =45/0;
System.out.println(b);
} catch(ArrayIndexOutOfBoundsException e2){
System.out.println("Exception: e2");
}
System.out.println("Just other statement");
} catch(ArithmeticException e3){
System.out.println("Arithmetic Exception");
System.out.println("Inside parent try catch block");
} catch(ArrayIndexOutOfBoundsException e4){
System.out.println("ArrayIndexOutOfBoundsException");
System.out.println("Inside parent try catch block");
} catch(Exception e5){
System.out.println("Exception");
System.out.println("Inside parent try catch block");
}
System.out.println("Next statement..");
}
}
6 class X {
int product;
public X(int x, int y) {
product = x * y;
}
void multiplication() {
System.out.println("Multiplication of x and y is: " + product);
}
}
class Y extends X {
int productAll;
public Y(int x, int y, int z) {
super(x, y);
productAll = x * y * z;
}
void multiplication() {
super.multiplication();
System.out.println("Multiplication of x, y and z is: " + productAll);
}
}
// Class with main entry point
public class Question {
public static void main(String args[]) {
Y y = new Y(2,3,4);
y.multiplication();
}
}
7 public enum Gender {
MALE, FEMALE;
public static void main(String[] args) {
Gender gender = Gender.MALE;
Gender[] genders = Gender.values();
if (gender == genders[1]) System.out.print("A-");
if (gender == Gender.MALE && Gender.FEMALE != null)
System.out.print("B-");
if (genders.length > 1)
System.out.print(Gender.MALE + "-" + (genders[1]));
}
}
8 public class Box {
public Box() {
System.out.print("D");
}
public Box(int n){
this();
System.out.print("T");
System.out.print(m1());
}
String m1(){
System.out.print("N");
return "S";
}
public static void main(String[] args) {
Box b = new Box(8);
b.m1();
}
}
9.
public enum E1 {
ONE, TWO;
public static void main(String[] args) {
E1 e1 = E1.ONE;
E1[] arr = E1.values();
if (e1 == arr[1]) {
System.out.print("X");
}
if (e1 == E1.ONE && E1.TWO != null)
System.out.print("W");
if(arr.length > 1)
System.out.print(E1.ONE + "-" + (arr[1]));
}
}
10.
public class Arr {
static int[] n = {1,2,3};
static void m1(int n){
Arr.n[0]++;
n++;
}
static void m2(int[] m){
m[0]++;
}
public static void main(String[] args) {
int n[] = {4,5,6};
m1(n[0]);
System.out.println(n[0]);
n = Arr.n;
m2(n);
System.out.println(n[0]);
}
}
11.
public class Arr2 {
public static void main(String[] args) {
int[][] nums = new int[2][];
nums[0] = new int[]{1, 2};
nums[1] = new int[]{3, 4, 5};
for(int i=0; i<nums.length; i++)
for(int j=0; j<nums[0].length;j++){
System.out.print(nums[j][i] + "-");
}
}
}
12.
public class Student {
static int n = 0;
public Student() {
n++;
}
void m1(int n) {
n = Student.n - 1;
}
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
new Student().m1(Student.n);
}
System.out.println(Student.n);
}
}
13.
public class Box {
public Box() {
System.out.print( m1() + "T" );
}
public Box(int n){
m1();
}
String m1(){
System.out.print("N");
return "S";
}
public static void main(String[] args) {
Box b = new Box(1);
b.m1();
b = new Box();
}
Solution:
1.
false
true
true
2.
Welcome to Java
There is an exception
Bye.
3.
7
4.
class A
method1 of class A
method2 of class A
method3 of class A
class B
method1 of class A
method2 of class B
method3 of class A
class B
method1 of class A
method2 of class B
method3 of class A
Class D
method1 of class D
method2 of class B
method3 of class A
5.
Inside block1
Exception: e1
Inside block2
Arithmetic Exception
Inside parent try catch block
Next statement..
6.
Multiplication of x and y is: 6
Multiplication of x, y and z is: 24
7.
B-MALE-FEMALE
8.
DTNSN
9.
WONE-TWO
10.
43
11.
1-3-2-4-
12.
3
13.
NNNST
9.
WONE-TWO
10.
43
11.
1-3-2-4-
12.
3
13.
NNNST
Comments
Post a Comment