0% found this document useful (0 votes)
20 views35 pages

OCJP Silver

The document contains Java code examples and explanations related to various programming concepts, including classes, methods, data types, and exception handling. It covers topics such as the Date and Time API, variable scope, and the differences between '==' and 'equals' for object comparison. Additionally, it includes examples of using Java collections and control flow statements like loops and conditionals.

Uploaded by

selt.miteg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views35 pages

OCJP Silver

The document contains Java code examples and explanations related to various programming concepts, including classes, methods, data types, and exception handling. It covers topics such as the Date and Time API, variable scope, and the differences between '==' and 'equals' for object comparison. Additionally, it includes examples of using Java collections and control flow statements like loops and conditionals.

Uploaded by

selt.miteg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Oracle Certified Java Programmer, Silver SE 11

2024 8 28

Copyright © 2024, Oracle and/or its affiliates 1


1. DiaryTest.java

package test;
import java.time.*;
public class Diary {
private Diary() { }
public static LocalDate getDate() {
return LocalDate.now();
}
}
public class DiaryTest {
public static void main(String[] args) {
System.out.println(Diary.getDate());
}
}

1. import
2. private Diary
3. Diary.class DiaryTest.class 2 .class
4. DiaryTest.class
5.

: 5.

public ( ) 1 public

: Date and Time API


Date and Time API API

) LocalDate.now() →

Copyright © 2024, Oracle and/or its affiliates 2


2.

public class Test {


private int value;
private Test(int tVal) {
int value = tVal * 2;
this.value = value * 2; // value
value = value * 3; // value
}
public int getValue() {
return value;
}
public static void main(String[] args) {
int tVal = 2;
Test t = new Test(tVal);
System.out.println(tVal + " " + t.getValue());
}
}

1. 2 8
2. 2 12
3. 2 16
4. 4 8
5. 4 12
6. 4 16

: 1. 2 8

this

private
private (
)

Copyright © 2024, Oracle and/or its affiliates 3


3. 2 // line 1 // line 2

[Item.java]

public class Item {


int price;
public Item() {
//line 1
}
}

[Test.java]

public class Test {


public static void main(String[] args) {
Item item = new Item();
// line 2
}
}

1. line 1 price = 500;


2. line 1 this.price = 500;
3. line 2 item.price = 0;
4. line 2 this.price = 500;

: 4. line 2 this.price = 500;

: this

this this.price (
) price
Item Test 4

static
line 2 static this (static
)

Copyright © 2024, Oracle and/or its affiliates 4


4.

List<String> list = Arrays.asList("Tokyo", "Kanagawa", "Saitama", "Chiba");


list.removeIf(s -> s.contains("i"));
System.out.println(list);

1. [Tokyo, Kanagawa, Saitama, Chiba]


2. [Saitama, Chiba]
3. [Tokyo, Kanagawa]
4.
5. UnsupportedOperationException
6. NullPointerException
7.

: 5. UnsupportedOperationException

: Arrays.asList() List

List List
UnsupportedOperationException List ( )
List Arrays.asList List List

List<String> list = new ArrayList<>(


Arrays.asList("Tokyo", "Kanagawa", "Saitama", "Chiba"));

List::of ( Java 9 )
List (Unmodifiable List) List
( )

List::removeIf ( Java 8 )
List Predicate true List
java.util.function.Predicate

list.removeIf((String s) -> {return s.contains("i"); });

Copyright © 2024, Oracle and/or its affiliates 5


5. line 1 (GC) Player

class Player {
String name;
}
public class Test {
private static void changeName(Player p, String n) {
p.name = n;
}
public static void main(String[] args) {
Player player1 = new Player(); // a
Player player2 = new Player(); // b
Player player3 = new Player(); // c
player2 = player3; // player1 = a, player2 = c, player3 = c
player3 = null; // player1 = a, player2 = c, player3 = null
changeName(player1, "Fernando"); // player1 = a, player2 = c, player3 = null
// line 1
}
}

1. Player GC
2. 1
3. 2
4. 3

: 2. 1

: (GC)

JVM ( ) = GC

line1 player1 player2 2

Copyright © 2024, Oracle and/or its affiliates 6


6. 7

public class Test {


public static void main(String[] args) {
int num = 5;
int age = num++; // line 1
int v = ++age - num++; // line 2
int ans = (v <= 0) ? (age < num) ? 7 : 8; // line 3
System.out.println(ans);
}
}

1. line 1 int age++ = num++;


