Premium

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



Question : :
int [] array = {1,2,3,4,5};
for (int i: array) {
if ( i < 2) {
keyword1 ;
}
System.out.println(i); if ( i == 3) {
keyword2 ;
}}

What should keyword1 and keyword2 be respectively, in oreder to produce output 2345?
 : :
1. continue, break
2. break, break
3. Access Mostly Uused Products by 50000+ Subscribers
4. continue, continue




Correct Answer : Get Lastest Questions and Answer :
Explanation:






Question :

package p1;
public interface DoInterface {
void method1(int n1); // line n1
}

package p3;
import p1.DoInterface;
public class DoClass implements DoInterface {
public DoClass(int p1) { }
public void method1(int p1) { } // line n2
private void method2(int p1) { } // line n3
}

public class Test {
public static void main(String[] args) {
DoInterface doi= new DoClass(100); // line n4
doi.method1(100);
doi.method2(100);
}
}
Which change will enable the code to compile?

 :
1. Adding the public modifier to the declaration of method1 at line n1
2. Removing the public modifier from the definition of method1 at line n2
3. Access Mostly Uused Products by 50000+ Subscribers
4. Changing the line n4 DoClass doi = new DoClass ( );



Correct Answer : Get Lastest Questions and Answer : Private members (both fields and methods) are only accessible inside the class they are
declared or inside inner classes. private keyword is one of four access modifier provided by
Java and its a most restrictive among all four e.g. public, default(package), protected and
private.





Question : Consider

Integer number = Integer.valueOff 808.1");

Which is true about the above statement?

 : Consider
1. The value of the variable number will be 808.1
2. The value of the variable number will be 808
3. Access Mostly Uused Products by 50000+ Subscribers
4. A NumberFormatException will be throw.
5. It will not compile



Correct Answer : Get Lastest Questions and Answer :
Explanation: The Integer class value of 0 returns an Integer from given string. But we need to pass string which has correct format for integer otherwise it will throw a
NumberFormatException. In this case we have passed string which is not an integer value (since what we passed is fractional number), so option D is correct.



Related Questions


Question : What is the proper way to defined a method that take two int values and returns their sum as an int value?

 : What is the proper way to defined a method that take two int values and returns their sum as an int value?
1. int sum(int first, int second) { first + second; }
2. int sum(int first, second) { return first + second; }
3. Access Mostly Uused Products by 50000+ Subscribers
4. int sum(int first, int second) { return first + second; }

5. void sum (int first, int second) { return first + second; }




Question : You have been given following code

public class MyClass {
public static void main(String[] args) {
String s = " Java Duke ";
int len = s.trim().length();
System.out.print(len);
}
}

What is the result?
 : You have been given following code
1. 8
2. 9
3. Access Mostly Uused Products by 50000+ Subscribers
4. 10
5. Compilation fails




Question : You have been given following code

public class SampleClass {
public static void main(String[] args) {
AnotherSampleClass asc = new AnotherSampleClass();
SampleClass sc = new SampleClass();
sc = asc;
System.out.println("sc: " + sc.getClass());
System.out.println("asc: " + asc.getClass());
}
}

class AnotherSampleClass extends SampleClass {
}

When you execute it, what will be printed ?


 : You have been given following code
1. sc: class Object asc: class AnotherSampleClass
2. sc: class SampleClass asc: class AnotherSampleClass
3. Access Mostly Uused Products by 50000+ Subscribers
4. sc: class AnotherSampleClass asc: class AnotherSampleClass





Question :You have been given following code

public class Student {
public String name = "";
public int age = 0;
public String major = "Undeclared";
public boolean fulltime = true;

public void display() {
System.out.println("Name: " + name + " Major: " + major);
}

public boolean isFullTime() {
return fulltime;
}
}
Which line of code initializes a student instance?

 :You have been given following code
1. Student student1;
2. Student student1 = Student.new();
3. Access Mostly Uused Products by 50000+ Subscribers
4. Student student1 = Student();





Question : Which of the following will print current time?

 : Which of the following will print current time?
1. System.out.print(new LocalTime()-now);
2. System.out.print(new LocalTime());
3. Access Mostly Uused Products by 50000+ Subscribers
4. System.ouLprint(LocalTime.today());
5. None of the above.






Question : Given the code fragment:
String name = "Spot";
int age = 4;
String str ="My dog " + name + " is " + age;
System.out.println(str);

And
StringBuilder sb = new StringBuilder();

Using StringBuilder, which code fragment is the best potion to build and print the following string
My dog Spot is 4

A. sb.append("My dog " + name + " is " + age);
System.out.println(sb);

B. sb.insert("My dog ").append( name + " is " + age);
System.out.println(sb);

C. sb.insert("My dog ").insert( name ).insert(" is " ).insert(age);
System.out.println(sb);

D. sb.append("My dog ").append( name ).append(" is " ).append(age);
System.out.println(sb);


 : Given the code fragment:
1. A,B
2. B,C
3. Access Mostly Uused Products by 50000+ Subscribers
4. A,D