0% found this document useful (0 votes)
27 views20 pages

Java Program Output Assignments

The document contains questions about Java programs and code snippets. It asks what the output would be for various programs that demonstrate concepts like static methods and variables, constructors, object references, and more. The programs range from simple examples with one or two lines of code to more complex examples with method calls and object instantiation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views20 pages

Java Program Output Assignments

The document contains questions about Java programs and code snippets. It asks what the output would be for various programs that demonstrate concepts like static methods and variables, constructors, object references, and more. The programs range from simple examples with one or two lines of code to more complex examples with method calls and object instantiation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Please do not copy paste code in eclipse. Try it yourself with pen and paper !!

===============================================================================

1) What will following lines print:

String x="We are learning";


String y="mistakes happen";
int z=1000;
[Link]("Java is easy. "+ x + " selenium and "+ y +" "+z +" times")
============================================================================

2) What will be the output of following program

public class Test {


public static void main(String[] args) {
int i=2;
f1(1);

public static void f1(int i)


{
f2(i+1);
}

public static void f2(int i)


{
f3(i+2);
}

public static void f3(int i)


{
[Link](i+3);
}

}
============================================================================

3) What will be the output of following:


public class Test {

public static void main(String[] args) {


int x=0;
while(true){
x = increment(x);

[Link]("Value of x is --"+x);

if(x>10)
break;

}
}

public static int increment(int i){


return i+1;
}
}
============================================================================
4) What will be the output of following program

public class Test {

public static void main(String[] args) {


int i=2;
while(makeDecision(i)){

i=i*i;
[Link](i);
}

public static boolean makeDecision(int i)


{
if(i%3 != 0){
return true;
}else{
return false;
}
}
}
============================================================================

5) What will be the output of following:

public class Test {

public static void main(String[] args) {

String arr1[] = new String [3];


String arr2[] = new String [3];

arr1[0]="A";
arr1[1]="B";
arr1[2]="C";

arr2[0]="1";
arr2[1]="2";
arr2[2]="3";
printAll(arr2);
printAll(arr1);

public static void printAll(String str[]){


for(int i=0; i < [Link] ; i++){
[Link](str[i]);
}
}

}
============================================================================

6) What will be the output of following:


public class Test {

public static void main(String[] args) {


int a[][] = new int[10][5];
for(int i=0;i<10;i++){

for(int j=0; j<5; j++){


a[i][j]=i*j;
}
}

[Link](a[0][0]);
[Link](a[1][3]);
[Link](a[3][4]);
}
}
============================================================================

7) What will be the output of following program?

public class Test {

public static void main(String[] args) {


int i;
[Link](i);

int j=100;
[Link](j);
}

}
============================================================================

8) What will be the output of following program?

public class Test {


int i;
static int j;

public static void main(String[] args) {


[Link](i);
[Link](j);
}

public void non_static(){


[Link](i);
[Link](j);
}

}
============================================================================

9) What will be the output of following program?

public class Test {

public static void main(String[] args) {


non_static();
}
public void non_static(){
[Link]("pass");

}
============================================================================

10) What will be the output of following program?

public class Test {

int i;
static int j;

public static void main(String[] args) {


non_static();
}

public static void non_static(){


[Link]("pass");

}
============================================================================

11) What will be the output of following program?

public class Test {

int i;
static int j;

public static void main(String[] args) {


Test t = new Test();
t.non_static();
t.meth_static2();
meth_static2();
t.i=100;
j=200;
t.j=400;
}

public void non_static(){


[Link]("pass1");
}

public static void meth_static2(){


[Link]("pass1");
}

}
============================================================================

12) Will this code compile?

public class Demo1 {


int var=10;

public static void main(String s[])


{

int local=var;
}
}
============================================================================

13) Will this code compile?

class Demo
{
static int var=9;
public static void func()
{
[Link]("learning static keyword");
}
}
public class Main
{

public static void main(String s[])


{
Demo ob = new Demo();
[Link]=9;
[Link]();

}
}
============================================================================

14) What will be the output of following program?

public class Main


{

int var;
static int stc=7;
public static void main(String s[])
{
Main ob1 = new Main();
[Link]=9;
[Link]("var of ob1 "+[Link]);

Main ob2 = new Main();


[Link]=90;
[Link]("var of ob2 "+[Link]);

[Link]=[Link]+100;

[Link]("ob1 "+[Link]);

[Link]("ob2 "+[Link]);

}
}
============================================================================

