#include
int main() {
char str[]=`MalayalaM`;
char $\times$ s;
s= str + 8;
while(s>=str){
printf("%c", $\times$ s);
s--;
}
return 0;
}
The address of the 8th character from the base address of the string` is stored in the character pointer `s`.Here, `s` is pointing to the 8th value in the `str` i.e. `M`.
In the start of the loop, since the address at `s` is greater than the address at `str`, the condition evaluates to true. Then printf() prints the value at address at s` i.e. `M` and the value of `s` is decremented which will now point to the 7th value in the `str` i.e. `a`.
In the same way loop is executed and the characters `str` are printed in reverse order. When the address at `s` becomes less than the address at `str`, the loop is terminated. Hence, we get the output "MalayalaM".
int main() {
char a[]="Add";
int * j;
j = &a;
printf("%c\n", * j+2);
return 0;
}
Here, base address of array `a` is assigned to the integer pointer `j`. ` * j` denotes value at the base address of `j` i.e. the first character of string `a` i.e. `A`.
Since, the ASCII integer value of `A` is 65, the format specifier "%c" in the printf() function prints the ASCII character equivalent of 67(65+2) i.e. `C`.
Option B is correct because a method-local inner class can be abstract, although it means a subclass of the inner class must be created if the abstract class is to be used (so an abstract method-local inner class is probably not useful).
Option A is incorrect because a method-local inner class does not have to be declared final (although it is legal to do so).
Options C and D are incorrect because a method-local inner class cannot be made public (remember-you cannot mark any local variables as public), or static.
public class Switch2 {
final static short x = 2;
public static int y = 0;
public static void main(String [] args) {
for (int z=0; z < 4; z++) {
switch (z) {
case x:
System.out.print("0 ");
default:
System.out.print("def ");
case x-1:
System.out.print("1 ");
break;
case x-2:
System.out.print("2 ");
}}}}When z == 0, case x-2 is matched.
When z == 1, case x-1 is matched and then the break occurs.
When z == 2, case x, then default, then x-1 are all matched.
When z == 3, default, then x-1 are matched.
The rules for default are that it will fall through from above like any other case (for instance when z == 2), and that it will match when no other cases match (for instance when z==3).
class Base {
Base() {
System.out.print("Base");
}
}
public class Alpha extends Base {
public static void main(String[] args) {
new Alpha(); / * Line 12 * /
new Base(); / *Line 13 * /
}
}
Option B is correct. It would be correct if the code had compiled, and the subclass Alpha had been saved in its own file. In this case Java supplies an implicit call from the sub-class constructor to the no-args constructor of the super-class therefore line 12 causes Base to be output. Line 13 also causes Base to be output.
LinkedHashMap is the collection class used for caching purposes. FIFO is another way to indicate caching behavior. To retrieve LinkedHashMap elements in cached order, use the values() method and iterate over the resultant collection.
for(int i = 0; i < 3; i++) {
switch(i) {
case 0: break;
case 1: System.out.print("one ");
case 2: System.out.print("two ");
case 3: System.out.print("three ");
}
}
System.out.println("done");
The variable i will have the values 0, 1 and 2.
When i is 0, nothing will be printed because of the break in case 0.
When i is 1, "one two three" will be output because case 1, case 2 and case 3 will be executed (they don`t have break statements).
When i is 2, "two three" will be output because case 2 and case 3 will be executed (again no break statements).
Finally, when the for loop finishes "done" will be output.
int main(){
static int c=5;
printf("c=%d",c--);
if(c)
main();
return 0;
}
The storage class of `c` is `static` means it can`t be re-initialized. The printf() prints the value of `c`
i.e. 5 then post decrement operator decrements it to 4. The if condition is c i.e. 4. This evaluates to true and main() is called.
This time `c` is not re-initialized to 5 i.e. it is still 4 and the printf() prints the value 4.
In the same manner, value is printed till c=1 i.e. when `c` is decremented from 1 to 0, then if condition evaluates to false.