• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

mock exam

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone tell me which is the answer,

What gets printed when the following code is compiled and run with the following arguments -
java test 2
Select the one correct answers.

public class test() {
public static void main(String args[]) {
Integer intObj = Integer.valueOf(args[args.length - 1]);
int i = intObj.intValue();
if(args.length > 1)
System.out.println(i);
if(args.length > 0)
System.out.println(i - 1);
else
System.out.println(i - 2);
}
}
a. test
b. test-1
c. 0
d. 1
e. 2.
thanks in advance

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 gets printed...Do you see anything fishy here ?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source:
1 public class test
2 {
3 public static void main(String args[])
4 {
5 Integer intObj = Integer.valueOf(args[args.length - 1]);
6 int i = intObj.intValue();
7 if(args.length > 1)
8 System.out.println(i);
9 if(args.length > 0)
10 System.out.println(i - 1);
11 else
12 System.out.println(i - 2);
13 }
14 }
Answer: out put will be 1
why:
first you correct your sentex error at line 1 i.e.
you typed public class test() insted public class test.
() are extra,becareful
line 5args.length will return 1
args[1-1]=args[0] is 2 (your argument to test)
2 will store in intobj
6so value of i will be 2
7 if(args.length > 1) will return false because args.length is 1
8by pass, no printing here
9 if(args.length > 1) will return true,printing is 1 . (i-1=2-1=1)
if you have still any confusion, please feel free to contact again
regards
Imran Jamil Malik
 
please buy this thing and then I get a fat cut of the action:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic