class Test
{
public static void main(String [ ] args)
{
if (args.length == 1) |
(args[1].equals("debug"))
{
System.out.println(args[0]);
}
else
{
System.out.println("Release");
}
}
}
Explanation:
An interesting and important difference: - in C++/BASH/etc. args[0] contains the name and the path to the program running the main() (in this case "Test")
- in Java args[0] is actually the first parameter to the program (in this case "debug").
So a run-time java.lang.Array_Index_Out_Of_Bounds_Exception is thrown for the code above.
The "|" and ";" operators are checking both their operands, unlike the short circuit operators "||" and ";;" which try to be shorter by checking their right operand ONLY in case the first was not enough to determine the result.
To correct the code replace:
- "|" with ";;"
- args[1] with args[0]
then the output will be
"debug".
public static void main(String[] args) {
String entries[] =
{"entry1","entry2"};
int count=0;
while (entries [count++]!=null){
System.out.println(count);
}
System.out.println(count);
}
Expalnation:
An ArrayIndexOutOfBoundsException would be thrown
when count ==2
===================================================================================
public static void main(String[] args) {
int x = 5, y;
while (++x < 7) {
y = 2;
}
System.out.println(x + y);
}
Explanation:
Before using local variables we must
initialize with default values.
------------------------------------------------------
public static void main(String[]
args) {
int x = 5, y=0;
while (++x < 7) {
y = 2;
}
System.out.println(x + y);
}
o/p: 7 + 2 = 9;
--------------------------------------------------------
public static void main(String[]
args) {
int x ;
System.out.println("Java
Champ");
}
o/p:
code compiles fine and print Java Champ
A. void methoda();
B. public double methoda();
C. public final double methoda();
D. static void methoda(double d1);
E. protected void methoda(double d1);
Answer: A, B
Given:
11. Float f = new Float("12");
12. switch (f) {
13. case 12: System.out.printIn("Twelve");
14. case 0: System.out.printIn("Zero");
15. default: System.out.printIn("Default");
16. }
What is the result?
A. Zero
B. Twelve
C. Default
D. Twelve
Zero
Default
E. Compilation fails.
Answer: E
Given:
1. public class X {
2. public static void main(String [] args) {
3. try {
4. badMethod();
5. System.out.print("A");
6. }
7. catch (Exception ex) {
8. System.out.print("B");
9. }
10. finally {
11. System.out.print("C");
12. }
13. System.out.print("D");
14. }
15. public static void badMethod() {
16. throw new RuntimeException();
17. }
18. }
What is the result?
A. AB
B. BC
C. ABC
D. BCD
E. Compilation fails.
Answer: D
Given:
10. public Object m() {
11. Object o = new Float(3.14F);
12. Object [] oa = new Object[1];
13. oa[0] = o;
14. o = null;
15. oa[0] = null;
16. return 0;
17. }
When is the Float object, created in line 11, eligible for garbage collection?
A. Just after line 13.
B. Just after line 14.
C. Just after line 15.
D. Just after line 16 (that is, as the method returns).
Answer: B
interface
TestA
{
String toString();
}
public class
InterfaceExamples {
/**
* @param
args
*/
public static void
main(String[] args) {
//
TODO Auto-generated method stub
System.out.println(new
TestA()
{
public
String toString()
{
return "test";
}
});
}
}
o/p:: test.
public class
RunnableTest {
public static void
main(String[] args) {
//
TODO Auto-generated method stub
Runnable r= new
Runnable()
{
@Override
public void
run() {
//
TODO Auto-generated method stub
System.out.println("cat");
}
};
Thread t= new
Thread(r)
{
public void
run() {
//
TODO Auto-generated method stub
System.out.println("Dog");
}
};
t.start();
}
}
o/p:: DOG
No comments:
Post a Comment
Thank you Keep Sharing ur Knowledge.