OCA Java Certification Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Package statement. What is it, and what are its requirements?

A

Defines which package a class is in. Must come first, there can only be one. (A comment could theoretically come before)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When is a variable’s scope determined?

A

Compile time, not runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the scope of a class variable?

A

Anywhere within the class, and from the outside if the access variable allows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the scope of a method/local variable?

A

Just the method until the method returns. Cannot be accessed from outside.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When does a method parameter get initialized?

A

When the method is invoked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a block level scope?

A

Defined by { } and only accessible within the block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe a class declaration

A

modifier “class” ClassName (extends OneParent) implements Interface1, Interface2 { //body }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the required components of a class declaration?

A

“class” ClassName { //body }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe a field declaration

A

modifier, type, name (E.g. private String stringName)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe a method declaration

A

modifier returnType methodName(DataType inputParameter, DataType inputParameter2) throws ExceptionType { //body }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe a constructor declaration

A

Constructors are used to create an object from the blueprint. They look exactly like a method declaration except there is no return type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the access modifier of the default constructor of a class?

A

The same as the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can a field and a method have the same name?

A

Yes, but it’s not a good idea.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the requirements for a class to be executable?

A
main class must be public in form: public static void main(String[] args) { //code }
OR
public static void main(String... args) { //code }
File name must match public class name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you get from source code to running application?

A

Save file as .java
Compile it to get the .class file
then run

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the javac compiler consume and produce?

A

Consumes source code,

produces bytecode

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Main method

A

public static void main (String[] args) { }
parameter name can be anything. e.g.
public static void main (String[] whatever) { }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What happens if you name your file something different from the declared class name (and this class has a valid main method)?

A

Compilation failure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

When you launch a program from the command line, what must you pass as a first argument?

A

The name of the main class without any extension (e.g. .java or .class)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

If you don’t import a package, how do you get access to a package?

A

Use a fully qualified name. E.g.

java.util.List

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What are the requirements for an import statement?

A

import statements must be at the beginning of a file just after the package statement (if there is one)
import all with *
java.lang is automatically imported

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
Given the import statement:
import my.pack.*;
Which types can be referred to by its simple name?
mypack.MyClass
my.pack.sub.pack.MyClass
none of the above
A

None. Why? Because there is no package hierarchy in Java, and all packages are independent.

23
Q

Can you import a subset of packages like this?

import mypackage.My*;

A

No.

24
Q

True or false? Importing a type from the same package will produce a compile error.

A

False. But it’s unnecessary.

25
Q

What if you import 2 types of the same class:
import package1.MyClass;
import package2.MyClass;

A

Compiler fails at the second import package.

26
Q

What if you import 2 types of the same class:
package1.MyClass;
package2.MyClass;
but you import the whole package for each?
import package1.;
import package2.
;

A

Compiler fails as soon as you use MyClass by its simple name. It doesn’t know which one you mean.
If you use the fully qualified name it will succeed.

27
Q

Java buzzword: What does “distributed” mean?

A

Java is designed to support applications on networks, where code can be executed from other machines.

28
Q

What are the 4 pillars of Object Oriented programming?

A
A PIE
Inheritance
Polymorphism
Abstraction
Encapsulation
29
Q
Which of the following is not a feature of Java?
Distributed
Flexible
Interpreted
Robust
A

Flexible

30
Q

True or false? Java is platform independent so it behaves the same way regardless of platform.

A

False.

31
Q
Which feature is associated with this description? Java classes are loaded on demand, even across a network.
Architecture neutral
Distributed
Dynamic
Portable
A

Dynamic

32
Q
Given a scenario where variables of a class are hidden from other classes and can be accessed only through public methods of their enclosing class, which OO concept is demonstrated?
Inheritance
Polymorphism
Abstraction
Encapsulation
A

Encapsulation

33
Q

What are the 11 Java buzzwords?

A
Mnemonic: that is "SO SHRIMP DAD"
Simple
Object oriented
Secure
High performance
Robust
Interpreted
Multi-threaded
Platform independent
Distributed
Architecture neutral
Dynamic
34
Q

What is the output?

 public class Whiz {
         public static void main(String [ ] a) {
                 int [ ] a = {1,2,3};
                 System.out.print(a[3]);
         }
 }
A

Code fails due to error on line 3 because the main method argument name and line 3 int array name are same. Trying to declare two variables with same name in same scope causes a compile time error.

35
Q

What is responsible for the hardware- and operating system-independence of the Java SE platform, the small size of compiled code (bytecodes), and platform security.

A

The JVM.

36
Q
Which of the following must be included in a java source file to compile?
Package declaration
Class Declaration
Import Statements
Main Method
Field Declaration
A

Class declaration.

37
Q

What command compiles the code, and from what file type to what file type?

A

javac command compiles .java into .class

38
Q
What is the output? 
      public class Whiz{
                       static int x = 4;
                   public static void main(String[] args) {

                                   for(int x = 5;x<10;x++)   
                                    x++;
                                    System.out.print(x--);
                   }
}
A

“4”

Note: the for loop has no brackets, so the for-loop variable scope ends at x++.
After that, the print statement prints the value of the static variable. The post decrement operator does nothing.

39
Q
What is the output?
public class Whiz{
       public static void main(String[] args) {
                   int x = 0;
                   do{
                                   int y = x++;
                                   System.out.print(y);
                   }while(y<5);
       }    }
A

Compilation fails.

We are trying to access y out of the scope where it is defined.

40
Q
Which of the following is sufficient to run a Java program only ?
JVM
JDK
JRE
Java SE platform
None of the above
A

JRE.
To run a java program, we only need to have the java runtime environment. The JRE consists of the Java Virtual Machine (JVM), Java platform core classes, and supporting Java platform libraries.

41
Q

Given the following class in the file programs/mine/whiz/Fan.java:

INSERT CODE HERE

public class Fan{ }

What replaces INSERT CODE HERE if we compile from programs ( root folder ) ?

A

package mine.whiz;

The package name represents any folders underneath the current path, which is mine.whiz in this case.

42
Q
Which of the following add form of multiple inheritance capability to java 8?
Static methods introduced in interfaces
Default methods introduced in interfaces
Enum introduced in Java8
Multiple extending feature
None of the above
A

Default methods.

From java 8 interface can have both default and static non abstract methods. From them default methods are inherited, since we can implement as many as interfaces, java 8 has form of multiple inheritance.

43
Q
What is the output?
public class Whiz{
                       static int x = 9;
                   public static void main(String[] args) {
                                   int x = 8;
                                   for(;x>-1;x--)
                                                   System.out.print(x);
                   }
    }
A

876543210

When there are two variables declared with same name in two different scopes, the closest scope variable overtakes other variable.

44
Q
What is the output?
class Test { 
    public static void main(String[] args) 
    { 
        String s = new String("hello"); 
        String s2 = "hello"; 
    if (s == s2) { 
        System.out.println("=="); 
    } 
        if (s.equals(s2)) { 
            System.out.println("equals"); 
        } 
    } 
}
A

equals

The String class equals method compares objects, but the == operator only compares references. If both the references are pointing to the same object then only the == operator returns true

45
Q
What is the output?
class Test { 
    public static void main(String[] args) 
    { 
        String s = new String("hello"); 
        String s2 = s; 
    if (s == s2) { 
        System.out.println("=="); 
    } 
        if (s.equals(s2)) { 
            System.out.println("equals"); 
        } 
    } 
}
A

==
equals

Because both the references are pointing to the same object so “==” printed and If both the reference are pointing to the same object so by default they the equal so “equals” printed.

46
Q
What is the output?
class Test { 
    public static void main(String[] args) 
    { 
        int marks = 90; 
        String result = marks > 35 ? "Pass" : "Fail"; 
        System.out.println(result); 
    } 
}
A

Pass

47
Q

What is the output?
class Test {
public static void main(String[] args)
{
int marks = 90;
String result = marks > 60 ? “Pass with 1st div.”
: marks < 50 ? “Pass with 2nd div.” :
marks < 40 ? “Pass with 3nd div.”;
System.out.println(result);
}
}

A

Compile Time Error

Because marks < 40 ? “Pass with 3nd div.” is not completed. Correction:
marks < 40 ? “Pass with 3nd div.”:”Fail” .

48
Q
What is the output?
class Test { 
    public static void main(String[] args) 
    { 
        String ta = "A "; 
        ta = ta.concat("B "); 
        String tb = "C "; 
        ta = ta.concat(tb); 
        ta.replace('C', 'D'); 
        ta = ta.concat(tb); 
        System.out.println(ta); 
    } 
}
A

A B C C

String are immutable to no replacing.

49
Q

Write the method as a lambda

void printHello() 
{ 
    System.out.println("Hello World "); 
}
A

() -> { System.out.println(“Hello World “); };

OR

() -> System.out.println(“Hello World “);

50
Q

Write the method as a lambda

void sayHello(String name) 
{ 
    System.out.println("Hello " + name); 
}
A

(name) -> { System.out.println(“Hello “ + name); };

OR

// {} optional 
(name) -> System.out.println("Hello " + name); 
OR 
// () optional for single input parameter. 
name -> System.out.println("Hello " + name);
51
Q

Write the method as a lambda

int add(int num1, int num2) 
{ 
    return num1 + num2; 
}
A

(int num1, int num2) -> { return num1 + num2; };

Or

(int num1, int num2) -> num1 + num2;

Or

// () mandatory for more than one input parameter. 
(num1, num2) -> num1 + num2;
52
Q

What is the output?

class Test { 
    public static void main(String[] args) 
    { 
        int a = 10; 
        int b = 20; 
    if (++a <= 10 && --b < 20) {} 
      System.out.println("Output of && operator: "
                       \+ "a = " + a + " b = " + b); 
    System.out.println("-------------"); 

    a = 10; 
    b = 20; 
    if (++a <= 10 & --b < 20) {} 
      System.out.println("Output of & operator: "
                      \+ "a = " + a + " b = " + b); 
}  }
A

Output of & operator: a = 11 b = 19

Why?
‘&&’ operator doesn’t check second operand if value for the first operand is ‘false’. But ‘&’ must check both the operands.

53
Q
  • Describe the relationship between source code and byte code
A

The source code is what we write (Person.java). The compiler uses it to generate byte code which is saved as a class (Person.class)