2. line 1 int age = ++num;
3. line 2 int v = ++age - num;
4. line 3 int ans = (v <= 0) ? (age < num) ? 7 : 8 : 9;

: 4. line 3 int ans = (v <= 0) ? (age < num) ? 7 : 8 : 9;

if-else 1

[if-else ]

int ans = 0;
if (v <= 0) ans = 10;
else ans = 11;

[ ]

// < > ? <true > : <false >


int ans = (v <= 0) ? 10 : 11;

line 3

num++ … num ++(1 )


++num … ++(1 ) num

Copyright © 2024, Oracle and/or its affiliates 7


7. true

class Region {
int id;
String name;
public Region(int id, String name) {
this.id = id;
this.name = name;
}
}
public class Test {
public static void main(String[] args) {
var r1 = new Region(3, "Japan");
var r2 = new Region(3, "Japan");
var r3 = r1;
System.out.println(r1 == r2); // false
System.out.println(r1 == r3); // true
System.out.println(r1.id == r2.id); // true
System.out.println(r1.name == r2.name); // true
System.out.println(r1.name.equals(r2.name)); // true
}
}

1. 0
2. 1
3. 2
4. 3
5. 4
6. 5

: 5. 4

: == equals

== equals

String (==)
String ( var s = "abc"; )
( ) new
String == true

Copyright © 2024, Oracle and/or its affiliates 8


8.

public class Test {


var booleanStr = "TRUE";
public static void main(String[] args) {
String numStr = "123";
var var = Integer.parseInt(numStr);
Boolean b1 = Boolean.parseBoolean(booleanStr);
System.out.print(var + ":" + b1);
System.out.print(" ");
var = Integer.valueOf(numStr);
boolean b2 = Boolean.valueOf(booleanStr);
System.out.print(var + ":" + b2);
}
}

1. 123:true 123:true
2. 123:false 123:false
3. 123:false 123:true
4. 123:true 123:false
5.
6. NumberFormatException
7. ClassCastException

: 5.

: var (Local Variable Type Interface)

var var ( )

for for
( null, , )

Copyright © 2024, Oracle and/or its affiliates 9


9. throws MyException
MyException (2 )

public String method() throws MyException {


// …
}

1. MyException
2. MyException
3. MyException
4. method() java.lang.Exception
5. method() MyException
6. method() MyException

:1 6

(Exception ) try-catch throws throws


throws
MyException MyException

Java SE 7 try { … } catch (MyException me) {


throw new Exception (me);} catch throws
(= )

Copyright © 2024, Oracle and/or its affiliates 10


10. java.lang

1. java.desktop
2. jdk.compiler
3. java.se
4. java.base
5. java.management
6. java.prefs

: 4. java.base

: Modular JDK

Java SE 9 JPMS (Java Platform Module System) JDK Modular JDK


Java API Java SE Platform API java.se
java.se Requires java.lang
API java.base java.base
requires java.base requires

JDK Module

Copyright © 2024, Oracle and/or its affiliates 11


11. doubleArray

double[] doubleArray = { 10.8, 33.2, 5.6 };

for (i : doubleArray) {
System.out.println(i);
}

for (int i = 0 : doubleArray) {


System.out.println(i);
}

for (var i : doubleArray) {


System.out.println(i);
}

for (double i : double[] doubleArray) {


System.out.println(i);
}

:3

: for

for Collection

for ( : ) {
//
}

Copyright © 2024, Oracle and/or its affiliates 12


( var ) (
Collection)

for

for (int n = 0; n < doubleArray.length; n++) {


double i = doubleArray[n];
System.out.println(i);
}

Copyright © 2024, Oracle and/or its affiliates 13


12.

public class Test {


public static void main(String[] args) {
int[][] numArr = { { 1, 3, 5 }, { 2, 4, 6 } };
int i = 0;
for (int[] contents : numArr) {
for (int j = 0; j < contents.length; j++) {
System.out.print(numArr[i][j] + " ");
if (numArr[i][j] == 4) {
break;
}
}
if (i < 10) {
i++;
continue;
}
}
}
}

1. 1 3 5 2 4 6
2. 1 3 5 2 4
3. 1 3 5 2
4. 1 3 5 2 6
5. ArrayIndexOutoufBoundsException
6.

: 2. 1 3 5 2 4

continue break

break … ( ) switch, while, do-while, for


continue … ( ) while, do-while, for

for 2 break 6

System.out.println(numArr[0][1]); // 3

Copyright © 2024, Oracle and/or its affiliates 14


13.

