- Executors
-
Learn
Learn Python Runnable examples, NumPy, pandas, and more Learn AINeural networks, ML and LLMs from scratch Learn C & C++ Pointers, memory, STL, and modern C++ Learn C#C# basics, OOP, LINQ, async, and .NET exercises Learn MatplotlibLine, bar, scatter and pie charts with live visualizers Python Algorithms Search, sorting, recursion, and visualizers O(n) C & C++ Algorithms Compare algorithms implemented in C and C++ Coding for Kids Learn to code by playing!
- ChallengesNew
- For TeachersNew
- Updates
- Buy me a coffee
A multi-class classifier outputs one probability per class (for example from softmax). Categorical cross-entropy punishes it for giving the correct class a low probability:
loss = -(ln(p₁) + ln(p₂) + … + ln(pₙ)) / n
where pᵢ is the probability the model gave to the correct class of sample i. To avoid ln(0), first raise any probability below 0.0000001 to 0.0000001.
Implement categorical_cross_entropy(probabilities, labels).
Input
probabilities: One row per sample, with one probability per class.
labels: The index of the correct class for each sample (0-based).
Output
Return the average loss as a decimal number.
Constraints
1 ≤ samples ≤ 1,000, 2 ≤ classes ≤ 100
0 ≤ each probability ≤ 1
0 ≤ labels[i] < number of classes
Examples
Example 1
Input:
probabilities = [
[0.7, 0.2, 0.1],
[0.1, 0.8, 0.1]
]
labels = [0, 1]
Expected output:
0.289909
Explanation
The correct classes got 0.7 and 0.8: -(ln(0.7) + ln(0.8)) / 2 = -(-0.356675 - 0.223144) / 2 ≈ 0.289909.
Example 2
Input:
probabilities = [
[1.0, 0.0]
]
labels = [1]
Expected output:
16.118096
Explanation
The correct class got probability 0, which is raised to 0.0000001, so the loss is -ln(0.0000001) ≈ 16.118096: a confident wrong answer is punished hard.
References
-
Example 1 Not runInput:
probabilities = [ [0.7, 0.2, 0.1], [0.1, 0.8, 0.1] ] labels = [0, 1]Expected output:0.289909Your output -
Example 2 Not runInput:
probabilities = [ [1.0, 0.0] ] labels = [1]Expected output:16.118096Your output
4 hidden tests also run when you submit. If your code fails one, you'll see that test. Solve the challenge to see them all.
Online Java Compiler
Write, compile, run, and debug Java code online in a full-featured browser IDE. Work with multiple files, save your workspace, and share or collaborate in real time - no setup required.
🚀 260,408 total executions (5,854 this month)
Practice Java with coding challenges
Solve hands-on problems right in this editor: run the sample tests, submit, and get an AI code review.
☕ About This Java Online Executor
The CodeUtility Java Executor lets you write and run Java programs directly in your browser - no installation, JDK setup, or IDE required. It’s powered by a secure sandbox that supports real Java runtime versions 8, 11, 17, 21, and the latest.
This tool compiles and executes Java code in the cloud using a real javac compiler and Java runtime
environment, so you can test classes, methods, and logic exactly as they would behave locally.
Whether you’re practicing object-oriented programming, preparing for interviews, or testing snippets before integration, the CodeUtility Java Executor provides a fast and reliable environment to run real Java code instantly from any device.
More than a code runner
Use a multi-file editor, an isolated execution terminal, runtime-specific libraries, persistent workspaces, sharing, and real-time collaboration from the same executor.
How to use this executor?
Choose a runtime, open or create a file, write your code, and select Run. Output appears in the terminal, while signed-in users can keep files in a workspace, install libraries, and collaborate through shares.
- Select a version and edit the current file or open another workspace file.
- Run the code, provide input in the terminal when requested, and review saved run history.
- Use libraries, workspace tools, sharing, and chat when your task needs more than one snippet.
💡 Java Basics & Examples You Can Try Above
1. Declaring Variables and Constants
Java requires you to declare the type of each variable. Use final for constants.
int age = 30;
double pi = 3.14159;
char grade = 'A';
String name = "Alice";
boolean isActive = true;
// Constants
final int MAX_USERS = 100;
final String COMPANY = "CodeUtility";
2. Conditionals (if / switch)
Use if, else if, and switch for control flow.
int x = 2;
if (x == 1) {
System.out.println("One");
} else if (x == 2) {
System.out.println("Two");
} else {
System.out.println("Other");
}
switch (x) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
default:
System.out.println("Other");
}
3. Loops
Use for, while, and do-while for iteration.
for (int i = 0; i < 3; i++) {
System.out.println(i);
}
int n = 3;
while (n > 0) {
System.out.println(n);
n--;
}
4. Arrays
Arrays hold fixed-size sequences of the same type.
int[] numbers = {10, 20, 30};
System.out.println(numbers[1]);
5. ArrayList Manipulation
Use ArrayList for dynamic-sized lists.
import java.util.ArrayList;
ArrayList<Integer> nums = new ArrayList<>();
nums.add(1);
nums.add(2);
nums.add(3);
nums.remove(Integer.valueOf(2));
for (int num : nums) {
System.out.print(num + " ");
}
6. Console Input/Output
Use Scanner for input and System.out for output.
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name);
7. Functions
Define methods with return types and parameters.
public static int add(int a, int b) {
return a + b;
}
System.out.println(add(3, 4));
8. HashMaps
Use HashMap for key-value storage.
import java.util.HashMap;
HashMap<String, Integer> ages = new HashMap<>();
ages.put("Alice", 30);
System.out.println(ages.get("Alice"));
9. Exception Handling
Use try and catch to handle runtime exceptions.
try {
throw new Exception("Something went wrong");
} catch (Exception e) {
System.out.println(e.getMessage());
}
10. File I/O
Use Files and Paths from java.nio.file for file handling.
import java.nio.file.*;
import java.io.IOException;
Files.writeString(Paths.get("file.txt"), "Hello File");
String content = Files.readString(Paths.get("file.txt"));
System.out.println(content);
11. String Manipulation
Java strings support many methods like length(), substring(), and contains().
String text = "Hello World";
System.out.println(text.length());
System.out.println(text.substring(0, 5));
System.out.println(text.contains("World"));
12. Classes & Objects
Java supports object-oriented programming using classes and instances.
class Person {
String name;
Person(String name) {
this.name = name;
}
void greet() {
System.out.println("Hi, I'm " + name);
}
}
Person p = new Person("Alice");
p.greet();