0% found this document useful (0 votes)
8 views4 pages

Java Programming Concepts and Methods

The document is a test covering various Java programming concepts, including iterating through 2D arrays, class definitions, method declarations, exception handling, and console output. It includes multiple-choice questions and code snippets for analysis. The test aims to assess understanding of Java syntax, object-oriented principles, and exception types.

Uploaded by

perropert
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)
8 views4 pages

Java Programming Concepts and Methods

The document is a test covering various Java programming concepts, including iterating through 2D arrays, class definitions, method declarations, exception handling, and console output. It includes multiple-choice questions and code snippets for analysis. The test aims to assess understanding of Java syntax, object-oriented principles, and exception types.

Uploaded by

perropert
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

PPJ

Example test
13 Jan 2025

I. To iterate trough every element of 2D array one can:

1. use one for each loop


2. use two for each loops
3. use [Link]() method
4. use two while loops

II. In java, a class can store:

1. definition of another public class


2. static methods
3. variables of any type
4. definition of a structure

III. Which of the following codes would be correct way of defining a method? All of the
following methods are stored inside of a Example class:

class Example{
public int a = 0;
public Example(){}
}

1. public void fun() throws Exception


2. static int [][][] fun(int a)
3. protected void fun(String [] args) throws Example
4. public double [] fun(public int a)

IV. Given this code:

class Test{
public static void main(String [] args){
Test t = new Test(4);
Test t2 = new Test();
[Link]();
}
}

Which of the follwing methods should be added so that the code will compile? Un-
neded methods should not be checked.

1. public Test()
2. public Test(String a)
3. public Test(int a)
4. public static void increaseLoad()

1 z4
PPJ
Example test
13 Jan 2025

5. public void increaseLoad()

V. Given the code:

class A{
private int a;
public A(int a){
this.a = a;
}
public A(){
a = 0;
}
public void print(){
[Link](this.a);
}
}

class B extends A{
private int b;
public B(){
super(10);
b=3;
}
public B(int a){
this.b = 9;
}
@Override
public void print(){
[Link](b);
}
}

class C{
public static void main(String [] args){
A a = new A(2);
A a2 = new B();
A a3 = new A();
[Link]();
[Link]();
[Link]();
}
}

What will be printed on the console

1. the code will not compile


2. 2 3 0

2 z4
PPJ
Example test
13 Jan 2025

3. 2 0 0
4. 0 3 0

VI. What is the difference between checked and unchecked Exceptions?

1. the programmer is forced to handle the unchecked exceptions


2. unchecked exceptions are checked at runtime
3. checked exceptions are derived directly from Exception class
4. unchecked exceptions are derived directly from Exception class

VII. Given the following code:

class A {
int a;
{
[Link]("Hello ");
}

public A(){
[Link]("There! ");
}

public A(int a){


this.a = a;
[Link]("General Kenobi! ");
}

public static void main(String [] args){


A a = new A();
A b = new A();
}
}

What will be printed on the console?

1. Hello There! Hello There!


2. code will not compile
3. Hello There! There!
4. There! There!

VIII. Which of the following codes would print the first element of String s?

1. [Link]([Link](0));
2. [Link](s[0]);
3. [Link]([Link]()[0]);

3 z4
PPJ
Example test
13 Jan 2025

4. [Link]([Link](0));

IX. Create a method which will take as an argument two-dimensional array of integer
numbers and will return as a result one dimensional array that stores only the even
numbers from the argument. If there are no even numbers the method should throw
an Exception with the message "No even numbers".

X. Given the following code:

class Test{
public static void main(String [] args){
Game g = new Game("Final Fantasy", 14, "MMORPG");
[Link](15);
Game g2 = new Game();
[Link](8);
[Link](g);
[Link](g2);
}
}

Create the missing class so the console will print: Game: Final Fantasy, Version: 15,
Type: MMORPG Game: Basic Game, Version: 8, Type: noType

4 z4

Common questions

Powered by AI

The correct Java statements for printing the first character of a string are `System.out.println(s.charAt(0));` or `System.out.println(s.toCharArray()[0]);` as they correctly extract and print the character at index 0 .

The 'Game' class should have fields: `String name`, `int version`, and `String type` with constructors setting default values and a method `updateVersion(int)` to modify the version. The `toString()` method must return 'Game: name, Version: version, Type: type' format .

To achieve output 'Hello There! Hello There!', ensure that the initialization block `{ System.out.print("Hello "); }` and constructor `{ System.out.print("There! "); }` in class `A` are intact. Each `new A()` call triggers these print statements sequentially .

Design a method taking an `int[][]` parameter, iterating through the array, collecting even numbers in a `List<Integer>`, and returning a new `int[]`. If the list is empty, throw a new `Exception("No even numbers")` .

A class in Java can store the definition of another public class, static methods, and variables of any type .

Among the given options, 'public void fun() throws Exception' is correct because it follows Java's method definition rules, allowing access specifiers, a return type, method name, and an optional exception declaration. Others have syntactic errors .

The output will be '2 3 0'. `a.print()` prints 2 from `A`, `a2.print()` calls the overridden print method in `B`, printing 3, and `a3.print()` prints 0 from the superclass `A` .

To traverse a 2D array in Java, you can use two `for-each` loops, two `while` loops, or the `Arrays.toString()` method, but using two nested loops is the conventional and most flexible approach .

The class `Test` should include constructors 'public Test()' and 'public Test(int a)' as well as a static method 'public static void increaseLoad()' to ensure the code compiles with the creation of `Test` objects and a call to `increaseLoad()` .

Checked exceptions are checked at compile-time and are derived from the `Exception` class, requiring handling with try-catch blocks or declaring with throws. Unchecked exceptions occur at runtime and usually derive from `RuntimeException` .

You might also like