Java OCA 8 Certification Exam: Practice Questions and Answers, Exams of Java Programming

A set of practice questions and answers to prepare for the java oca 8 certification exam. it covers various aspects of java programming, including compilation, execution, classpath, access modifiers, and error handling. The questions test understanding of core java concepts and problem-solving skills. This resource is valuable for students and professionals aiming to obtain the java oca 8 certification.

Typology: Exams

2024/2025

Available from 04/28/2025

Emmicole
Emmicole 🇺🇸

1

(1)

18K documents

1 / 34

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Oca 8 questions and answers
Consider following code snippet:
package com.skillcertpro.test;
public class Exam {
public static void main(String [] args) {
System.out.println("All the best!");
}
}
Location of Exam.java file:
D:.
????WORK
????QUIZ
????SEC07
????classes
????src
????com
????skillcertpro
????test
Exam.java
You are currently at Sec07 folder.
D:\WORK\Quiz\Sec07>
Which of the following javac command, typed from above location, will generate
Exam.class file structure under classes directory?
D:.
????WORK
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download Java OCA 8 Certification Exam: Practice Questions and Answers and more Exams Java Programming in PDF only on Docsity!

Consider following code snippet: package com.skillcertpro.test; public class Exam { public static void main(String [] args) { System.out.println("All the best!"); } } Location of Exam.java file: D:. ????WORK ????QUIZ ????SEC ????classes ????src ????com ????skillcertpro ????test Exam.java You are currently at Sec07 folder. D:\WORK\Quiz\Sec07> Which of the following javac command, typed from above location, will generate Exam.class file structure under classes directory? D:. ????WORK

????QUIZ

????SEC

