1Z0-811 Java Basics and Syntax • Complete Question Bank
Complete 1Z0-811 Java Basics and Syntax question bank — all 0 questions with answers and detailed explanations.
Refer to the exhibit.
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a + b);
System.out.println("Sum: " + a + b);
}
}Refer to the exhibit.
public class LoopTest {
public static void main(String[] args) {
int count = 0;
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) {
continue;
}
count++;
}
System.out.println(count);
}
}class A {
private int x = 5;
}
class B extends A {
public void print() {
System.out.println(x);
}
}public class Test {
public void print(String s) { System.out.println("String"); }
public void print(Object o) { System.out.println("Object"); }
public static void main(String[] args) {
Test t = new Test();
t.print(null);
}
}public class MyClass {
public static void main(String[] args) {
int num = 10;
if(num = 5) {
System.out.println("Five");
}
}
}public class Test {
public static void main(String[] args) {
int a = 5;
int b = a++ + ++a;
System.out.println(b);
}
}public class Test {
public static void main(String[] args) {
int x = 10;
int y = x++;
System.out.println(y);
}
}$ java Test
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at Test.main(Test.java:3)Refer to the exhibit.
public class Test {
public static void main(String[] args) {
int x = 10;
if (x = 5) {
System.out.println("Equal");
}
}
}A financial trading application processes high-volume transactions. The system uses a multithreaded architecture where multiple threads update a shared Account object's balance field. Recently, intermittently incorrect balance calculations have been reported. Developers suspect a race condition on the balance field. The Account class is defined as follows:
public class Account {
private double balance = 0.0;
public void deposit(double amount) { balance += amount; }
public void withdraw(double amount) { balance -= amount; }
public double getBalance() { return balance; }
}Threads are created using ExecutorService with a fixed thread pool. The issue occurs only under heavy load. Which course of action should the development team take to resolve the issue while maintaining performance?
A developer is writing a program to find the maximum value in an array of integers. The code is:
int[] nums = {10, 20, 5, 30, 15};
int max = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > max) {max = nums[i];
} }
System.out.println(max);
The output is 30, which is correct for this array. However, the developer is concerned that if all numbers are negative, the output would be 0 instead of the highest negative number. Which modification ensures the algorithm works correctly for all integer arrays?
You are tasked with debugging a Java application that processes temperature sensor readings. The application reads an array of double values representing temperatures in Celsius, and should output the number of readings that exceed a threshold of 30.0°C. The current implementation is:
public class TemperatureAnalyzer {
public static void main(String[] args) {double[] readings = {25.5, 32.1, 30.0, 28.9, 35.7};
int count = 0;
for (int i = 0; i < readings.length; i++) {
if (readings[i] > 30.0) {count++;
}
}System.out.println("Count: " + count);
} }
However, the output is "Count: 3" instead of the expected 2 (since 30.0 is not above the threshold). The developer believes the issue is that the condition should be '>=' to include 30.0, but the specification says strictly above 30.0. After further investigation, you discover that the problem is due to floating-point imprecision when comparing with the literal 30.0. Which of the following changes would correctly fix the comparison to reliably count readings strictly greater than 30.0, considering floating-point representation?
Refer to the exhibit. int x = 10; double y = 5.5; int result = x + y;
What is the output?
```java
public class Test {
public static void main(String[] args) {String s1 = "hello"; String s2 = "hello"; System.out.println(s1.equals(s2));
} }
```
Refer to the exhibit.
public class DataTypes {
public static void main(String[] args) {
boolean flag = true;
if (flag = false) {
System.out.println("False");
} else {
System.out.println("True");
}
}
}Drag or tap steps into the slots.
Drag or tap steps into the slots.
Drag a concept onto its matching description — or click a concept then click the description.
Accessible from anywhere
Accessible within same package and subclasses
Accessible only within same package
Accessible only within same class
Drag a concept onto its matching description — or click a concept then click the description.
Ordered collection allowing duplicates
Collection with no duplicates
Collection for holding elements prior to processing
Key-value pairs, keys unique
Double-ended queue supporting insertion/removal at both ends
public class Test {
public void print(int i) { System.out.println("int"); }
public void print(double d) { System.out.println("double"); }
public static void main(String[] args) {
Test t = new Test();
t.print(10);
}
}javac Test.java
Test.java:3: error: variable x might not have been initialized
System.out.println(x);
^Given the following code:
class A {A() { System.out.print("A "); }
}
class B extends A {B() { System.out.print("B "); }
}
class C extends B {C() { System.out.print("C"); }
public static void main(String[] args) {new C();
} }
What is the output when running the main method?
public class InitOrder {
static { System.out.print("A "); }
{ System.out.print("B "); }
public InitOrder() { System.out.print("C "); }
public static void main(String[] args) {
new InitOrder();
}
}A developer writes the following code to compare two strings: String s1 = "Java"; String s2 = new String("Java");
if (s1 == s2) { System.out.println("Equal"); } else { System.out.println("Not equal"); }What is the output?
A method is declared as:
public void setAge(int age) { this.age = age; }Which statement is correct about this code assuming the class has an instance variable age?
Refer to the exhibit.
javac Test.java
Test.java:3: error: class Test is public, should be declared in a file named Test.java
public class Test {
^
1 errorA junior developer wrote the following code to calculate the sum of an array:
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 1; i <= numbers.length; i++) {sum += numbers[i];
}
System.out.println("Sum: " + sum);
The developer expects the output to be 15, but the program throws an exception. What is the root cause and the correct fix?
A mobile app backend uses Java streams to process user data. The code snippet filters users who are active and older than 18, then collects them into a list:
List<User> result = users.stream() .filter(u -> u.isActive()) .filter(u -> u.getAge() > 18) .collect(Collectors.toList());
Performance metrics show that this stream operation is slow when the user list has millions of entries. The team wants to improve performance without changing the business logic. Which change would most likely improve performance?
A junior developer is reviewing a Java source file and sees the following declaration at the top of the file:
package com.acme.util;
Which statement correctly describes the requirement for this declaration?
A developer is writing a method that calculates the average of two integers and wants the result to include fractional values. The method signature is:
public static double average(int a, int b)
Which return statement produces the intended result?
A junior developer writes the following code snippet:
int x = 10;
if (x = 5) {System.out.println("Equal");
} else {System.out.println("Not Equal");
}
What happens when this code is compiled and run?
A developer writes a class with a static field and an instance field:
public class Counter {static int total = 0;
int count = 0;
void increment() {total++; count++; System.out.println(total + " " + count);
} }
In main, two Counter objects c1 and c2 are created. c1.increment() is called, then c2.increment(). What is printed?
A programmer writes the following code:
public class Test {
public static void main(String[] args) {
int i = 0;
for (; i < 3; i++) {System.out.print(i + " ");
}
}
}What is the output?