• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

exam qs- object orientation- help!

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please explain, thanks!
1.What will be the result of attempting to run the following program?
public class Qaa75 {
public static void main(String args[]) {
String[][][] arr = {
{ {}, null },
{ { "1", "2" }, { "1", null, "3" } },
{},
{ { "1", null } }
};
System.out.println(arr.length + arr[1][2].length);
}
}
A]Program will terminate withArrayOutOfBounds Exception
B]Program will terminate withNullPointer Exception
C]4 printed to output
D]6 printed to output
E]7 printed to standard output

2.Which code fragments will succeed in printing the last argument given on the command line to the standard output, and exit gracefully with no output if no arguments are given?
CODE FRAGMENT A:
public static void main(String args[]) {
if (args.length != 0)
System.out.println(args[args.length-1]);
}
CODE FRAGMENT B:
public static void main(String args[]) {
try { System.out.println(args[args.length]); }
catch (ArrayIndexOutOfBoundsException e) {}
}
CODE FRAGMENT C:
public static void main(String args[]) {
int ix = args.length;
String last = args[ix];
if (ix != 0) System.out.println(last);
}
CODE FRAGMENT D:
public static void main(String args[]) {
int ix = args.length-1;
if (ix > 0) System.out.println(args[ix]);
}
CODE FRAGMENT E:
public static void main(String args[]) {
try { System.out.println(args[args.length-1]); }
catch (NullPointerException e) {}

3. What will be written to the standard output when the following program is run?
class Base {
int i;
Base() {
add(1);
}
void add(int v) {
i += v;
}
void print() {
System.out.println(i);
}
}
class Extension extends Base {
Extension() {
add(2);
}
void add(int v) {
i += v*2;
}
}
public class Qd073 {
public static void main(String args[]) {
bogo(new Extension());
}
static void bogo(Base b) {
b.add(8);
b.print();
}
}
A]9
B]18
C]20
D]21
E]22

4. interface I {
void setValue(int val);
int getValue();
}
DEFINITION A:
(a)class A extends I {
int value;
void setValue(int val) { value = val; }
int getValue() { return value; }
}
DEFINITION B:
(b)interface B extends I {
void increment();
}
DEFINITION C:
(c)abstract class C implements I {
int getValue() { return 0; }
abstract void increment();
}
DEFINITION D:
(d)interface D implements I {
void increment();
}
DEFINITION E:
(e)class E implements I {
int value;
public void setValue(int val) { value = val; }
}

5. Given the following code, which method declarations, when inserted at the indicated position, will not cause the program to fail compilation?
public class Qdd1f {
public long sum(long a, long b) { return a + b; }
// insert new method declaration here
}
A]public int sum(int a, int b) {return a + b;}
B]public int sum(long a, long b) { return 0;}
C]abstract int sum();
D]private long sum(long a , long b) { return a + b; }
E]public long sum(long a, int b) {return a + b; }

6. What will be written to the standard output when the following program is run?
public class Qcb90 {
int a;
int b;
public void f() {
a = 0;
b = 0;
int[] c = { 0 };
g(b, c);
System.out.println(a + " " + b + " " + c[0] + " ");
}
public void g(int b, int[] c) {
a = 1;
b = 1;
c[0] = 1;
}
public static void main(String args[]) {
Qcb90 obj = new Qcb90();
obj.f();
}
}
A]000
B]001
C]010
D]100
E]101
 
Enthuware Software Support
Posts: 4897
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whops..looks like you've got hold of a mock exam without explanations!
Trying 3 things will help you the most:
1. Looking into the API
2. Compiling the code.
3. After compiling, it doesn't take much time to run it!
If you still need help (in fact most of the time you'll not), posting one topic per thread will probably get you better explanations.
-Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These all are Qs from Mughal's book and you will find a neat explanation of these Qs overe there.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any where exactly IS over there???
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic