Java Assignment
Java Assignment
1. Question:
public class IncrementDecrementQuiz Output:
{
public static void main(String[] args)
{
int i = 11;
i = (i++) + (++i);
System.out.println(i);
}
}
Explanation:
In the above code, we have given an integer variable i which has an initial
value of 11.
i++ here stands for post-increment operator and ++i for pre-increment
operator. So in i++ it take the value as 11 and then increment to next value.
In ++i it take the value 12 and first makes an increment in I value to 13
Then add those value 11+13.
2. Question:
public class IncrementDecrementQuiz Output:
{
public static void main(String[] args)
{
int a=11, b=22, c;
c = a + b + a++ + b++ + ++a + ++b;
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
}
}
Explanation:
Here we three integer a=11,b=22,c as our variables.
So for finding the value of c we gave some pre and post increment operators.
a + b + a++ + b++ + ++a + ++b. so here first it took the value of a as 11 and b as 22.
Then we assign a post increment operator for both a and b but it still takes the
value as 11 and 22. After that we assign a pre-increment operator which now
takes the value of a as 13 and b as 24. Adding all those value we got our output.
3. Question:
public class JavaIncrementDecrementQuiz Output:
{
public static void main(String[] args)
{
int ch = 'A';
System.out.println("Char value is = " + ch++);
}
}
Explanation:
Here we declared an integer variable ch and assigned a value “A”.
And then printing the assigned value. But it prints only the numerical value of
the Character value “A” i.e 65.
4. Question:
public class IncrementDecrementQuiz Output:
{
public static void main(String[] args)
{
int m = 0, n = 0;
int p = --m * --n * n-- * m--;
System.out.println(p);
}
}
Explanation: Here we have two integer type variable m =0 and n = 0.
And we assign some operators to a new variable p.
In that we have a pre decrement operator so there for m and n we have -1
value for both. After that there is a post decrement operator which also took
the values as -1. So the value will return as 1.
5. Question:
public class explicitCasting{
public static void main(String[] args) Output:
{
double d = 100.04;
long l = (long)d;
int i = (int)l;
System.out.println("Double value " + d);
System.out.println("Long value " + l);
System.out.println("Int value " + i);
}
}
Explanation:
Here we have three variables of different data types such as: double, long, int.
After that, we just print all these variables taking the value from their above
datatypes.
In that, we convert the double data to long data and it will discard the
fractional part.
6. Question:
public class castingChar{ Output:
public static void main(String[] argv)
{
char ch = 'c';
int num = 88;
ch = (char) num;
System.out.print(+ch);
}
}
Explanation:
In this code, int is typecasted into char, so for printing the the value of ch we
add an extra datatype before num. so we can assign new value to ch.
7. Question:
class castingToByte{ Output:
public static void main(String args[])
{
byte b;
int i = 257;
double d = 323.142;
System.out.println("Conversion of int to byte.");
b = (byte)i;
System.out.println("i = " + i + " b = " + b);
System.out.println(
"\nConversion of double to byte.");
b = (byte)d;
System.out.println("d = " + d + " b= " + b);
}
}
Explanation:
In the given code we have
Variable b declared as byte.
i as integer as 257
d as double with value 323.142
Now the integer i is casted to byte
A byte in Java is an 8 bit signed integer which has a range of -128 to 127.
When we cast 257 to byte, it wraps around due to overflow which results in a
value of 1.
When a double is cast to byte,
The fractional part get cutoff and the integer part is then subjects to same
overflow. The integer part 323 exceeds the byte range, so it wrapped around a
value of 67.
8. Question
class Main { Output:
public static void main(String[] args) {
int num = 10;
System.out.println("The integer value: " + num);
double data = num;
System.out.println("The double value: " + data);
}
}
Explanation:
We have given an integer type variable of value 10.
In the first output case, we just print the value of the num variable.
Then in the next line, we assign the value of num to another variable data of
datatype double and it prints the fraction value of the num.
9. Question Output:
class Main {
public static void main(String[] args) {
double num = 10.99;
System.out.println("The double value: " + num);
int data = (int)num;
System.out.println("The integer value: " + data);
}
}
Explanation:
In the above code, we have a variable num of datatype double (which prints
the real number) and print it.
And in the next output line we assign the variable num to another variable
data of datatype integer and print it.
10. Question:
import java.io.*; Output:
public class typeConversion{
public static void main(String[] args)
{
long a = 3;
byte b = 2;
double c = 2.0;
int final_datatype = (int) (a + b + c);
System.out.print(final_datatype);
}
}
Explanation:
The provided java code has three variables:
Variable a as long type data.
Variable b as byte type data.
And variable c as double type data.
Now, b is promoted to long because a is long. Which results of a+b to long
datatype.
The long result is then promoted to double because c is double.
Since the above expression is in double, it cannot be directly assigned to an int
without explicit casting. So we add an extra int before the a+b+c so it casts the
double into integer.