public class Test {


public static void print(int num1, double num2) {
System.out.println((num1 * 3) + " " + (num2 * 4));
}
public static void print(Integer num1, float num2) {
System.out.println((num1 * 5) + " " + (num2 * 6));
}
public static void print(int num1, float num2) {
System.out.println((num1 * 7) + " " + (num2 * 8));
}
public static void print(Integer num1, double num2) {
System.out.println((num1 * 9) + " " + (num2 * 10));
}
public static void main(String[] args) {
Test.print(7, 20.0f);
}
}

1. 21 80.0
2. 35 120.0
3. 49 160.0
4. 63 200.0
5. NumberFormatException
6.

: 3. 49 160.0

print 4

Test.print(7, 20.0f);

print 7 , 20.0f int , float

public static void print(int num1, float num2) {


System.out.println((num1 * 7) + " " + (num2 * 8));
}

Copyright © 2024, Oracle and/or its affiliates 15


int , float
public static void print(int num1, float num2) { } … 2
public static void print(int num1, double num2) { } … 1 1
public static void print(Integer num1, float num2) { } … 1 1

public static void print(Integer num1, double num2) { } … 1


1

Copyright © 2024, Oracle and/or its affiliates 16


14.

public class Test {


String message; // line 1
private static void showMessage() { // line 2
message = "Hello!!";
System.out.println(message);
}
public static void main(String[] args) {
Test.showMessage(); // line 3
}
}

1. line 1 private static String message;


2. line 2 private void showMessage() {
3. line 2 public static void showMessage() {
4. line 3 new Test().showMessage();

: 1. line 1 private static String message;

: static

static ( )
static showMessage() message

static static

.
.

Copyright © 2024, Oracle and/or its affiliates 17


15. line 1 (2
)

[Parent.java]

package p1;
public abstract class Parent {
protected abstract void displayValue();
}

[Child.java]

package p2;
import p1.Parent;
public abstract class Child extends Parent {
// line 1
}

1. private void dispalayValue(){ }


2. protected void displayValue(){ }
3. void displayValue(){ }
4. public abstract void displayValue() { }
5. abstract void displayValue();

:1 2

: abstract

abstract abstract abstract

abstract abstract (
)

public > protected > (default) > private

1. private Child
displayValue()

Copyright © 2024, Oracle and/or its affiliates 18


16.

class Base {
private void print() {
System.out.print(" : ");
}
public void methodX() {
print();
}
}
public class Sub extends Base {
private void print() {
System.out.print("Hello: ");
}
public void methodY() {
print();
}
public static void main(String... args) {
Sub s = new Sub();
s.methodX();
s.methodY();
}
}

1. : Hello:
2. : :
3. Hello: :
4. Hello: Hello:
5.

: 1. : Hello:

: private

private private
private
private

Copyright © 2024, Oracle and/or its affiliates 19


17. 2

[User.java]

package app.entity;
public class User {
private int no;
public User(int no) { this.no = no; }
public void no() { } // line 1
}

[Test.java]

package app;
import app.entity.*;
public class Test {
public static void main() {
int no = 4; // line 4
User user = new User(no);
}
}

( )

javac -d /u01/work/build $(find . -name "*.java")

1. java /u01/work/build/app/Test
2. java app.Test
3. java -cp /u01/work/build app.Test
4. java -classpath /u01/work/build/app Test
5. java -p /u01/work/build -m Test

: 3. java -cp /u01/work/build app.Test

java javac
CLASSPATH -classpath ( -cp )
(FQCN)
javac -d (.class) -d
java
-classpath ( -cp ) -d

Copyright © 2024, Oracle and/or its affiliates 20


18.

class A {
public void test() {
System.out.print("A ");
}
}
class B extends A {
public void test() {
System.out.print("B ");
}
}
public class C extends A {
public void test() {
System.out.print("C ");
}
public static void main(String[] args) {
A b1 = new A();
A b2 = new C();
b1 = (A) b2; //line 1
A b3 = (B) b2; //line 2
b1.test();
b3.test();
}
}

1. A B
2. A C
3. C C
4. line 1 ClassCastException
5. line 2 ClassCastException

: 5. line 2 ClassCastException

line 2
b2 (C ) B C B
ClassCastException

(
)

Copyright © 2024, Oracle and/or its affiliates 21


19.

class Food {
String name;
public Food(String name) { this.name = name; }
public String toString() { return name; }
}
public class Test {
public static void main(String[] args) {
Food[] foods = { new Food("Wine"), new Food("Cheese"), new Food("Meat") };
System.out.print(foods);
System.out.print(" | " + foods[1]);
System.out.print(" | " + foods[1].name);
}
}

1. [LFood;@15db9742 | Food@6d06d69c | Cheese


2. [LFood;@15db9742 | Cheese | Cheese
3. [Wine, Cheese, Meat] | null | Cheese
4.
5.

: 2. [LFood;@15db9742 | Cheese | Cheese

( ) 0 foods[0]

toString
toString Object Object
toString Food@28feb3fa ( )
Food toString name

Object toString
Arrays.toString

System.out.print(Arrays.toString(foods));

Copyright © 2024, Oracle and/or its affiliates 22


20.

public class Main {


public static void main(String[] args) {
ArrayList<String> myList = new ArrayList<>(10);
String[] myArray;
try {
while (true)
myList.add("My String");
} catch (RuntimeException re) {
System.out.print("Caught a RuntimeException ");
} catch (Exception e) {
System.out.print("Caught an Exception ");
}
System.out.print("→ Ready to use ");
}
}

1. Caught a Runtime Exception → Ready to use


2. Caught an Exception → Ready to use
3. Caught a Runtime Exception
4. Caught an Exception
5. → Ready to use
6. OutOfMemoryError
7.

: 6. OutOfMemoryError

while List OutOfMemoryError (


1 ({ } ) )

while (true)
myList.add("My String");

OutOfMemoryError (OOME)
JVM GC
java.lang.Error (
)

Copyright © 2024, Oracle and/or its affiliates 23


21. MyInterface (2 )

public interface MyInterface {


void print();
}

public class MyImpl implements MyInterface {


public void print() {
System.out.print("one");
}
}

public class MyImpl {


public void print() {
System.out.print("one");
}
}

public class MyImpl implements MyInterface {


private void print() {
System.out.print("one");
}
}

public abstract class MyImpl implements MyInterface {


public void print() {
System.out.print("one");
}
}

public abstract class MyImpl implements MyInterface {


public String print() {

Copyright © 2024, Oracle and/or its affiliates 24


System.out.print("one");
return null;
}
}

:1 4

: abstract

public abstract
public ( public
= )
abstract

abstract
abstract abstract (abstrace
)

5 print()
(
)

Copyright © 2024, Oracle and/or its affiliates 25


22. moduleA com.acme.Main

[Main.java]

package com.acme;
public class Main {
public static void main(String... args) {
System.out.println("Module Test");
}
}

[module-info.java]

module moduleA {
// …
}

1. java -cp mlib/moduleA.jar com.acme.Main


2. java -p mlib moduleA com.acme.Main
3. java -p mlib -m moduleA/com.acme.Main
4. java -cp mlib/moduleA.jar -m moduleA/com.acme.Main
5. java -p mlib moduleA/com/acme/Main

: 3. java -p mlib -m moduleA/com.acme.Main

java -p --module-path
-m --module

> java -p mlib -m moduleA/com.acme.Main

JAR Main java -p mlib -m moduleA

CLASSPATH JAR
requires
exports (=
)
JAR
JAR

Copyright © 2024, Oracle and/or its affiliates 26


23.

[moduleA/module-info.java]

module moduleA {
requires moduleB;
}

[moduleB/module-info.java]

module moduleB {
exports api to moduleA;
}

1. moduleA api
2. moduleA moduleB public
3. moduleA moduleB api public
4. moduleB moduleA api public
5. moduleB moduleA api
6. moduleB api moduleA 2

: 3. moduleA moduleB api public

: module-info.java

module-info.java module-info.java
module-info.class

module MyModule { // line1


requires AnotherModule; // line2
experts sample.myPkg; // line3
}

line1:

line2: MyModule AnotherModule


line3: MyModule exports
public exports < > to < >

Copyright © 2024, Oracle and/or its affiliates 27


24. line 1

var list = new ArrayList<>(List.of("Tokyo", "Kanagawa", "Saitama", "Chiba"));


for(String s: list) { System.out.print(s);} // line 1

1. list.for(System.out.print(s));
2. list.forEach(list -> System.out.print(v));
3. list.forEach( var -> {System.out.print(v);});
4. list.for((String s) -> System.out.print);
5. list.forEach(System.out::print);
6. list.forEach(list -> s);

: 5. list.forEach(System.out::print);

: List

for List Java SE 8 List


(Collection) forEach forEach
java.util.function.Consumer Consumer

java.util.function.Consumer
void

(Consumer)
( )

Consumer<String> c1 = (String s) -> {System.out.println(s);}; //


Consumer<String> c2 = x -> System.out.println(x); //
Consumer<String> c3 = System.out::println //

Copyright © 2024, Oracle and/or its affiliates 28


25.

public interface MyInterface {


int getValue();
}

class MyClass {
public static void main(String... args) {
MyInterface i = () -> 'a';
System.out.println(" : " + i.getValue());
}
}

1. : a
2. : 'a'
3. : 97
4.
5. NullPointerException
6. NumberFormatException

: 3. : 97

MyInterface SAM (Single Abstract Method)


(= getVaule )

MyInterface i = () -> {return 'a';};


// i getValue char 'a' int

MyInterface ( 1Z0–815
)

MyInterface m = new MyInterface() {


@Override
public int getValue() {return 'a';};
};

: char 'a' int 97


int System.out::print

Copyright © 2024, Oracle and/or its affiliates 29


26.

class MyRuntimeException extends RuntimeException {}


public class Main{
public static void main(String... args) {
try {
String duke = "Java";
if (duke.equalsIgnoreCase("JAVA")) {
throw new MyRuntimeException();
}
} catch (MyRuntimeException | NullPointerException e) {
System.out.print("catch 1 ");
} catch (Exception e) {
System.out.print("catch 2");
} catch (RuntimeException e) {
System.out.print("catch 3 ");
} finally {
System.out.print("finally");
}
}
}

1. catch 1 finally
2. catch 1 catch 3 finally
3. catch 2 finally
4. catch 2 catch 3 finally
5. catch 1 catch 2 catch 3 finally
6. catch 3 finally
7.

: 7.

: try-catch

try-catch try catch


finally try
( : ) catch
catch catch

multi-catch
multi-catch catch

} catch (Exception1 | Exception2 | Exception3 ex) { ex.printStackTrace(); }


multi-catch ( )

Copyright © 2024, Oracle and/or its affiliates 30


27.

interface HelloInterface {
public void greeting();
}

abstract class AbstractHello {


private void greeting() {
System.out.println("Hello World");
}
}

public class HelloImpl extends AbstractHello implements HelloInterface {


public static void main(String[] args) {
var hello = new HelloImpl();
hello.greeting();
}
}

1. Hello World
2.
3.
4. IlleaglAccessException
5. NoSuchMethodException

: 3.

: abstract

AbstractHello private AbstractHello


HelloImpl private
HelloImpl HelloInterface
HelloImpl

Copyright © 2024, Oracle and/or its affiliates 31


28.

[Parent.java]

public class Parent {


protected boolean check(Integer val) {
return true;
}
}

[Child.java]

public class Child extends Parent {


boolean check(int val) {
return false;
}
public int modify(int val) {
if(check(val)) return val;
return 0;
}
public static void main(String[] args) {
Child c = new Child();
System.out.println(c.modify(10));
}
}

1. 10
2. 0
3.
4. IlleagalArgumentException
5. NumberFormatException

: 2. 0

: int Integer

( ) int ( ) java.lang.Integer
modify int
check Child check

Copyright © 2024, Oracle and/or its affiliates 32


29. Java

1. JRE
2. JDK
3. (IDE)
4. (IDE) JDK
5. (IDE) JRE
6. JDK JRE

: 2. JDK

: Java SE 11 JRE

Java SE 11 Public JRE ( Oracle ) Java SE 9 Applet


Web NPAPI JRE
Java
jlink

Copyright © 2024, Oracle and/or its affiliates 33


30.

[A.java]

public class A {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return getName();
}
}

[B.java]

public class B extends A {


public void setName(String name) {
super.setName("\"" + name + "\"");
}
public void setName(String[] nameParts) {
var output = format(nameParts);
setName(output);
}
private String format(String[] parts) {
var sb = new StringBuilder();
for(String part: parts) {
sb.append(part).append(' ');
}
return sb.toString();
}
public static void main(String... args) {
A a = new B();
a.setName(args);
System.out.println(a);
}
}

java B John Peter

1. John Peter
2. "John Peter "
3. A String setName

Copyright © 2024, Oracle and/or its affiliates 34


4. a A B private
5. A a = new B();
6. A a = new B(); ClassCastException
7. format ArrayIndexOutOfBoundsException

: 3. A String setName

String...

( ) 1

a.setName(args);
main args String a.setName(args);
String a A A String

B setName B

Copyright © 2024, Oracle and/or its affiliates 35

You might also like