Q1. What is the output of the following code snippet?
char ch = 'A';
System.out.print(ch);
ch++;
System.out.print(ch);
++ch;
System.out.println(ch);
A1.
ABC
Q2. What is the output of the following code snippet?
char ch = 'D';
ch += 6;
System.out.println("ch: " + ch);
A2.
ch : J
Q3. What is the output of the following code snippet?
char ch = 'D';
ch = ch + 6;
System.out.println("ch: " + ch);
A3. The code snippet will NOT compile. The ch + 6 results in an integer, which cannot be assigned to the LHS datatype of char, without a typecast.
Q4. What is the output of the following code snippet?
char ch = 'D';
ch = (char)(ch + 6);
System.out.println("ch: " + ch);
A4.
ch: J
Q5. What is the output of the following code snippet?
int i;
char ch;
for(i = 65; i <= 70; i++)
{
ch = i;
System.out.print(ch);
}
System.out.println();
A5. The code snippet will NOT compile. The statement “ch = i;” results in an int being assigned to a char, which requires an explicit typecast. The compile will show an error “possible loss of precision”.
Q6. What is the output of the following code snippet?
for(i = 65; i <= 70; i++)
{
ch = (char)i;
System.out.print(ch);
}
System.out.println();
A6.
ABCDEF
Q7. What is the output of the following code snippet?
int i;
char ch;
for(i = 65; i <= 70; i++)
{
ch = (char)i++;
System.out.print(ch);
}
System.out.println();
A7.
ACE
Q8. What is the output of the following code snippet?
int i, count;
char ch = 'A';
for(count = 1; count <= 5; count+=2)
{
i = ch + count;
System.out.println("count: " + count + ", i: " + i);
}
A8.
count: 1, i: 66
count: 3, i: 68
count: 5, i: 70
Q9. What is the output of the following code snippet?
int i, count;
char ch1 = 'A', ch2 = 'a';
for(count = 1; count <= 5; count+=2)
{
i = ch1 + ch2;
System.out.println("ch1, ch2, i: " + ch1 + ", " + ch2 + ", " + i);
ch1++;
ch2++;
}
A9.
ch1, ch2, i: A, a, 162
ch1, ch2, i: B, b, 164
ch1, ch2, i: C, c, 166
Explanation: char addition means the integer numbers are added. The characters ‘A’ and ‘a’ are 65 and 97, which give 162. In subsequent iterations of the loop, we add 1 to each of ch1 and ch2, and hence the sum changes by 2 in each loop; also the characters change from A to B to C, and a to b to c as well.
Q10. What is the output of the following code snippet?
String s = "hello";
for(int i=0; i < s.length(); i++)
{
char ch = s.charAt(i);
char chNew = (char)(ch + i + 1);
System.out.print( chNew );
}
A10.
igopt
char ch = 'A';
System.out.print(ch);
ch++;
System.out.print(ch);
++ch;
System.out.println(ch);
A1.
ABC
Q2. What is the output of the following code snippet?
char ch = 'D';
ch += 6;
System.out.println("ch: " + ch);
A2.
ch : J
Q3. What is the output of the following code snippet?
char ch = 'D';
ch = ch + 6;
System.out.println("ch: " + ch);
A3. The code snippet will NOT compile. The ch + 6 results in an integer, which cannot be assigned to the LHS datatype of char, without a typecast.
Q4. What is the output of the following code snippet?
char ch = 'D';
ch = (char)(ch + 6);
System.out.println("ch: " + ch);
A4.
ch: J
Q5. What is the output of the following code snippet?
int i;
char ch;
for(i = 65; i <= 70; i++)
{
ch = i;
System.out.print(ch);
}
System.out.println();
A5. The code snippet will NOT compile. The statement “ch = i;” results in an int being assigned to a char, which requires an explicit typecast. The compile will show an error “possible loss of precision”.
Q6. What is the output of the following code snippet?
for(i = 65; i <= 70; i++)
{
ch = (char)i;
System.out.print(ch);
}
System.out.println();
A6.
ABCDEF
Q7. What is the output of the following code snippet?
int i;
char ch;
for(i = 65; i <= 70; i++)
{
ch = (char)i++;
System.out.print(ch);
}
System.out.println();
A7.
ACE
Q8. What is the output of the following code snippet?
int i, count;
char ch = 'A';
for(count = 1; count <= 5; count+=2)
{
i = ch + count;
System.out.println("count: " + count + ", i: " + i);
}
A8.
count: 1, i: 66
count: 3, i: 68
count: 5, i: 70
Q9. What is the output of the following code snippet?
int i, count;
char ch1 = 'A', ch2 = 'a';
for(count = 1; count <= 5; count+=2)
{
i = ch1 + ch2;
System.out.println("ch1, ch2, i: " + ch1 + ", " + ch2 + ", " + i);
ch1++;
ch2++;
}
A9.
ch1, ch2, i: A, a, 162
ch1, ch2, i: B, b, 164
ch1, ch2, i: C, c, 166
Explanation: char addition means the integer numbers are added. The characters ‘A’ and ‘a’ are 65 and 97, which give 162. In subsequent iterations of the loop, we add 1 to each of ch1 and ch2, and hence the sum changes by 2 in each loop; also the characters change from A to B to C, and a to b to c as well.
Q10. What is the output of the following code snippet?
String s = "hello";
for(int i=0; i < s.length(); i++)
{
char ch = s.charAt(i);
char chNew = (char)(ch + i + 1);
System.out.print( chNew );
}
A10.
igopt