15) What will be the output of following program?

public class Test {


int i;

Test(int i){
i=i;
}
public static void main(String[] args) {

Test t = new Test(7);


[Link](t.i);
}

}
============================================================================

16) What will be the output of following program?

public class Test {


int age;
String name;

Test(int age,String name){


[Link]=age;
[Link]=name;
}
public static void main(String[] args) {

Test t1 = new Test(17,"A");


Test t2 = new Test(13,"B");
Test t3 = new Test(14,"C");
t3=t2;
t2=t1;
t1=t3;
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
}

}
============================================================================

17) Whats the output of following program?

public class Test {


int age;
String name;

Test(){
non_static_meth();
static_meth();
}
public static void main(String[] args) {

Test t1 = new Test();


}

public void non_static_meth(){


[Link]("NM ");
}

public static void static_meth(){


[Link]("SM");

}
============================================================================

18) In real world, Contructors are used to:

1) Initialize all variables of a class


2) Initialize non-static varialbles of a class
3) static variables can be initialized in constructors
4) Give initial state to object
============================================================================

19) Whats the output of following program?

public class Test {


int i;
int j;

Test (int i, int j){


this.i=i;
this.j=j;
}

public static void main(String[] args) {

Test t1 = new Test();


Test t2 = new Test();

}
============================================================================

20) Whats the output of following program?

public class Test {


int i;
int j;

public static void main(String[] args) {

Test t1 = new Test();


Test t2 = new Test();

t1.j=t2.i=5;
t1.i=t2.j=6;

[Link](t1.j++ + " " + t2.i--);


}
}
============================================================================

21) Whats the output of following program?

public class Test {

Test t1= new Test();


int i;
static int j;
static Test t2 = new Test();
public static void main(String[] args) {
t1.i=10; //1
i=19; //2
j=10; //3
t2.i=19; //4

}
}
============================================================================

22) Compile-time errors are generated at which lines?

public class Test {

public static void main(String[] args) {


public int a; // 1
protected int b; // 2
private int c; // 3
static int d; // 4
transient int e; // 5
volatile int f; // 6
final int g = 1; // 7
int i=7; // 8
int h; //9
[Link](h); //10

}
}

1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
============================================================================

23) What will be output of follwoing?

class JavaClass {
static int i;
static JavaClass obj;
public static void main (String[] args) {

[Link]( obj + "" +i);

}}
============================================================================

24) What will be output of follwoing?

public class Test {

static int i;
static Test obj;
public static void main (String[] args) {

Test obj;
int i;
[Link]( obj + "" +i);

}}
============================================================================

25) A compile-time error is generated at which line?

public class Training {

public static void main(String[] args) {


static int a=1; //1
int b=1; //2
}

public void abc(){


static int a=1; //3
int b=1; //4

}
============================================================================

26) Which is the valid way of calling the main1 method?

public class JavaClass {

public static void main(String[] arg){

main1(); //1
JavaClass j = new JavaClass();
j.main1(); //2
}

public void main1(){

a) 1
b) 2
c) Both 1 and 2
d) Neither 1 nor 2
e) None of these
============================================================================

27) Compile time errors are generated at which lines?

public class JavaClass {

int i=1;
static int a=1;

public static void main(String[] args) {

public void nonstaticMethod(){


calArea(); // 1
nonstaticMethod(); //2

[Link](); // 3
JavaClass t = new JavaClass();
[Link](); // 4

i=i+1; // 5
a=a+1; // 6

static int b=1; // 7


}

public static int calArea(){

return 8*8;
}
}

a) 1,2,5,7
b) 2,5,7
c) 7
d) 2,4,6,7
e) 4,5,7
============================================================================

28) Compile time errors are generated at which lines?

public class JavaClass {

int i=1;
static int a=1;

public static void main(String[] args) {

JavaClass t= new JavaClass();


calArea(); //1
nonstaticMethod(); //2

[Link](); //3
[Link](); //4
i=i+1; //5
a=a+1; //6

static int b=1; //7

public void nonstaticMethod(){

public static int calArea(){

return 1*1;

}
}

a) 1,2,5,7
b) 2,5,7
c) 4,6,7
d) 2,4,6,7
e) 4,5,7
============================================================================