????classes ? ????com ? ????skillcertpro ? ????test ? Exam.class ? ????src ????com ????skillcertpro ????test Exam.java - javac -d classes\ src\com\skillcertpro\test\Exam.java Use -d option with javac command. As you are typing javac command from within Sec07 directory, hence path of java file relative to Sec07 directory needs to be given. So, correct command is: javac -d classes\ src\com\skillcertpro\test\Exam.java Consider following code snippet: package com.skillcertpro.test; public class Exam { public static void main(String [] args) {

To execute Exam class from WORK folder, you should specify the classpath (Quiz
Sec07\classes) which contains whole path of the class(com\skillcertpro\test
Exam.class). Space After First path. And you should also use fully qualified name of the class, which is com.skillcertpro.test.Exam. Hence correct option is: java -cp Quiz\Sec07\classes\ com.skillcertpro.test.Exam What will be the result of compiling and executing Test class? java Test good morning everyone private class Test { public static void main(String args[]) { System.out.println(args[1]); } } - Compilation Error Top level class can have two access modifiers: public and default. Over here Test class has private modifier and hence compilation error. For the class Test, which options, if used to replace /INSERT/, will print "Hurrah! I passed..." on to the console? Select ALL that apply. package com.skillcertpro.oca; public class Test { /INSERT/ {

System.out.println("Hurrah! I passed..."); } } - static public void main(String [] args) public static void main(String [] a) As System.out.println needs to be executed on executing the Test class, this means special main method should replace /INSERT/. Special main method's name should be "main" (all characters in lower case), should be static, should have public access specifier and it accepts argument of String [] type. String [] argument can use any identifier name, even though in most of the cases you will see "args" is used. Position of static and public can be changed but return type must come just before the method name. Suppose you have created a java file, "MyClass.java". Which of the following commands will compile the java file? - javac MyClass.java Command to compile a java file: javac .java [.java extension is compulsory]. Command to execute a java class: java [.class extension should not be used]. Consider 3 files: //Order.java package orders; public class Order {

You need to import Order and Item classes. To import Order class, use either import orders.Order; OR import orders.; and to import Item class, use either import orders.items.Item; OR import orders.items.; Differences between Compilation error and Runtime error - compile time errors occur when the code is built, but the program fails to compile. In contrast, Java runtime errors occur when a program successfully compiles but fails to execute. If code doesn't compile, the program is entirely unable to execute. A run time error will only occur when the code is actually running. These are the most difficult - and lead to program crashes and bugs in your code which can be hard to track down. An example might be trying to convert a string: "hello" into an integer: string helloWorld = "hello"; int willThrowRuntimeError = Convert.ToInt32(helloWorld); The compiler may not see this as a problem but when run an error will be thrown. Compiler errors are due to inaccuracies in code, where the compiler throws an error to alert you to something which will not compile, and therefore cannot be run. An example of a compiler error would be: int = "this is not an int";

Consider below code: public class Test { public static void main(String[] args) { System.out.println("ONE"); } public static void main(Integer[] args) { System.out.println("TWO"); } public static void main(byte [] args) { System.out.println("THREE"); } } What will be the result if Test class is executed by below command? java Test 10 - ONE Like any other method, main method can also be overloaded. But main method called by JVM is always with String [] parameter. Don't get confused with 10 as it is passed as "10". Run above class with any command line arguments or 0 command line argument, output will always be ONE. Consider below code of main.java file: package main;

How many of the above statements is/are true? - Two statements Though given code looks strange but it is possible in java to provide same name to package, class (and constructor), variable and method. Above code compiles successfully and on execution prints ONE on to the console. Constructor is not invoked as 'new' keyword is not used and that is why TWO will not be printed to the console. In real world coding, you would not see such code and that is why it is a good question for the certification exam. Consider incomplete code of M.java file class M { } ________ class N { } Following options are available to fill the above blank:

  1. public
  2. private
  3. protected
  4. final
  5. abstract How many above options can be used to fill above blank (separately and not together) such that there is no compilation error? - Only two options

Top-level class can use only two access modifiers [public and default(don't specify anything)]. private and protected cannot be used. As file name is M.java, hence class N cannot be public. Top-level class can be final, hence it is a correct option. Top-level class can be abstract and hence it is also a correct option. Given code of Test.java file: class A { public static void main(String [] args) { System.out.println("A"); } } class B { public static void main(String [] args) { System.out.println("B"); } } class C { public static void main(String [] args) { System.out.println("C"); }

args[1] = "Day!"; System.out.println(args[0] + " " + args[1]); } } And the commands: javac Test.java java Test Good What is the result? - An exception is thrown at runtime public static void main(String[] args) method is invoked by JVM. Variable args is initialized and assigned with Program arguments. For example, java Test: args refers to String [] of size 0. java Test Hello: args refers to String [] of size 1 and 1st array element refers to "Hello" java Test 1 2 3: args refers to String [] of size 3 and 1st array element refers to "1", 2nd array element refers to "2" and 3rd array element refers to "3". Command used in this question: java Test Good, so args refers to String[] of size 1 and element at 0th index is "Good". args[1] = "Day!"; is trying to access 2nd array element at index 1, which is not available and hence an exception is thrown at runtime. What will be the result of compiling and executing Test class?

package com.skillcertpro.oca; public class Test { public static void main(String[] args) { System.out.println(1 + 2 + 3 + 4 + "Hello"); } } - 10Hello In The integer is First, it Follower The Math rules. In The First is String, Then it Adopt The String rules and Apply concatenation. System.out.println( "Hello"); As expression contains + operator only, which is left to right associative. Let us group the expression. 1 + 2 + 3 + 4 + "Hello" #VALUE! #VALUE! #VALUE! [Let us solve it now,] #VALUE! #VALUE! #VALUE! [+ operator with String behaves as concatenation operator.] = 10Hello Wrapper classes are defined in which of the following package? - java.lang

When printed on the console; byte, short, int & long prints 0, float & double print 0.0, boolean prints false and char prints nothing or non-printable character (whitespace). Reference type instance variables are initialized to null. Valid variables - For readability purpose underscore (_) is used to separate numeric values. This is very useful in representing big numbers such as credit card numbers (1234_7654_9876_0987). Multiple underscores are also allowed within the digits. Hence, int x = 5____0; compiles successfully and variable x stores 50. float f = 123.76_86f; compiles successfully. 1_2_3_4 is int literal 1234 and int can easily be assigned to double, hence double d = 1_2_3_4; compiles successfully. ____50 is a valid variable name, and as this variable is not available hence, int y = ____50; causes compilation error. Underscores must be available within the digits. For the statement int z = 50____; as underscores are used after the digits, hence it causes compilation error. Consider below code of Test.java file: package com.skillcertprokhattry.oca; public class Test { public static void main(String[] args) { char c1 = 'a'; //ASCII code of 'a' is 97 int i1 = c1; //Line n System.out.println(i1); //Line n

What is the result of compiling and executing Test class? - 97 Range of char data type is from 0 to 65535 and hence it can be easily assigned to int type. println() method is overloaded to accept char type and int type both. If char type value is passed, it prints char value and if int type value is passed, it prints int value. As i1 is of int type, hence corresponding int value, which is 97, is printed on to the console. What will be the result of compiling and executing Test class? package com.skillcertpro.oca; public class Test { public static void main(String[] args) { m(1); } private static void m(Object obj) { System.out.println("Object version"); } private static void m(Number obj) {

} - FTFFF

Boolean.valueOf(String s) returns true if passed String argument is not null and is equal, ignoring case, to the String "true". In all other cases it returns false. Boolean.valueOf("abc") => false. As "abc".equalsIgnoreCase("true") is false. Boolean.valueOf("TrUe") => true. As "TrUe".equalsIgnoreCase("true") is true. Boolean.valueOf("false") => false. As "false".equalsIgnoreCase("true") is false. Boolean.valueOf(null) => false. As passed argument is null. Boolean.valueOf("FALSE") => false. As "FALSE".equalsIgnoreCase("true") is false. What will be the result of compiling and executing Test class? package com.skillcertpro.oca; public class Test { public static void main(String[] args) { System.out.println(new Boolean("ture")); } } - False Boolean class code uses equalsIgnoreCase method to validate the passed String, so if passed String is "true" ('t', 'r', 'u' and 'e' can be in any case), then boolean value stored in Boolean object is true otherwise false.

In this question passed String is "ture" and not "true" and that is why false is printed on to the console. What will be the result of compiling and executing Test class? package com.skillcertpro.oca; public class Test { public static void main(String[] args) { Boolean b = new Boolean("tRUe"); switch(b) { case true: System.out.println("ONE"); case false: System.out.println("TWO"); default: System.out.println("THREE"); } } } - None of THE options switch can accept primitive types: byte, short, int, char; wrapper types: Byte, Short, Integer, Character; String and enums. switch(b) causes compilation failure as b is of Boolean type. What will be the result of compiling and executing Test class?