0% found this document useful (0 votes)
4 views13 pages

Java Code Output and Error Analysis

This document contains examples of Java code snippets and their expected outputs. It provides 23 code examples covering topics like method overriding, inheritance, interfaces, constructors, static methods and variables. Each code example is accompanied by its expected output.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

Java Code Output and Error Analysis

This document contains examples of Java code snippets and their expected outputs. It provides 23 code examples covering topics like method overriding, inheritance, interfaces, constructors, static methods and variables. Each code example is accompanied by its expected output.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

12202040501019 Dharmik Rabadiya

Assignment 2
Q.1]Give the output for following java code if possible, Otherwise
indicate appropriate error. Correct the error and give the output

1]
Class demo
{
public static void main(String a[])
{
short s = 5;
s = (short)(s+1); // Adding an integer literal to a short
requires explicit casting
[Link](s);
}
}
// Output: 6

2]
class demo
{
public static void main(String a[])
{
int s = 258;
byte b ;
b = (byte)s; // Casting int to byte may result in loss of data
[Link]("s = "+s+"b = "+b);
}
}
// Output: s = 258 b = 2

3]
class demo
{
public static void main(String a[])
{
int s;
char c = 'A';
s = c; // Implicit narrowing conversion, char to int
[Link]("s = "+s);
}
}
// Output: s = 65

4]
abstract class demo{
public static void main(String a[])
{
[Link]("Demo program");
}
}
// Output: Demo program
12202040501019 Dharmik Rabadiya

5]
abstract class demo
{
abstract void test();
public static void main(String a[])
{
[Link]("main in abstract class");
}
}
class B extends demo
{
void test()
{
[Link]("test method ");
}
}
class C
{
public static void main(String a[])
{
[Link]("Demo programin main");
[Link](a);
B b = new B();
[Link]();
}
}
// Output:
// main in abstract class
// Demo programin main
// test method

7.
class A
{
int i;
String s;
double d;
A(int i,String s ,double d)
{
this.i = i; // Assign values to instance variables using this
keyword
this.s = s;
this.d = d;
}
void display()
{
[Link]("i = "+i+" s = "+s+" d = "+d); // Add spaces
in print statement
}
}
class demo
{
public static void main(String s[])
{
A a = new A(25,"first",3.45);
[Link]();
12202040501019 Dharmik Rabadiya

}
}
Output: i = 25 s = first d = 3.45

8]
interface B
{
void display();
}
class D implements B
{
public void display()
{
[Link]("class D interface B");
}
}
class E implements B
{
public void display()
{
[Link]("class E interface B");
}
}
class F
{
void display()
{
[Link]("class F");
}
}
class demo
{
public static void main(String a[])
{
B b;
b = new D();
[Link]();
b = new E();
[Link]();
// b= new F(); // Error: F cannot be converted to B
}
}
Output:class D interface B
class E interface B

9]
interface B
{
void display();
}

class D implements B
{
public void display()
12202040501019 Dharmik Rabadiya

{
[Link]("class D interface B");
}
}
class E implements B
{
public void display()
{
[Link]("class E interface B");
}
void show()
{
[Link]("class E");
}
}
class demo
{
public static void main(String a[])
{
B b;
b = new D();
[Link]();
b = new E();
[Link]();
// [Link](); // Error: show() method is not in interface B
}
}
Output:
class D interface B
class E interface B

10]
interface A
{
void display();
}

interface B
{
int display();
}

class D implements A
{
public void display()
{
[Link]("class D interface A");
}
}

class demo
{
public static void main(String a[])
{
D var = new D();
[Link]();
}
12202040501019 Dharmik Rabadiya

}
Output:
class D interface A

11]
interface A
{
void display();
}

interface B
{
int display(int a);
}

class D implements A
{
public void display()
{
[Link]("class D interface A");
}
public int display(int a)
{
[Link]("class D interface B and value of a = "+a);
return 0;
}
}

class demo
{
public static void main(String a[])
{
D var = new D();
[Link]();
[Link](25);
}
}
Output: class D interface A
class D interface B and value of a = 25

12]
interface A
{
void display();
}

class B implements A
{
public void display()
{
[Link]("class B interface A");
}
}
12202040501019 Dharmik Rabadiya

class C extends B implements A


{
public void display()
{
[Link]("class c interface A");
}
}

class demo
{
public static void main(String a[])
{
B var;
var = new B();
[Link]();

C var1;
var1 = new C();
[Link]();

var = var1;
[Link]();
}
}
Output:

class B interface A
class c interface A
class c interface A

13]
interface A
{
void display();
}

interface B extends A
{
void display();
void show();
}

class D implements A
{
public void display()
{
[Link]("class D interface A");
}
public void show()
{
[Link]("class D interface B derived from interface
A");
}
}
12202040501019 Dharmik Rabadiya

class demo
{
public static void main(String a[])
{
D var = new D();
[Link]();
[Link]();
}
}
Output:

class D interface A
class D interface B derived from interface A

14]
class A
{
static int x = 10;
static class B
{
public static void main(String ar[])
{
[Link](); // Call static method using class name
}
}
static void display()
{
[Link]("x = "+x);
}
}

// Execute class B;
Output:

x = 10

15]
class A
{
static int a = 10;
static String name = "gcet"; // Make name static
static void display() // Make method static
{
[Link]("a = "+a+" name = "+name);
}
}

class demo
{
public static void main(String a[])
{
[Link]();
12202040501019 Dharmik Rabadiya

}
}
Output:

a = 10 name = gcet

16]
class A
{
int x = 10;
class B
{
int y = 20; // Inner class variable
void show()
{
[Link]("inner class variable \n x = "+x+" y = " +
y);
}
}
void display()
{
[Link]("outer class variable \n x = "+x); // Access
outer class variable
}
}

class C
{
public static void main(String a[])
{
A obj = new A();
[Link]();
}
}
Output:

outer class variable


x = 10

17]
class A
{
String x;
int y;

A()
{
this("gcet",100); // Call constructor with parameters
}
A(String w,int z)
{
x = w;
12202040501019 Dharmik Rabadiya

y = z;
}

void display()
{
[Link](x+ "\n" + y );
}
}

class demo
{
public static void main(String a[])
{
A obj = new A();
[Link]();
}
}
Output:

gcet
100

18]
class A
{
int x,y;

A()
{
this(10,100);
}
A(int a, int b)
{
x = a;
y = b;
}
}

class B extends A
{
int x1, y1;

B()
{
super(); // Call default constructor of parent class
this(20,200);
}

B(int a, int b)
{
super(a, b); // Call parameterized constructor of parent class
x1 =a;
y1 = b;
}
12202040501019 Dharmik Rabadiya

void display()
{
[Link]("x = "+x+" y = "+y+"\n"+"x1 = "+x1+" y1=
"+y1);
}
}

class demo
{
public static void main(String a[])
{
B b= new B();
[Link]();
}
}
Output:

x = 10 y = 100
x1 = 20 y1= 200

19]
class A
{
public static void main(String a[])
{
char c = ' '; // Initialize char variable
boolean b = false; // Initialize boolean variable

[Link](c);
[Link](b);

}
}
Output:
false

20]
class A {
final int i = 5;

void display() {
for(i = 0; i < 5; i++) {
[Link](i);
}
}
}

class B {
public static void main(String a[]) {
A obj = new A();
[Link]();
}
}
12202040501019 Dharmik Rabadiya

Output:

Compilation Error: Cannot assign a value to final variable i

21]
class B {
B() {
[Link](2 + 2 + "result");
[Link]("result" + 2 + 2);
}
}

class A {
public static void main(String a[]) {
B b = new B();
}
}
Output:

4result
result22

22]
class A3 {
void Hello() {
[Link]("A3");
}

void Hello(int i) {
[Link]("Hello from A3" + i);
}
}

class B3 extends A3 {
void Hello() {
[Link]("B3");
}
}

class C3 extends B3 {
void Hello(String s) {
[Link]("Hello from C3" + s);
}
}

class MethodOverriding {
public static void main(String args[]) {
A3 obj = new C3();
[Link]();
}
}
Output:

Justification:
12202040501019 Dharmik Rabadiya

The method Hello() in class B3 overrides the method Hello() in class A3.
+

23]
class Parent {

class Child extends Parent {

class ObjectReferenceVariable {

public static void main (String args []) {


Parent P;
P = new Parent();
P = new Child();
Child C;
C = new Child();
C = new Parent();
}
}
Error:
Compilation Error: Incompatible types: Parent cannot be converted to
Child

24]
interface B {
void display();
}

class D1 {}

class D2 implements B {
public void display() {
[Link]("D2");
}
}

class D3 implements B {
public void display() {
[Link]("D3");
}
}

class InterfaceReference {
public static void main(String args[]) {
B b;
b = new D1(); // Error: D1 does not implement B interface
[Link]();
b = new D2();
[Link]();
}
}
Output:
12202040501019 Dharmik Rabadiya

Compilation Error: D1 cannot be converted to B

Q.2]
public class EmbeddedQuote {
public static void main(String[] args) {
[Link]("\"Information Technology\"");
}
}
Output:

"Information Technology"

Q.3]
Yes, a public static void main(String args[]) method of one class can be
called from another class.

Example:

class ClassA {
public static void main(String args[]) {
[Link]("Hello from ClassA");
}
}

class ClassB {
public static void main(String args[]) {
[Link](args); // Calling main method of ClassA
}
}

Q.4]
No, you cannot declare a one-dimensional array that includes both double
and float elements.

// Invalid declaration
double[] array = {1.5, 2.5f, 3.5};
12202040501019 Dharmik Rabadiya

Common questions

Powered by AI

In Java, constructors play a crucial role in initializing objects in derived classes. When a subclass constructor is executed, it implicitly calls the parent class's constructor using `super()`, ensuring the parent class is initialized before the subclass. This cascading mechanism allows each constructor in the hierarchy to prepare the object incrementally. For example, class `B` which extends class `A`, calls both its own constructors as well as its parent class's constructors to correctly initialize member variables like `x` and `y` inherited from `A`, before setting its own `x1` and `y1` .

Defining constructors with parameters in class hierarchies allows for precise control over object initialization, enabling the passing of initial values directly, enhancing object configuration. This practice prevents objects from being initialized in an inconsistent or undefined state by using `this` or `super` to specify constructor chaining. Parameterized constructors facilitate complex initial conditions and customization when objects of derived classes are created, ensuring consistency across class hierarchies as demonstrated in class `A` and `B` with their respective constructors .

Method overloading in Java is resolved at compile time based on the parameter lists. When a class hierarchy involves methods with different signatures, Java determines which method to call based on the argument types and order provided in the method call. Overloaded methods in different classes do not override each other; they maintain distinct bindings dependent on context and input parameters. When similar methods appear across a hierarchy, Java will utilize the subclass method when invoked on an object of the subclass type, as seen in the `Hello()` methods across `A3`, `B3`, and `C3` classes, where calls resolve based on the runtime type of the reference .

Creating an abstract class with a main method is beneficial when the abstract class needs to demonstrate or test static behaviors or functionalities without instantiation. It is useful for showcasing the execution order or to log initiation steps without using a derived class. This can serve as an initial demonstration while the abstract functionalities are yet to be implemented in subclasses. The main method in the abstract class can execute basic code as shown in `abstract class demo`, which prints a message without needing an instance .

In Java, when a class implements multiple interfaces with methods having the same name and different signatures, method overloading occurs, which is legally permissible. However, method ambiguity issues can arise if the interfaces have methods of the same name and signature definitions. The implementations must ensure each method variant is distinctly defined and handle possible conflicts or unintended overloading effects. While Java allows overloading within a class, semantic conflicts can lead to logical implementation challenges, requiring careful design planning .

In Java, when an integer literal is added to a short variable, the result is automatically promoted to an int. To store this result back into a short variable, explicit casting is necessary. For example, the expression `s = (short)(s+1)` casts the result of `s+1` back to a short, avoiding a compilation error. Without casting, there would be a type mismatch error since Java requires matching operand types without implicit narrowing conversion .

In Java, assigning a parent class reference to a child class variable leads to a compilation error because of type incompatibility. The type system ensures type safety by requiring explicit casting for conversions that can lead to runtime errors. Since a Parent object does not have the additional characteristics of a Child object, an implicit downward cast is unsafe; hence it results in a compilation error unless explicitly casted, potentially leading to a `ClassCastException` if improperly used .

Java interfaces facilitate polymorphism by allowing multiple classes to implement the same interface, thereby offering interchangeable instances that can call the same method signatures. This design enables varied class objects to be accessed through a common interface reference variable, invoking interface methods that can result in different execution flows depending on the object's specific class implementation. Polymorphism allows for flexible and extensible code. As seen in classes `D` and `E` implementing interface `B`, different method results occur while using the same `B` reference type .

A static nested class in Java is preferable in scenarios where the nested class logically belongs to the outer class but does not need access to the outer class's instance members or methods. It allows grouping of classes that are only used in one place, increasing encapsulation and improving maintainability. Additionally, static nested classes can be instantiated without an instance of the outer class, which is useful when the class encapsulates behavior that operates independently of the instance of the outer class. For instance, `class B` within `class A` allows calling static methods from the outer class contextually, without needing to instantiate `A` .

Casting an integer to a byte in Java can result in data loss because byte is an 8-bit signed integer with a range from -128 to 127, while int has a range of -2^31 to 2^31-1. When you cast an integer like 258 to a byte, it results in '2' because Java retains the lower 8 bits of the integer's binary representation, discarding the overflow bits, leading to a potentially unexpected value .

You might also like