29) What will be outut of following program?

public class Test {


int i;
int j;

public static void main(String[] args) {


int area = calArea1(3,4);
[Link](area);
Test t = new Test();
area = calArea2(t);
[Link](area);
}

public static int calArea1(int i, int j) {


return i*j;
}

public static int calArea2(Test t) {


t.i=t.i+10;
t.j=t.i+20;
return t.i*t.j;
}

}
============================================================================

30) What will be the output of following?

public class Test {

public static void main(String[] args) {


A a = new A();
B b = new B();
[Link](a.x);
[Link](a.y);
[Link](b.x);
[Link](b.y);

class A{

String x="Parent";
}

class B extends A{
String y="Child";

}
============================================================================

31) What will be the output of following?

public class Test {

public static void main(String[] args) {


A a = new A();
B b = new B();

[Link]();
[Link]();

[Link]();
[Link]();

class A{

public void parentMeth(){

}
}

class B extends A{
public void childMeth(){

}
============================================================================

32) What will be the output of following?

class A{
}

class B {

class C extends A,B{

}
============================================================================

33) What will be the output of following?


interface A{

interface B {

class C implements A,B{

}
============================================================================

34) What will be the output of following?

public class Test{

public static void main(String[] a){


A a1 = new B();
a1.meth1();
a1.meth2();
a1.meth3();
a1.meth4();
}

interface A{
public void meth1();
public void meth2();
public void meth3();
}

class B implements A{

@Override
public void meth1() {
[Link]("meth1");

@Override
public void meth2() {
[Link]("meth2");

}
@Override
public void meth3() {
[Link]("meth3");

public void meth4() {


[Link]("meth4");

}
============================================================================

35) What will be the output of following program?

public class Test {

public static void main(String[] args) {

try{
int o[] = new int[2];
o[3]=23;
}catch(Exception e){
[Link]([Link]());
[Link]();
}

==============================================================================

36) What will be the output of following program?

public class Test {

/**
* @param args
*/
public static void main(String[] args) {

try{
int o[] = new int[2];
o[3]=23;
o[1]=33;
}catch(Exception e){
[Link]([Link]());
[Link]();
}

[Link](o[1]);
}

===============================================================================
37) What will be the output of following program?

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
int o[] = new int[2];

try{
o[3]=23;
o[1]=33;
}catch(Exception e){
[Link]([Link]());
[Link]();
}

[Link]("2nd pos --"+o[1]);


}

}
================================================================================

38) What will be the output of following program?

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
SomeClass obj=null;
try{
[Link]();
[Link]("success");
}catch(Exception e){
[Link]([Link]());
[Link]();
}

}
}
==================================================================================

39) What will be the output of following program?

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
divide(4,2);
divide(4,0);

}
public static int divide(int a,int b) throws Exception{
int result = a/b;
return result;
}
}

===================================================================================

40) What will be the output of following program?

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
int a=divide(4,2);
[Link](a);
int b=divide(4,0);

[Link](b);

}
public static int divide(int a,int b) throws Exception{
int result = a/b;
return result;
}
}

===================================================================================
==

41) What will be the output of following program?

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
int a=divide(4,2);
[Link](a);
int b=divide(4,0);

[Link](b);

}
public static int divide(int a,int b) {
int result = a/b;
return result;
}
}
===================================================================================
=====

42) What will be the output of following program?

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
int a=divide(4,2);
[Link](a);
int b=divide(4,0);

[Link](b);

}
public static int divide(int a,int b) {
int result=0;
try{
result = a/b;
}catch(Exception e){
[Link]();
}
return result;
}
}
===================================================================================
======

43) What will be the output of following program?

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
try{
int a=divide(4,2);
[Link](a);
int b=divide(4,0);
[Link](b);
}catch(Exception e){
[Link]("error");
}

}
public static int divide(int a,int b) {
int result=a/b;

return result;
}
}
===================================================================================
======

44) What will be the output of following program?

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
try{
int a=divide(4,2);
[Link](a);
int b=divide(4,0);
[Link](b);
}catch(Exception e){
[Link]("error 1");
}

}
public static int divide(int a,int b) {
int result=0;
try{
result=a/b;
}catch(Exception e){
[Link]("error 2");
}

return result;
}
}
===================================================================================
=======

45) What will be the output of following program?

public class Test {

/**
* @param args
*/
public static void main(String[] args) {

[Link]("A");
[Link](5000L);
[Link]("B");

}
==========================================================================

46) What will be the output of following program?

public class Test {

/**
* @param args
*/
public static void main(String[] args) {

throw new Exception("Some exception");

}
=========================================================================

47) What will be the output of following program?


public class Test {

/**
* @param args
*/
public static void main(String[] args) {
xyz();

public static void xyz() throws Exception{


throw new Exception("Some exception");

}
===========================================================================

48) What will be the output of following program?

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
try {
xyz();
} catch (Exception e) {
// TODO Auto-generated catch block
[Link]("error 1");
[Link]();
}

public static void xyz() throws Exception{


throw new Exception("Some exception");

}
==========================================================================

49) What will be the output of following program?

public class Test {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {

xyz();

}
public static void xyz() throws Exception{
throw new Exception("Some exception");

}
============================================================================

Common questions

Powered by AI

Java strictly enforces correct method and variable declarations. Using static variables within non-static contexts can cause compile errors—the method `non_static()` cannot be static with `static int b=1;`. Additionally, misuses like declaring `static int a` inside a method or using unauthorized access modifiers for local variables (like `public, protected, private`) trigger compilation errors .

In Java, direct recursive instantiation within a class definition of itself will cause runtime issues but not directly at the compile-time stage if just declaring fields. However, if a field is statically initialized with new instantiations like `Test t1 = new Test();` inside the class variable declarations, it can lead to a `StackOverflowError` at runtime due to endless self-instantiation attempts . Nevertheless, the source shows understanding around intricacies in class instantiation without specific elaboration on errors occurring .

When an object of a class that implements an interface and adds additional methods is used polymorphically based on the interface, only the methods declared in the interface are accessible. The additional method `meth4()` in class `B` cannot be called from the interface `A` reference `a1` . This demonstrates encapsulation and access limitations in polymorphism where, although `meth4` is defined in `B`, it's not accessible via `A` type reference during execution, thus prints only `meth1`, `meth2`, `meth3` .

When multiple instances of a class are created, each instance maintains its own copy of instance variables. Changes to the instance variables of one object don't affect others. Adjustments to `t1.j` and `t2.i` in a program result in `t1.j` becoming `5` and `t2.i` remaining `5` due to the use of post-increment on `t1.j` and pre-assignment decrement on `t2.i` .

Upon executing a program that involves static and instance variables, any change made to a static variable will be reflected across all instances, while changes to instance variables affect only the particular object. In the code, both `ob1.stc` and `ob2.stc` will print the new value after reassignment (`107`), while `ob1.var` and `ob2.var` maintain their respective values (`9` for `ob1`, `90` for `ob2`).

If a static method tries to directly access a non-static member, the compiler will generate an error because non-static fields cannot be accessed within a static context unless through an instance of the class. In the code, trying to print `i` using `System.out.println(i);` in a static method without creating an instance will result in a compilation error .

Java does not allow the usage of uninitialized local variables. Attempting to print an uninitialized local variable `i` in a `System.out.println` call will result in a compile-time error, because local variables must be explicitly initialized before use .

In a nested loop where `a[i][j] = i*j`, for a 10x5 array, the expression fills the array such that each element is the product of its indices. The result is, when printed, `a[0][0]` is 0, `a[1][3]` is 3, and `a[3][4]` is 12, reflecting the multiplication of indices .

In Java, primitive method parameters, like `i` in `Test(int i){ i=i; }`, are effectively local copies, so assigning them doesn't affect the class instance variables of the same name. Thus, the constructor assignment does not modify the class-level `i`, and its default value remains. Therefore, `System.out.println(t.i);` outputs `0`, the default integer initialization value .

The program initializes `x` to 0 and increments it by 1 in each loop iteration using the `increment` method. The loop prints the value of `x` after each increment, and breaks when `x` exceeds 10. Therefore, the output will be sequential Print statements: "Value of x is --1" to "Value of x is --11", and finally exits the loop .

You might also like