Java Code Examples for Beginners
Java Code Examples for Beginners
`variableA` is initialized by `showOutput()`, which returns `variableB=15` at declaration. In Java, instance variables are set during declaration or in a constructor, with method-distributed assignment making `variableA=15` due to `variableB=15` assignment in `showOutput`, so it prints 15 .
The Condition class for-loop executes only once since the initial condition `z=3` is already true, and the loop decrements `z` but no check is repeated. Within the loop, `(--var2 > 1)` evaluates to true as `var2` decrements to `2.13`; `(var1++ > 2)` is also true. Therefore, the sum `var2=var1+var2` is executed, resulting in `var2=6.26`. Post-loop, `var1` is 4.13 (after increment) and final output is '4.13 6.26' .
In Java, numeric arrays like the `double value[]` are initialized with default values of `0.0`. Thus, when `System.out.println("Arra val contain" + value[0]);` is executed, it prints 'Arra val contain0.0' since `value[0]` was never assigned any other value, demonstrating Java's initialization mechanism for double arrays .
The `SwitchD` class executes case `1` matched with `i=1`, printing 'one', but lacks break statements, allowing fall-through. Thus, subsequent prints 'two' and 'default' are executed until the switch structure ends. Proper break statements per case would prevent this execution beyond the intended match .
When a new `Cafeteria` object is instantiated with a string, the constructor `Cafeteria(String text)` is invoked, setting the `cafe` attribute to 11. This overrides the default constructor for `Cafe`, which would set `cafe` to 9. Consequently, the printed value for the `cafe` attribute is 11, demonstrating constructor overloading in inheritance .
The first condition `(intx !=4)&& (intx >=99) || ! BVal1` is true as `intx` is 100, leading to 'Accenture' being printed, followed by 'IDC'. `BVal2=BVal1` sets `BVal2` to true. The condition `(BVal2=true) && BVal1 != BVal2` evaluates the assignment, not comparison, setting `BVal2` to true, making `BVal1 != BVal2` false, hence 'High Performance Delivered' is not printed .
The `MainClass` attempts to divide `i` by `j`, where `j=0`, leading to an `ArithmeticException`. However, only the generic `Exception` catch block is defined, capturing the arithmetic exception arising from division by zero. Therefore, 'i am exception' is printed, demonstrating the priority of catch blocks in exception handling .
The Vehicle interface incorrectly declares `int noOfWheels;`, trying to assign a property which is illogical as interfaces can declare methods with implicit public abstract functions, not variables. To fix this, change it to a method, e.g., `int getNoOfWheels();`, and implement this method in `Car` .
The Demo class contains a `while(i--)` loop that starts with `i=1`. However, the predecrement operation makes `i` immediately equal to 0 before the loop body is executed. Since the condition becomes false right at the beginning, the loop body is never executed, resulting in no output .
When the Calculator class executes, the output will be 'dou'. The method `add(double ddda)` is selected over `add(int addd)` and `add(float ddb)` because the parameter passed, `12.24`, is a `double` literal by default. Java chooses the most specific method available that can accept the parameter, and in this case, it is `add(double)` .


![public static void main(String s[])
{
System.out.println(new
Cafeteria("test").cafe);
}
}
public class Demo
{
public](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F326%2F569038774%2F3.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F569038774%2FDumps-Java-Programming&__type=image)

![interface Vehicle
{
int noOfWheels;
}
public class Car implements Vehicle
{
public static void main(String s[])
{
System.out](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F326%2F569038774%2F5.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F569038774%2FDumps-Java-Programming&__type=image)
![{
public static void main(String s[])
{
int
intx=100;
boolean BVal1=true;
boolean BVal2=false;
if( (intx !=4)&& (intx >=99)](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F326%2F569038774%2F6.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F569038774%2FDumps-Java-Programming&__type=image)
![public static void main(String
s[])
{
double value[]=new double
[81];
value[1]=21.23;
System.out.println("Arra val
conta](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F326%2F569038774%2F7.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F569038774%2FDumps-Java-Programming&__type=image)

![{
public static void main(String s[])
{
float area;
RoomArea room=new RoomArea();
room.getData(15,5);
area=room.length * room](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F326%2F569038774%2F9.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F569038774%2FDumps-Java-Programming&__type=image)
