Premium

Oracle Java Programming Certification Questions and Answers (Dumps and Practice Questions)



Question : Given the code fragment: What is the result?

public static void main(String args[]){
int ii=0;
int jj=7;
for(ii=0; ii System.out.print(ii + " ");
}

  : Given the code fragment: What is the result?
1. 2 4
2. 0 2 4 6
3. 0 2 4
4. Compilation Fails



Correct Answer : 3
Explanation: As you can see we need to iterate jj-1 = 6 , Hence till value of ii is less than 6 iterate . While iterating ii value jumps by 2 in each iteration.
So it will first print 0 and increment by 2 , hence next it will print 2 and increment by 2 , which make ii value to 4 and print 4.







Question : Given: What is the result?
  : Given: What is the result?
1. ns = 50 s =125
ns = 125 s = 125
ns = 100 s = 125
2. ns = 50 s =125
ns = 125 s = 125
ns = 0 s = 125
3. ns = 50 s =50
ns = 125 s = 125
ns = 100 s = 100
4. ns = 50 s =50
ns = 125 s = 125
ns = 0 s = 125




Correct Answer : 2
Explanation: Here "s" is a static value. Hence, all instances will point to same variable. Hence, s is assigned with value 125 while second object created (Alpha 125).
Now when you call print method it will call 125 for each static reference. However, in case of Alpha (100) object if clause is not passed hence, non-static variable ns will not be
initialized and remain 0. Which will print values as below.


ns = 50 s =125
ns = 125 s = 125
ns = 0 s = 125






Question : Given the code from the Greeting.Java file:

public class Greeting {
public static void main(String args[]){
System.out.println("Hello " + args[0]);
}
}

Which set of commands prints Hello Duke in the console?

  : Given the code from the Greeting.Java file:
1. javac Greeting
java Greeting Duke
2. javac Greeting.java Duke
java Greeting
3. javac Greeting.java
java Greeting Duke
4. javac Greeting.java
java Greeting.class Duke




Correct Answer : 3
Explanation: Java compiler explicitly need .java extension, hence 1st option is out.
Regarding option 2 , we don't need argument during compilation (javac), hence this option is out.
Regarding option 3 : javac is compiler with .java extension and during runtime argument is accepted , hence this option is correct.




Related Questions


Question : Given the code fragment:

String shirts[][] = new String[2][2];
shirts[0][0]="red";
shirts[0][1]="blue";
shirts[1][0]="small";
shirts[1][1]="medium";

Which code fragment prints red: blue: small: medium?


 :  Given the code fragment:
1. A

2. B
3. C

4. D



Question :


A. this.amount = 0;
B. amount = 0;
C. acct (0) ;
D. acct.amount = 0;
E. acct. getAmount () = 0;
F. acct.changeAmount(0);
G. acct.changeAmount(-acct.amount);
H. acct.changeAmount(-acct.getAmount());


 :
1. A,B,C
2. A,C,D
3. D,G,H
4. E,F,G
5. A,E,F




Question : Given the code fragment from three files:
Which code fragment, when inserted at line 2 (in third file), enables the code to compile?


package sales;
public class SalesMan { }

package sales.products;
public class Product { }

package market;
//insert code here
public class USMarket {
SalesMan sm;
Product p;
}
 :  Given the code fragment from three files:
1. import sales.*;

2. import java.sales.products.*;
3. import sales;
import sales.products;
4. import sales.*;
import sales.products.*;



Question : : Which statement best describes encapsulation?
 :  : Which statement best describes encapsulation?
1. Encapsulation ensures that classes can be designed so that only certain fields and methods of an object are accessible from other objects.
2. Encapsulation ensures that classes can be designed so that their methods are inheritable.
3. Encapsulation ensures that classes can be designed with some fields and methods declared as abstract.
4. Encapsulation ensures that classes can be designed so that if a method has an argument MyType x, any subclass of MyType can be passed to that method.



Question : Given the code fragment:

int a[] = {1,2,3,4,5};

for(XXX){
System.out.print(a[e]);
}

Which option can replace xxx to enable the code to print 24?

 :   Given the code fragment:
1. int e = 0; e<=4; e++
2. int e = 1; e<5; e+=2
3. int e = 1; e<=5; e+=1
4. int e = 1; e<5; e+=2




Question : Given:

And given code fragment :

DVD dvd = new DVD(10,20)

Which code fragment should you use at line n1 to instantiate the dvd object successfully?
 :  Given:
1. super.r =r;
this.c = c;
2. super(r);
this(c);
3. super(r);
this.c = c;

4. this.c=r;
super(c);