Premium

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



Question : You have been given following code

public class Test {
static boolean bVar;

public static void main(String[] args) {
boolean bVar1 = true;
int count = 8;
do {
System.out.println("Hello Java! " + count);
if (count >= 7) {
bVar1 = false;
}
} while (bVar != bVar1 && count > 4);
count -= 2;
}
}

When you run it, what would be printed ?

  : You have been given following code
1. Hello Java! 8 Hello Java! 6 Hello Java! 4
2. Hello Java! 8 Hello Java! 6
3. Hello Java! 8
4. Compilation fails



Correct Answer : Get Lastest Questions and Answer :
Explanation:





Question : You have been given following code

public class TestLoop {
public static void main(String[] args) {
int array[] = { 0, 1, 2, 3, 4 };
int key = 3;
for (int pos = 0; pos < array.length; ++pos) {
if (array[pos] == key) {
break;
}
}
System.out.print("Found " + key + "at " + pos);
}
}

What is the result?

  : You have been given following code
1. Found 3 at 2
2. Found 3 at 3
3. Compilation fails
4. An exception is thrown at runtime



Correct Answer : Get Lastest Questions and Answer :
Explanation: The following line does not compile:
System.out.print("Found " + key + "at " + pos);
The variable pos is undefined at this line, as its scope is only valid in the for loop.
Any variables created inside of a loop are LoCAL To THE LooP






Question : A method doSomething () that has no exception handling code is modified to trail a method that throws a checked exception. Which two modifications, made independently,
will allow the program to compile?

A. Catch the exception in the method doSomething().
B. Declare the exception to be thrown in the doSomething() method signature.
C. Cast the exception to a RunTimeException in the doSomething() method.
D. Catch the exception in the method that calls doSomething().
 : A method doSomething () that has no exception handling code is modified to trail a method that throws a checked exception. Which two modifications, made independently,
1. A,B
2. B,C
3. C,D
4. A,D

5. A,C



Correct Answer : Get Lastest Questions and Answer :
Explanation: Valid Java programming language code must honor the Catch or Specify Requirement.
This means that code that might throw certain exceptions must be enclosed by either of the following:
- A try statement that catches the exception. The try must provide a handler for the exception, as described in Catching and Handling Exceptions.
- A method that specifies that it can throw the exception. The method must provide a throws clause that lists the exception, as described in Specifying the Exceptions Thrown by
Method. Code that fails to honor the Catch or Specify Requirement will not compile.







Related Questions


Question : You have been given below code

interface Pet { }
class Dog implements Pet { }
public class Beagle extends Dog{ }

Which three are valid?

A. Pet a = new Dog();
B. Pet b = new Pet();
C. Dog f = new Pet();
D. Dog d = new Beagle();
E. Pet e = new Beagle();
F. Beagle c = new Dog();

  : You have been given below code
1. A,B,C
2. C,D,E
3. Access Mostly Uused Products by 50000+ Subscribers
4. A,D,E
5. B,C,F




Question : Given the following classes:

public class ColorTest {
public static void main(String[] args) {
String[] colors = { "red", "blue", "green", "yellow", "maroon", "cyan" };
int count = 0;
for (String c : colors) {
if (count >= 4) {
break;
} else {
continue;
}
if (c.length() >= 4) {
colors[count] = c.substring(0, 3);
}
count++;
}
System.out.println(colors[count]);
}
}

What is the result?


  : Given the following classes:
1. Yellow
2. Maroon
3. Access Mostly Uused Products by 50000+ Subscribers
4. A StringIndexOutOfBoundsException is thrown at runtime.





Question : Which two actions will improve the encapsulation of a class?
A. Changing the access modifier of a field from public to private
B. Removing the public modifier from a class declaration
C. Changing the return type of a method to void
D. Returning a copy of the contents of an array or ArrayList instead of a direct reference

  : Which two actions will improve the encapsulation of a class?
1. A,B
2. B,C
3. Access Mostly Uused Products by 50000+ Subscribers
4. A,D






Question : You have been given following code.

public class Two {

public static void main(String[] args) {
try {
doStuff();
System.out.print("1");
} catch (Exception e) {
System.out.print("2");
}
}

public static void doStuff() {
if (Math.random() > 0.5)
throw new RuntimeException();
doMoreStuff();
System.out.print("3 ");
}

public static void doMoreStuff() {
System.out.print("4");
}
}


Which two are possible outputs?

A. 2
B. 4 3 1
C. 1
D. 1 2

  : You have been given following code.
1. A,B
2. B,C
3. Access Mostly Uused Products by 50000+ Subscribers
4. A,D
5. C,D




Question : You have been given below code

public class Two {

public static void main(String[] args) {
int day = 1;
switch (day) {
case "7": System.out.print("Uranus");
case "6": System.out.print("Saturn");
case "1": System.out.print("Mercury");
case "2": System.out.print("Venus");
case "3": System.out.print("Earth");
case "4": System.out.print("Mars");
case "5": System.out.print("Jupiter");
}
}
}

Which two modifications, made independently, enable the code to compile and run?
A. Adding a break statement after each print statement
B. Adding a default section within the switch code-block
C. Changing the string literals in each case label to integer
D. Changing the type of the variable day to String
E. Arranging the case labels in ascending order

  : You have been given below code
1. A,B
2. B,C
3. Access Mostly Uused Products by 50000+ Subscribers
4. A,E
5. A,C



Question : Which three are advantages of the Java exception mechanism?

A. Improves the program structure because the error handling code is separated from the normal program function
B. Provides a set of standard exceptions that covers all the possible errors
C. Improves the program structure because the programmer can choose where to handle exceptions
D. Improves the program structure because exceptions must be handled in the method in which they occurred
E. allows the creation of new exceptions that are tailored to the particular program being


  : Which three are advantages of the Java exception mechanism?
1. A,B,C
2. B,C,D
3. Access Mostly Uused Products by 50000+ Subscribers
4. A,C,E
5. A,D,E