TÀI LIỆU ÔN THI FE - PRO192
TÀI LIỆU ÔN THI FE - PRO192
KIẾN THỨC
Kế thừa
Cha = new Con
Con = new Con
Con = (Cha)xx
Cú pháp của Tính kế thừa trong Java
Tính kế thừa trong Java - Từ khóa extends và implements trong Java | 155 bài học Java
miễn phí hay nhất (vietjack.com)
List .size
List ms có compare
Wrapper thuộc java.lang.Number
Boxing và unboxing là cách chuyển đổi giữa kiểu dữ liệu nguyên thủy
(primitive) và kiểu dữ liệu đối tượng (object) trong Java.
Boxing là quá trình chuyển đổi giá trị của một kiểu dữ liệu nguyên thủy
thành một đối tượng tương ứng của kiểu dữ liệu đối tượng tương ứng. Ví
dụ, để boxing một giá trị int thành một đối tượng Integer, chúng ta có
thể sử dụng cú pháp sau:
java.lang.Comparable
The max() method of Java Collections class is used to get the maximum element of the given
collection, according to the natural ordering of its elements.
CÂU HỎI
Access modifier
AC - vì default là cùng package
Shape and Main are defined in the same package, examine the code and select the correct
statements. (select 2)
line1> class Shape {
line2> public void print() {
line3> System.out.println("Shape");
line4> }
line5> }
line6> class Main {
line7> public static void main(String... args) {
line8> Shape anShape = new Shape(); line9> anShape.print();
line10> }
line11> }
A. The class Main will print Shape
B. If the code on line 2 is changed as follows, the class Main will print Shape:
private void print() {
C. If the code on line 2 is changed as follows, the class Main will print Shape:
void print() {
D. If the code on line 2 is changed as follows, the class Main will print Shape:
default void print() {
===============
B
In situations where there are no implementations to inherit, an interface is often a better
choice than an abstract class. This is because a class can implement multiple interfaces but
can only inherit from a single abstract class. Additionally, interfaces allow for greater
flexibility in designing a system because they allow classes to be interchangeable as long as
they implement the same interface.
A signed data type has an equal number of non-zero positive and negative values available
====> False. The range of negative numbers is greater by 1 than the range of positive numbers
phải là {
https://youtu.be/sMBZ1mMqRRM
https://www.careerride.com/view/placement-papers-on-java-set-2-13345.aspx
B
C The variables declared in the initialization step must all have the same
data type
B
BC
analyze the byte values to look for patterns that are indicative of text or
binary data.
C
C
https://www.geeksforgeeks.org/datainputstream-readbyte-method-in-java-with-examples/
Acd
a-z digits 0-9, and two special characters such as _ underscore and
$ dollar sign.
assignment operators.
Parrot
https://www.geeksforgeeks.org/file-class-in-java/
The statement B is wrong. The File class in Java provides several methods
to access data in a file, such as reading data from a file, writing data to a
file, checking file permissions, etc. Therefore, the statement "File class
C ( input)
http://www.java2s.com/ref/java/java-oca-ocp-practice-question-520.html
A (chứa object)
Stack chứa khuôn class
Heap là một vùng nhớ trong bộ nhớ được sử dụng để lưu trữ các
đối tượng khi từ khóa new được gọi ra, các biến static và các biến
toàn cục (biến instance).
BD
Trong java luôn là pass by value
Đã thử & A C
D
byte: The byte data type is an 8-bit signed two's complement
integer. It has a minimum value of -128 and a maximum value of
127 (inclusive).
A
B
A
A123
ép kiểu obj từ kiểu Object sang kiểu int[], ta sử dụng cú pháp (int[])obj. Việc
ép kiểu này chỉ được thực hiện khi kiểu thực tế của obj là một mảng kiểu
nguyên (int[]). Nếu obj không phải là một mảng kiểu nguyên, thì việc ép kiểu
này sẽ gây ra lỗi ClassCastException.
3. You have been given a design document for a veterinary registration system for
implementation in Java. It states:
"A pet has an owner, a registration date, and a vaccination-due date. A cat is a pet
that has a flag indicating whether it has been neutered, and a textual description of
its markings."
Given that the Pet class has already been defined, which of the following fields
would be appropriate for inclusion in the Cat class as members? (Choose all that
apply.)
A. Pet thePet;
B. Date registered;
C. Date vaccinationDue;
D. Cat theCat;
E. boolean neutered;
F. String markings;
EF
C
A
B
B
C
A
AC
C
http://www.java2s.com/ref/java/java-oca-ocp-practice-question-76.html
d
Hợp lí khi nào? A C ( b=m, d=m) —> large = small
B
ClassCastException vì w là kiểu thing, w là 1 cái interface → cat, 2 cái kh
liên quan gì
B
A
–A compound statement (also known as a block) is a sequence of zero or
more statements enclosed in curly braces {}. The curly braces define a
new scope for variables declared within the block.
CE – viết hoa nên là object, object thì có null
import java.io.*;
class Base {
public void amethod() throws FileNotFoundException {
}
}
@Override
public void amethod() {
}
public ExcepDemo() {
try {
DataInputStream din = new DataInputStream(System.in);
System.out.println("Pausing");
din.readByte();
System.out.print("Continuing");
this.amethod();
} catch (IOException ioe) {
}
}
}
A. Compile time error caused by protected constructor
B. Compile and run with output of "Pausing" and "Continuing" after a key is hit
C. Compile time error caused by amethod not declaring Exception
D. Runtime error caused by amethod not declaring Exception
99 100 102
intern() là một phương thức của lớp String trong Java, được sử dụng để lấy một tham chiếu
đến chuỗi trong bộ nhớ heap (heap memory) của Java String Pool.
String a = "XYZ";
String b = new String("XYZ");
if (a == b) {
System.out.print(" a==b, ");
} else {
System.out.print(" a # b, ");
}
String c = b.intern();
if (a == c) {
System.out.println(" a == c");
} else {
System.out.println(" a # c");
}
}
}
B – đệ quy
D – super
C — mấy cái khác là tuần tự
D
2: 101010
>>:
>> là dịch giữ dấu, >>> là dịch không giữ dấu (nhiều > hơn thì dứt khoát hơn)
Zero-fill bit (hay còn gọi là unsigned right shift hay logical right shift) là một
phép dịch bit trên một số nguyên không giữ dấu, nghĩa là các bit 0 sẽ được
thêm vào bên trái của số sau khi dịch.
Both primitives and object references can be both converted and cast
( chuẩn)
http://www.java2s.com/ref/java/java-oca-ocp-practice-question-97.html
Int chuyển sang double & ngược lại
int
Float là ép, string là convert
A
http://www.java2s.com/ref/java/java-oca-ocp-practice-question-528.html
C
12
public class NewClass {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("dest");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeInt(3);
dos.writeFloat(0.0001f);
dos.close();
fos.close();
}
catch (IOException e) { }
}
}
C – points out
}
Select an operation which may cause an unchecked exception.
Bc
Statement 1: A protected member of a class can be accessed from any class in the
same package and from a subclass that is outside the package.
Statement 2: A member of a class that is declared private can be accessed only
within the class but nowhere outside the class.
Choose the correct answer?
Select one:
a. Statement 1 is FALSE, Statement 2 is TRUE
In his book, Herbert Schildt says in page 172 (3rd paragraph) that "protected applies
only when inheritance is involved.".
In page 228, Table 9-1 shows that a protected member can be accessed from a non-sub
class within the same package.
01 Given:
11. public static void parse(String str) {
12. try {
13. float f = Float.parseFloat(str);
14. } catch (NumberFormatException nfe) {
15. f = 0;
16. } finally {
17. System.out.println(f);
18. }
19. }
20. public static void main(String[] args) {
21. parse("invalid");
22. }
What is the result?
A. 0.0
B. Compilation fails.
C. A ParseException is thrown by the parse method at runtime.
D. A NumberFormatException is thrown by the parse method at runtime.
Answer: B – chưa khởi tạo f =))
B
D – vector vs Array
class Up {
public Up(String s) {
System.out.print("B");
}
}
⇒ false false
Nếu bạn override phương thức equals trong lớp Animal để so sánh
giá trị của instance variable id thay vì so sánh địa chỉ vùng nhớ của hai
đối tượng thì kết quả sẽ trả về là true khi so sánh a1.equals(a2)
trong đoạn mã trên.
===> CC
https://www.geeksforgeeks.org/vector-vs-arraylist-java/
ArrayList is fast because it is non-synchronized. Vector is slow because it is
synchronized. Vector is synchronized, which means only one thread at a time
can access the code, while ArrayList is not synchronized, which means
multiple threads can work on ArrayList at the same time.
Question 15)
What will be output if you try to compile and run the following code, but
there is no file called Hello.txt in the current directory?.
import java.io.*;
public class Mine {
public static void main(String argv[]){
Mine m=new Mine();
System.out.println(m.amethod());
}
public int amethod() {
try {
FileInputStream dis=new FileInputStream("Hello.txt");
}catch (FileNotFoundException fne) {
System.out.println("No such file found");
return -1;
}catch(IOException ioe) {
} finally{
System.out.println("Doing finally");
}
return 0;
}
}
3) https://www.erpgreat.com/java/java-programmer-certification-mock-exam-no-1.htm
(a) super(void);
(b) superclass.();
(c) super.A();
(d) super();
=== D
Read more on Sarthaks.com - https://www.sarthaks.com/2434062/which-of-these-
correct-way-of-calling-constructor-having-parameters-superclass-subclass
The following lists the complete contents of the file named Derived.java
AD
https://docs.oracle.com/javase/tutorial/java/generics/why.html
The package provides some of the most useful Java classes that are
frequently needed in all types of applications.
A. java.lang
B. java.util
C. java.pack
D. java.io
B
Đa hình – F
True, false
False, true
False, false
True, true
True true
D (does not)
http://www.java2s.com/ref/java/java-oca-ocp-practice-question-981.html
garbage collection does not guarantee that a program will not run out of
memory, because there are some situations where objects can still be
referenced and therefore not eligible for garbage collection, even though
the program no longer needs them. This can cause memory leaks and
eventually lead to an OutOfMemoryError.
Option A is incorrect because although System.gc() can suggest garbage
collection, it does not guarantee that garbage collection will occur
immediately. In fact, it is up to the JVM to determine whether or not to
perform garbage collection in response to a call to System.gc(). Option B is
incorrect because setting all references to an object to new values does
not guarantee that the garbage collector will actually collect the object, and
it is generally not a good practice to rely on this approach. Option C is
incorrect because although garbage collection can help prevent memory
leaks and manage memory usage, it cannot guarantee that a program will
not run out of memory under all circumstances.
A. Compiler error at line (1).
B. No compiler error and no exception.
C. An exception is thrown at line (2).
D. An exception is thrown at line (1).
The provided code has a syntax error in the constructor of class A. The
parameter int ag is incorrectly named as age, which is the same name
as the instance variable. This causes a compilation error because the
compiler cannot distinguish between the two and determine which one is
being referenced.
C
The ideal class to implement for the given programming problem is Set
because it is designed to store a collection of unique values and provides
efficient methods for testing membership, adding, and removing elements.
Since we want to store unique part ID numbers, we can use a Set to ensure
that each ID is only stored once.
Additionally, if we want the list to be in sorted order, we can use a
SortedSet implementation, such as TreeSet, which maintains its
elements in sorted order according to their natural ordering or a specified
comparator. Note that a SortedSet also satisfies all the requirements of
a Set.
A. No output "
Trong đoạn code này, phương thức place() chỉ được gọi nếu biểu thức
(b1 == true) || place(true) trả về giá trị true. Tuy nhiên, vì biến
b1 được khởi tạo với giá trị true, biểu thức (b1 == true) sẽ trả về giá trị
true và phương thức place() không cần được gọi để đánh giá biểu
thức. Điều này có thể dẫn đến việc phương thức place() không được gọi,
dẫn đến việc không in ra chuỗi "Borcetshire".
BC
D sai vì nó chả ảnh hưởng gì cả
java.lang.NullPointerException
What happens when you try to compile and run the following application?
10. import java.io.*
11 public class Main [
12. public static void main(String argv[]) {
13 try
14
File d = new File("dir
15. Filet new File(d,"fi.xt").
18 (!exists()) { 17 LcreateNewFile().
18 21.
19 }catch (IOException e) { 20 e.printStackTrace()
22 1 231
A. Line 17 is never executed.
B. Line 17 throws an exception at runtime.
C. Line 15 throws an exception at runtime.
D. Line 17 creates a directory named "dir" and a file "fi.txt" within it
E. Line 15 creates a directory named 'dir" and a file "fi.txt" within it
BD
D long không được vì nó to hơn int
A – không có break
23
Trong trường hợp của câu lệnh if, sử dụng toán tử & hoặc toán tử &&
không có sự khác biệt về kết quả, vì biểu thức điều kiện (x==4 & !b2) và
(x==4 && !b2) đều sẽ được đánh giá như nhau. Tuy nhiên, sử dụng toán
tử && sẽ có hiệu suất cao hơn trong một số trường hợp, vì nó sẽ ngắt việc
kiểm tra toán hạng bên phải nếu toán hạng bên trái đã là false.
B
Truyền vào thì kh thay đổi gì, i++ thì in trước cộng sausau
When a negative long is cast to a byte, what are the possible values of the result
| all the above
A đó
Vì kí tự thì nó xem là 1 số nguyên
| output: int version
import java.io.*;
public class Main {
public static void main(String argv[]) {
try {
File d = new File("dir");
File f = new File(d, "fi.txt");
if (!f.exists())
// d.mkdirs();
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
False ( super)
A
Array.length không có ()
Bc
an inner class may be defined as static
an inner class may extend another class
Lỗi vì ở i
Output 2
B
Bc
C
http://www.java2s.com/ref/java/java-oca-ocp-practice-question-569.html
C - allall
B
A
A – abstract thì không có thânthân
class Base{
protected int i = 99;
}
public class Ab{
private int i = 1;
public static void main (String argv[]){
Ab a = new Ab();
a.hallow();
}
abstact void hallow(){
System.out.println("Claines "+i);
}
B — A chứ
AB
A
bd
C for sure
class A {
static int N = 10;
static String S = "Hello";
int x = 5;
static {
N = 7;
S = "GO";
}
void print() {
S = "TO";
System.out.println(S + N + x);
}
}
class Main {
public static void main(String[] args) {
System.out.println(A.S);
A a = new A();
a.print();
}
}
A vì tên filefile
CC
C – stream streamstream
B all nhé
D nhé
1010
1001
→ 1011 = 1111
D
D
C not B
method of Object class is used to obtain class of an object at run time - Class
getClass()
methods is used to check for infinitely large and small values - isInfinite()
Generics add stability to your code by making more of your bugs detectable at
compile time
methods of Character wrapper can be used to obtain the char value contained
in Character object.
methods of Character wrapper can be used to obtain the char value contained
in Character object. - getValue()
InputStream và OutputStream trong Java đọc và ghi dữ liệu theo byte, chứ
không phải theo character. Điều này là do các lớp này được thiết kế để
hoạt động với các luồng dữ liệu nhị phân (binary streams) thay vì các
luồng dữ liệu văn bản (text streams).
Tuy nhiên, trong Java, bạn có thể sử dụng các lớp bọc (wrapper
classes) như InputStreamReader và OutputStreamWriter để đọc và ghi
dữ liệu theo character.
https://docs.oracle.com/javase/8/docs/api/java/io/ObjectInputStream.html
http://www.java2s.com/ref/java/java-oca-ocp-practice-question-10.html
(Choose 2 answers)
Which of the following methods can be legally inserted in place of the comment
//Method Here ? (select two)
class Base{
public void amethod(int i) {
}
public class Scope extends Base{
public static void main(String argv[]){
//Method Here
A. void amethod(int i) throws Exception {}
B. void amethod(long i) throws Exception {)
C. public void amethod(int i) throws Exception {}
D. void amethod(long i){}
If all three top-level elements occur in a source file, they must appear in which order?
A. Imports, package declarations, classes/interfaces/enums
B. Classes/interfaces/enums, imports, package declarations
C. Package declaration must come first, order for imports and class/interfaces/enum definiti
significant
D. Package declaration, imports, class/interface/enum definitions.
E. Imports must come first; order for package declarations and class/interface/enum definitic
significant
A low-level input stream reads data and returns it in bytes, and a low-level output stream
accepts data as bytes and writes the output in bytes.
https://tranthianhthi.files.wordpress.com/2018/01/06_i0_trong_java.pdf
Select the order of access More on Classes from least restrictive to most restrictive.
A. public, private, protected, default
B. default, protected, private, public
C. public, default, protected, private
D. default, public, protected, private
E. public, protected, default, private
Java OCA OCP Practice Question 1154 (java2s.com)
An object is
A. what classes are instantiated from
B. an instance of a class
C. a reference to an attribute
D. a variable
E. a concept of Object Oriented Programming
Consider the following code:
1. Dog rover, fido;
2. Animal anim;
3.
4. rover = new Dog();
5. anim = rover,
6. fido = (Dog)anim;
Where:
Mammal extends Animal Dog extends Mammal
Which of the following statements is true?
A. Line 5 will not compile.
B. Line 6 will not compile.
C. The code will compile but will throw an exception at line 6.
D. The code will compile and run.
E. The code will compile and run, but the cast in line 6 is not required and can be eliminated
Which of these stream contains the classes which can work on character stream?
A. InputStream
B. OutputStream
C. Character Stream
D. Byte Stream
Java.io Character Streams Questions & Answers - Sanfoundry
B
B
How can you change the current working directory using an instance of the File class called
A. FileName.chdir("DirName")
B. FileName.cd("DirName")
C. FileName.cwd("DirName")
D. The File class does not support directly changing the current directory.
"A pet has an owner, a registration date, and a vaccination-due date. A cat is a pet that has
a flag indicating if it has been neutered, and a textual description of its markings."
Given that the Pet class has already been defined and you expect the Cat class to be used
freely throughout the application, how would you make the opening declaration of the Cat
class, up to but not including the first opening brace? Use only these words and spaces:
boolean, Cat, class, Date, extends, Object, Owner, Pet, private, protected, public, String.
A. protected class Cat extends Owner
B. public class Cat extends Object
C. public class Cat extends Pet
D. private class Cat extends Pet
You have been given a design document for a veterinary registration system for
implementation in Java. It states
"A pet has an owner, a registration date, and a vaccination-due date. A cat is a pet that has
a flag indicating whether it has been neutered, and a textual description of its markings."
Given that the Pet class has already been defined, which of the following fields would be
appropriate for inclusion in the Cat class as members? (Choose all that apply.)
A. Pet thePet;
B. Date registered;
C. Date vaccinationDue;
D. Cat theCat;
E. boolean neutered;
F. String markings;
public class Test{
public static void main(String[] args){
String s1 = "xyz";
String s2 = "xyz";
if (s1 == s2)
System.out.println("Line 4");
if (s1.equals(s2)) System.out.println("Line 6");
}
}
What is the output?
A. Line 4
Line 6
B. Line 4
C. Line 6
D. No output, compile error
E. No output
https://youtu.be/cFXbWRlTTF8
public class Test{
public static void main(String[] args){
Object ob1= new Object();
Object ob2= ob1;
if(ob1.equals(ob2)) System.out.println("ob1 equals ob2");
if(ob1==ob2) System.out.println("ob1==ob2"); System.out.println("Have a nice day!");
}
}
What is the output?
A. ob1 equals ob2
ob1==ob2
Have a nice day!
B. ob1 equals ob2
Have a nice day!
C. ob1==ob2
Have a nice day!
D. Have a nice day!
https://youtu.be/2cgLTZfmy44
A
What will be the result when you attempt to compile and run the
following code?.
public class Conv{
public static void main(String argv[]){
Conv c=new Conv();
String s=new String("ello");
c.amethod(s);
}
public void amethod(String s){
char c='H';
c+=s;
System.out.println(c);
}
}
A. Compilation and output the string "Hello"
B. Compilation and output the string "ello"
C. Compilation and output the string elloH
D. Compile time error
String không cộng trực tiếp đc vs char
https://youtu.be/q17a_oZbAI0
What is the range of values that can be assigned to a variable of type short?
A. Depends on the underlying hardware
B. 0 through 2^16-1
C. 0 through 2^32 - 1
D. -2^15 through 2^15-1
E. -2^31 through 2^31-1
All objects belonging to the same class have the same characteristics and possible actions
A. TRUE
B. FALSE
Class chỉ là khuôn mẫu thôi nên, các object cùng class vẫn có thể có các tính chất khác
nhau tùy theo người dùng định nghĩa
class Dogl{
String mat;
String mui;
Suppose prime is an int and wrapped is an Integer. Which of the following are legal Java stal
A. prime = wrapped;
B. wrapped = prime;
C. prime = new Integer(9);
D. wrapped = 9;
E. All the others
Given:
55. int []x= {1, 2,3,4, 5};
56. int y []=x;
57. System.out.println(y[2]);
Which is true?
A. Line 57 will print the value 2.
B. Line 57 will print the value 3.
C. Compilation will fail because of an error in line 55.
D. Compilation will fail because of an error in line 56
###2
Which of the following declarations is INCORRECT?
A. int[] ar = new int(3);
B. double d = 1.3E + 21;
C. double d = 1.3D;
D. String s = "Hi!";
String s = "Hi!";
int[] ar = new int(3);
int[] ar = new int[3];
double d =1.3E+21;
double d =1.3D;
OOP
Inheritance
Polymorphism
The ability of a programming language to process objects differently depending on their type
is
a. Inheritance
b. Overloading
c. Polymorphism
d. Abstraction
e. Encapsulation
f. Overriding
Encapsulation
____ is the process of identifying and grouping attributes and actions related to a particular
entity as relevant to the application at hand
A. Persistence
B. Construction
C. Polymorphism
D. Data abstraction
E. Inheritance
F. Encapsulation
=====================
A. TRUE
B. FALSE
Is it possible to define a class called Thing so that the following method can return true under
certain circumstances?
boolean weird(Thing s) {
Integer x = new Integer(5);
return s.equals(x);
}
A. Yes
B. No
Dùng toString
The _______ escape sequence brings the cursor to the beginning of the current line.
A. \n
B. \r
C. \t
D. \\
E. \b
F. Which sequence depends on the particular implementation of the Java Virtual
Machine
Java OCA OCP Practice Question 66 (java2s.com)
This question concerns the following class definition:
1. package one
2
3. public class Bird (
4 protected static int k = 0;
5. public Bird() (k++; }
6. protected void fly() {}
7. static int getValue() { return(k);)
8.)
Which statement is true about class Bird and the following class Grebe?
1. package two,
2.
3. class Parrot extends one Bird (
4. Parrot() (k++)
5.
6. public static void main(String args[]) (
7. System.out.print("Before: " + k);
8. Parrot x = new Parrot();
9. System.out.println(" After: " + k):
10. x.fly):
11.}
12.}
A. The program will compile and execute. The output will be Before: 0 After: 2
B. The program will compile and execute. The output will be Before: 0 After: 1.
C. Compilation of Parrot will fail at line 4 because static members cannot be
overridden
D. Compilation of Parrot will fail at line 10 because method fly() is protected in the
superclas
###3
package one;
public Bird() {
k++;
}
package two;
Parrot() {
k++;
}
Which of the following modifiers does not allow a variable to be modified its value once it was
initialized.
A. final
B. static
C. private
D. transient – lasting for only a short time; temporary:
Question:Execute the following code in an empty directory. What is the result?
B. A new directory called dirname is created in the current working directory. A new file called
C. A new directory called dirname and a new file called filename are created, both in the
Answer:E is correct. Constructing an instance of the File class has no effect on the local file
system.
https://www.java-samples.com/showtutorial.php?tutorialid=715
Given the following class definition, which of the following statements would be legal
after the comment //Here (select two)
class InOut{
String s= new String("Between");
public void amethod(final int iArgs){
int iam;
class Bicycle{
public void sayHello(){
//Here
}
}//end of bicycle class
}//end of amethod
public void another(){
int iOther;
}
}
A. System.out.println(s);
B. System.out.println(iOther); //
C. System.out.println(iam); // chưa khởi tạo
D. System.out.println(iArgs);
public class InOut {
(Choose 1 answer) Which of the following may legally appear as the new type
(between the parentheses) in a cast operation?
A. Primitives
B. All of the others
C. Abstract classes
D. Final classes
Int x = (int)2.0
Which of the following statements is true?
A. An abstract class may be instantiated.
B. An abstract class must contain at least one abstract method.
C. An abstract class must contain at least one abstract data field.
D. An abstract class must declare that it implements an interface.
E. An abstract class may not have any abstract method
Java OCA OCP Practice Question 73 (java2s.com)
An abstract class
A. can contain all concrete methods.
B. must contain at least one abstract method.
C. must contain all abstract method.
D. can not contain any data field
If you need a Set implementation that provides value-ordered iteration, which class
should you use?
A. HashSet
B. LinkedHashSet
C. TreeSet
D. None of them
Answers to Questions and Exercises: (The Java™ Tutorials > Collections >
Implementations) (oracle.com)
Which one statement is true about the following code fragment? (choose 1)
1. import java.lang.Math;
2. Math myMath = new Math();
3. System.out.println("cosine of 0.123 = " + myMath.cos( 0.123 ) );
B
Math thì không thể extend,
Math là private inteface nên nó cũng không new được
Given:
11. public abstract class Shape {
12. int x;
13. int y;
14. public abstract void draw();
15. public void setAnchor(int x, int y) {
16. this.x = x;
17. this.y = y;
18. }
19.}
and a class Circle that extends and fully implements the Shape class.
Which is correct?
A. Shape s = new Shape(); s.setAnchor (10,10); s.draw();
B. Circle c = new Shape(); c.setAnchor (10,10); c.draw();
C. Shape s = new Circle(); s.setAnchor (10,10); s.draw();
D. Circle c = new Circle(); c.Shape.setAnchor (10,10); c.Shape.draw();
###5
What interfaces can be implemented in order to create a class that can be serialized?
A. No interfaces need to be implemented. All classes can be serialized.
B. implements java.io.Serializable. There are no methods in the interface.
C. implements java.io.Serializable, which defines two methods: readObject and writeObject.
D. implements java.io.Externalizable, which defines two methods: readObject and
writeObject.
E. implements java.io.Externalizable, which defines two methods: readExternal and
writeExternal
http://www.java2s.com/ref/java/java-oca-ocp-practice-question-169.html
Suppose a source file contains a large number of import statements. How do the imports
affect the time required to compile the source file?
a. Compilation takes no additional time.
b. Compilation takes slightly more time.
c. Compilation takes significantly more time.
d. Compilation takes slightly less time.
Java OCA OCP Practice Question 1001 (java2s.com)
Java OCA OCP Practice Question 1241 (java2s.com) – Class loading takes no
additional time.
#PRO192 SPRING 2023 ca 16h10
Given:
20. public class CreditCard {
22. private String cardID;
23. private Integer limit;
24. public String ownerName;
26. public void setCardInformation(String cardID,
27. String ownerName,
28. Integer limit) {
29. this.cardID = cardID;
30. this.ownerName = ownerName;
31. this.limit = limit;
32. }
33. }
Which is true?
A. The class is fully encapsulated.
B. The code demonstrates polymorphism.
C. The ownerName variable breaks encapsulation.
D. The cardID and limit variables break polymorphism
What will happen when you attempt to compile and run this code?
class Base{
public final void amethod(){
System.out.println("amethod");
}
}
public class Fin extends Base{
public static void main(String argv[]){
Base b = new Base();
b.amethod();
}
}
A. Compile time error indicating that a class with any final methods must be declared final its
B. Compile time error indicating that you cannot inherit from a class with final methods
C. Run time error indicating that Base is not defined as final
D. Success in compilation and output of "amethod" at run time.
hand side
Which of the following may appear on the left-hand side of an instanceof operator?
A. A reference
B. A class
C. An interface
D. A variable of primitive type
Which of the following may appear on the right-hand side of an instanceof operator?
(Choose all that apply.)
A. A reference
B. A class
C. An interface
D. A variable of primitive type
E. The name of a primitive type
Given:
1. public interface A {
2. String DEFAULT_GREETING = "Hello World";
3. public void method1();
4.}
A programmer wants to create an interface called B that has A as its parent. Which interface
correct?
A. public interface B extends A {}
B. public interface B implements A {}
C. public interface B instanceOf A {}
D. public interface B inheritsFrom A {}
DURGA SOFTWARE SOLUTIONS
In order to use the TreeSet class, the class that describes elements must implement the..... i
A. java.lang.Comparable
B. java.lang.Comparing
C. java.util.Comparable
D. java.util.treeset.Compare
public Person(){
this("Peter");
System.out.print("first ");
}
Given:
11. public interface Status {
12./* insert code here */ int MY_VALUE = 10;
13.}
Which three are valid on line 12? (Select three)
A. final
B. static
C. native
D. public
E. protected
F. abstract
B (đã làm)
C
Select the correct statement:
A. An object reference cannot be cast to an interface reference
B. An object reference can be cast to an interface reference when the object implement all
methods of the referenced interface.
C. An object reference can be cast to an interface reference when the object implements the
referenced interface.
D. An object reference can always be cast to an interface reference.
are methods which are in the same class and have the same name but different parar
A. Overloaded methods
B. Overridden methods
C. Override able methods
D. Overload able methods
E. Shared methods
F. Shadowing methods
Method overloading: overloading means two or more methods in one class have the same
method name but different parameters.
https://www.quora.com/Are-methods-with-the-same-name-in-different-classes-and-different-
signature-overloading-Java-OOP-overloading-development
An/A ___ declaration contains signatures, but no implementations, for a set of me also contain
constant definitions.
A. class
B. object
C. data type
D. abstract class
E. interface
A (đã làm)
D (đã làm)
B C (đã làm)
What will happen if you try to compile and run the following code?
public class Q {
public class Q {
What will be the result of attempting to compile and run the following code?
abstract class MineBase {
abstract void amethod();
static int i;
}
public class Mine extends MineBase {
public static void main(String argv[]){
int[] ar=new int[5];
for(i=0;i < ar.length;i++)
}
}
A. a sequence of 5 0's will be printed
B. Error: ar is used before it is initialized
C. Error Mine must be declared abstract
D. IndexOutOfBoundes Error
=============
What is the output of the program below ?
A. ABC1
Exception
finally 1
last1
B. ABC1
ABC2
ABC3
Exception
class Base {
Base() {
System.out.println("Base");
}
}
A. Lines 5 and 12 will not compile because the method names and return types are missing.
B. Line 12 will not compile because you can only have one static initializer.
C. The code compiles and execution produces the output x = 10.
D. The code compiles and execution produces the output x = 15
E. The code compiles and execution produces the output x = 3
In order for objects in a List to be sorted, those objects must implement which
interface and method?
a. Comparable interface and its compare method
b. Comparable interface and its compareTo method
c. Comparable interface and its equals method
d. Compare interface and its compareTo method
Given the following code, what will be the outcome?
10. public class Funcs extends java.lang. Math {
11. public int add(int x, int y) {
12. return x + y;
13. }
14. public int sub(int x, int y) {
15. return x - y;
16. }
17. public static void main(String[] a) {
18. Funcs f = new Funcs();
19. System.out.println("" + f.add(1, 2) + " " + f.sub(3,4));
20. }
21.}
A. The code compiles but does not output anything.
B. "3-1" is printed out to the console.
C. The line 10 causes compile-time error.
D. "3 1" is printed out to the console.
The process of identifying common features of objects and methods is
A. Polymorphism
B. Inheritance
C. Encapsulation
D. Abstraction
E. Overloading
F. Overriding
D (đã làm)
Suppose the declared type of x is a class, and the declared type of y is an interface.
When is the assignment x = y; legal?
● A. When the type of x is Object
● B. When the type of x is an array
● C. Always
● D. Never
http://www.java2s.com/ref/java/java-oca-ocp-practice-question-240.html
A. It always returns 0.
B. It always returns 1.
C. It always an int where every bit is 1.
D. The returned value varies depending on the argument
a. Inheritance
b. Overloading
c. Polymorphism
d. Abstraction
e. Encapsulation
f. Overriding
B (đã làm)
How do you use the File class to list the contents of a directory?
A. String contents = myFile.list();
B. File contents = myFile.list();
C. StringBuilder contents = myFile.list();
D. The File class does not provide a way to list the contents of a directory.
Java OCA OCP Practice Question 548 (java2s.com)
What happens when you try to compile and run the following program?
import java.util.*;
public class Main{
public static void main(String argv[]){
Vector<Integer> t = new Vector<Integer>();
t.add(12);
t.add(2);
t.add(5);
t.add(2);
Iterator<Integer> i = t.iterator();
int sum=0;
while(i.hasNext()) sum += i.next();
System.out.println(sum);
}
}
A. The program will print out: 19
B. The program will print out: 21
C. The program will print out: 17
D. The program has a compile error
import java.util.*;
D (đã làm)
After the following code fragment ran, what is the value of variable a?
String s;
int a;
s = "Foolish boy.";
a = s.indexOf("fool");
A. -1
B. 0
C. 4
D. random value
E. 1
import java.util.*;
What will be the result when you attempt to compile this program?
An instance of the java.util.Scanner class can read data from the keyboard (1), a file
(2), a string of characters (3).
(1)is ............ ,(2) is.............. ,and (3) is................
A. true, true, true
B. true, true, false
C. true, false, true
D. None of the others.
E. true, false, false
B (đã làm)
B.
int j=0;
do {
System.out.println("j=" + j++);
if(j==3) continue loop;
} while (j<10);
C.
int i = 3;
while (i) {
System.out.println("i is " + i);
}
D.
while (int i<7) {
i++;
System.out.println("i is " + i);
}
—
B (đã làm)
When is it appropriate to pass a cause to an exception's constructor?
● A. Always
● B. When the exception is being thrown in response to catching of a different
exception type
● C. When the exception is being thrown from a public method
● D. When the exception is being thrown from a private method
http://www.java2s.com/ref/java/java-oca-ocp-practice-question-473.html
B, do khi check i.intValue() nó sẽ check khi i đang null, nên throw nullpointer exception
Given the following code
import java.io.*;
public class Ppvg {
Assuming the file Ppvg.java is available to be read which of the following statements
is true if you try to compile and run the program?
A. An error will occur at compile time because the method fliton attempts to return
two values
B. The program will run and output only"fliton" and"flytwick"
C. The program will run and output only"flytwick"
D. The program will run and output only"fliton"
What happens when you attempt to compile and run these two files in the same directory?
//File P1.java
package MyPackage;
class P1{
void afancymethod(){
System.out.println("What a fancy method");
}
}
//File P2.java
public class P2 extends P1{
public static void main(String argv[]){
P2 p2 = new P2();
p2.afancymethod();
}
}
class Van {
String driver;
int rate;
Van() {
driver = "unknown";
rate = 5;
}
SpecVan() {
super();
}
void display() {
String s = "(" + super.toString() + "," + type + ")";
System.out.println(s);
}
}
B -done
B
class Base {
Base(int i) {
System.out.println("Base");
}
}
Seven() {
System.out.println("Seven");
}
}
import java.util.*;
A -done
A -done
A. Comparable
B. Comparable<Student>
C Comparator<Student>
D. Comparator
B
C
B. Package
C
B không được protected
String color;
int price;
Vase() {
price = 5;
}
int type;
SpecVase() {
type = 5;
}
void display() {
String s = "(" + super.toString() + "," + type + ")";
System.out.println(s);
}
}
B
C
Abe
C
D
D
interface A {
void foo();
void fun();
}
class B implements A {
public void foo() {
System.out.print("B1");
}
C - ko co dau phay
C
interface Eating {
https://youtu.be/D8E-3hjl7Xk
What is the output when you try to compile and run the following program?
A. String
B. No output, compile-time error
C. The code runs with no output
D. StringBuffer
Assuming any exception handling has been set up, which of the following will
create an instance of the RandomAccessFile class?
given that you have a method scale defined as follows, where scalex and scaley are
constants
1. double px = 10.02 ;
2. double py = 20.34;
3. Point thePoint = scale( px, py ) ;
A. The compiler objects to line 1.
B. The program compiles and runs
C. The program compiles but a runtime cast exception is thrown.
D. A compiler error occurs in line 3
Double → int
class Point {
public int x;
public int y;
Class SomeException:
1. public class SomeException {
2. }
Class A:
1. public class A {
2. public void doSomething() { }
3. }
Class B:
1. public class B extends A {
2. public void doSomething() throws SomeException { }
3. }
What will happen when you attempt to compile and run this code?
class Base {
A. The code will compile and run,printing out the words 'My Func"
B. The code will compile but complain at run time that the Base classs has non abstract
methods
C. The compiler will complain that the Base class is not declared as abstract
D. The compiler will complain that the method myfunc in the base class has no body
End