OCA Java SE 8 Programmer 1 - Chapter 1

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

The members of a class defined using the protected access modifier are accessible to

- Derived classes in the same package - Unrelated classes in the same package - Derived classes in a separate package (using inheritance, not reference variable)

The main method should comply with the following rules

- The method must be marked as a public method - The method must be marked as a static method - The name of the method must be main - The return type of this method must be void - The method must accept a method argument of a String array or a variable argument (varargs) of type String

Public access modifier allows the field to be accessible to:

-Derived classes in the same package -Derived classes in a separate package -Unrelated classes in the same package -Unrelated classes in a separate package

How many access modifiers are there?

4 public (least restrictive) protected default private (most restrictive)

Examine the following content of Java source code file Multiple.java and select the correct options: //Contents of Multiple.java public interface Printable {} interface Movable{} a. A Java source code file can't define multiple interfaces b. A Java source code file can only define multiple classes c. A Java source code file can define multiple interfaces and classes d. The previous class will fail to compile

A and B are incorrect C is correct D is also correct - The name of a public class must match the name of the java source code file. The public interface Printable cant be in the Multiple.java file. It would need to be Printable.java

What can access a protected class?

A derived class can inherit and access protected members of its base class, regardless of the package in which its defined. ^A derived class in a separate package can't access protected members of its base class using reference variables

What happens to a method when you attach the nonaccess modifier 'final' to it?

A final method defined in a base class can't be overridden by a derived class.

Examine the following content of Java source code file Multiple2.java and select the correct options: //contents of Multiple2.java interface Printable {} class MyClass {} interface Movable {} public class Car {} public interface Multiple 2 {} a. The code fails to compile b. the code compiles successfully c. Removing the definition of class Car will compile the code d. Changing the class Car to a non public class will compile the code e. Changing interface Multiple2 to a nonpublic interface will compile the code

A is correct and B is incorrect - multiple public classes/interfaces. Car is public under Multiple2.java C is correct - removing Car will mean there is only one public field D is correct - making Car nonpublic will mean there is only one public field E is incorrect - there is still a public field under a non matching java source code file present

How can you access a static method?

A static method can be accessed using the name of the object reference variables and the class in a manner similar to static variables

What can and can't access modifiers be applied to ?

Access Modifiers CAN be applied to classes, interfaces, and their members (instance & class variables & methods). Access modifiers CANNOT be applied to local variables & method parameters (will fail to compile)

Why is adding the word abstract to an interface redundant?

Because an interface is an abstract entity by default. The Java compiler automatically adds the keyword abstract to the definition of an interface.

Do you always have to prefix classes and interfaces with the package when you use them?

Classes and interfaces in the same package can use each other without prefixing their names with the package name. But to use a class or an interface from another package, you must use its fully qualified name. packageName.anySubpackageName.ClassName

How can the access of default and protected access modifiers be easily remembered?

Default can be compared to package-private and protected access can be compared to package private + kids. Kids being derived classes. Kids can access protected methods only by inheritance and not by reference (dot operater).

What are overloaded methods?

If a class defines a main method that doesnt match the signature of the main method. They are methods with the same name but different signatures.

Where do you put the import statement?

If a package statement is present in a class, the import statement must follow the package statement. Reversing this order will cause the package to fail to compile

What is encapsulation?

In object-oriented computer programming languages, the notion of encapsulation (or OOP Encapsulation) refers to the bundling of data, along with the methods that operate on that data, into a single unit. In java, you can control the level of access and modifications to your objects.

What is an instance variable?

Instance variables store the state of an object (model, weight, company). Defined within a class but outside all methods.

What happens to a variable when you attach the nonaccess modifier 'static' to it?

It becomes a shared variable by all objects of a class, common to all instances of a class. Once you update it, its changed for all objects. Common to define constants as static. Preferred way to access static members is by class name.

What happens to a class when you attach the nonaccess modifier 'final' to it?

It can't be extended by another class

What happens to a class when you attach the nonaccess modifier 'abstract' to it?

It can't be instantiated (represented as an instance)

What is important to note about the abstract method?

It doesn't have a body. Note: If it has {} it still has a body - just an empty one.

How restrictive is the public access modifer?

It is the least restrictive access modifier. Classes and interfaces defined using the public access modifier are accessible across all packages, from derived to unrelated classes.

What is Automatic Memory Management?

Java has garbage collectors that reclaim memory from objects that are no longer in use. This frees developers from managing memory and prevents memory leaks.

how are multiline and end of line comments formatted?

Multi line starts with /* and ends with */ End of line is just // after code

Can an concrete class define an abstract method?

NO. An abstract class may or may not define an abstract method. But a concrete class can't define an abstract method.

What are the Java class components?

Package statement Import Statements Comments Class Declaration - Variables - Comments - Constructors - Methods

What are some of the valid features and components of Java?

Platform Indepence Object Orientation Abstraction Encapsulation Inheritance Polymorphism Type Safety Automatic Memory Management Multithreading and concurrency Security

What are some of the irrelevant features and components of Java?

Single-Threaded Relation to JavaScript

What order are classes & interfaces defined in?

The classes and interfaces can be defined in any order of occurences in a java source code file.

What is a nonaccess modifier?

They change the default behavior of a Java class and its members.

Will this compile? Why/Why not? class MyClass { static int x = result(); static int result() {return 20;} int nonStaticResult() {return result();} }

This will compile. Static variables referencing static method. Non static method using static method. All good.

Will this compile? Why/Why not? class MyClass { static int x = count(); int count() {return 10;} }

This will not compile because you have a static method referencing a non static method

How is class behavior defined?

Using methods which may include method parameters

What is Platform Independence?

WORA - write once, run anywhere. Java code can be executed on multiple systems without recompilations. If you have a JVM on your machine, it will interpret the byte code accordingly.

Can you have more than one executable class in a java application?

Yes but we choose one (and exactly one) when the time comes to start its execution

Are classes & interfaces imported using the import statement available to all the classes & interfaces defined in the same java source code file ?

Yes. Classes & interfaces imported using the import statement are available to all the classes & interfaces defined in the same java source code file.

Can you access static members from a null reference?

Yes. You can access static variables and methods using a null reference. Because static variables and methods belong to a class and not to an instance, you can access them using variables, which are initialized to null. Such code won't throw a runtime exception (NullPointerException)

Which will compile? a) public static void main (String args ... ) b) public static void main (String ... args )

b) will compile, a) won't compile. To define a variable argument variable, the ellipsis (...) must follow the type of the variable and not the variable itself.

What can the public access modifier be applied to ?

it can be applied to: - top level class, interface, enum - classes variables and methods - instance variables and methods it cannot be applied to: - method parameter and local variables

When you define a public class or an interface in a java source file, the names of the class or interface and Java source file must...

match!

How do you define an array?

square brackets [ ] They can follow either the variable name or its type: public static void main (String [ ] args) public static void main(String minniemouse [ ])

Java source code File

used to define Java entities such as class, interface, enum and annotation

Can you define a combination of classes and interfaces in the same java source code file?

yes!

What is security (java) ?

- type safe and includes garbage collection - provides secure class loading and verification ensures execution of legitimate Java code - the java platform defines multiple APIs including cryptography and public key infrastructure - Can execute under security manager to control access to your resources. - Access to a resource can be controlled - Enables you to define digital signatures, certificates, and keystores to secure code and file exchanges - Secures state of its objects with encapsulation and data hiding - Java applets execute in browsers and don't allow code to be downloaded to a system (enabling security for browsers)

The members of a class defined using the default access modifier are accessible to

-Derived classes in the same package -Unrelated classes in the same package Package-private.

The declaration of a class is composed of the following parts

1. Access modifiers 2. Nonaccess Modifiers 3. Class Name 4. Name of the base class, if the class is extending another class 5. All implemented interfaces if the class is implementing any interfaces 6. Class Body (class fields, methods, constructors), included within a pair of curly braces {}

What are the 6 package rules to remember?

1. Package names should all be in lowercase 2. The package and subpackage names are separated using a (.) 3. Package names follow the rules defined for valid identifiers in Java 4. For classes and interfaces defined in a package, the package statement is the first statement in a Java source file (a .java file). The exception is the comments can appear before or after a package statement 5. There can be a maximum of one package statement per Java source code file (.java file) 6. All the classes and interfaces defined in a Java source code file are defined in the same package. They can't be defined in separate packages.

What is the definition of a class?

A class is a design used to specify the attributes and behavior of an object. A class is a design from which an object can be created.

What is a top-level class?

A class that isnt defined with any other class

How is an instance variable different from a class variable?

A class variable is static and is shared by all the objects of a class. Changing the value of an instance variable for one object wont change the value of an instance variable for another.

Package

All Java classes are part of a package. If not named, default package (no name)

What is Inheritance?

It is a mechanism where you can to derive a class from another class for a hierarchy of classes that share a set of attributes and methods. In java you can inherit classes and implement interfaces and this save you from redefining common code.

What happens to an interface when you attach the nonaccess modifier 'final' to it?

It will fail to compile. You can't mark an interface as final.

What are the mandatory and optional components of a class?

Mandatory: - Keyword class - Name of Class - Class body marked by opening and closing curly brackets Optional: - Access modifer - Nonaccess modifier - Keyword "extends" with base class and "implements" with interface, you don't have to have a base class or interface

What is Polymorphism?

Mean many forms. Java enables instances of classes to exhibit multiple behaviors for the same method calls.

What is Multithreading and concurrency?

Multithreading refers to a process of executing two or more threads simultaneously for maximum utilization of the CPU. Concurrency is the ability to run several or multi programs or applications in parallel.

Are the keyword java and the class name passed on to the main method?

NO. You pass Method Parameters to the main method (also called command line parameters or command line values). EX: java HelloExamWithParameters 1 2 ^ 1 and 2 are the method parameters

What can a static method access?

Neither static methods nor static variables can access the non-static variables and methods of a class. But the reverse is true: Non static variables and methods can access static variables and methods. This is because the static members of a class exist even if no instances of the class exist. Static members are forbidden from accessing instance methods and variables, which can exist only if an instance of the class is created

Can every access modifier be applied to all java entities?

No

Can static methods and variables access the instance variables of a class?

No

Can variables be defined with the nonaccess modifier abstract?

No

Does importing more classes increase the size of your own class?

No

Is Java Single threaded?

No, but you can code a single thread. But java will execute its own processes and operate multi threaded.

Can classes and interfaces defined in the same Java source code file be defined in separate packages?

No, classes and interfaces defined in the same Java source code file cant be defined in separate packages.

Can members of a named package access classes and interfaces defined in the default package?

No. A class from a default package can't be used in any named packaged class, regardless of whether they're defined in the same directory or not.

Are Class and class interchangeable in Java?

No. Java is case sensitive. The keyword class must be lowercase

Is Java related to JavaScript?

No. Just a similar name. JavaScript is for making web pages interactive.

Is it compulsory to define all the attributes of a class before its methods?

Nope ! (but keep readability in mind)

What is object orientation?

The concept of "objects", which can contain data and code: data in the form of fields, and code, in the form of procedures. A feature of objects is that an object's own procedures can access and often modify the data fields of itself.

How do you import classes from the default package?

The default package is automatically imported in the Java classes and interfaces defined within the same directory on your system.

What makes a class executable?

The main method. The first requirement in creating an executable Java application is to create a class with a method whose signature (name and method arguments) matches the main method.

What happens to a method when you attach the nonaccess modifier 'static' to it?

The method is not associated with objects and cant use any of the instance variables of a class. Define static methods to access or manipulate static variables. You can't override the state static methods of a derived class but you can redefine them.

How is the state of a class defined?

The state of a class is defined using attributes or instance variables

What is abstraction?

You can abstract objects and include only the required properties and behavior in your code. Abstraction is used to hide background details or any unnecessary implementation about the data so that users only see the required information.

How do you import a static member?

You can import an individual static member of a class or all its static members using the import static statement. Static Members are better accessed by prefixing their name with the class or interface names. By using static import, you can drop the prefix and just use the name of the static variable or method. We call this static imports, but the syntax is *import static*

How do you import everything from a package?

You can import the all the classes with an asterisk! import certification.* NOTE: You can't import classes from a subpackage by using an asterisk in the import statement.. You will only import the classes from the package. So if you have a package with 2 subpackages and 1 class - it will only import that one class, not the classes within the subpackages.

What happens to a variable when you attach the nonaccess modifier 'final' to it?

You can't reassign the variable.

What if you have multiple classes or interfaces with the same name from different packages?

You can't use the import statement for that. You just have to use their fully qualified names. import java.util.Date; import java.sql.Date; class AnnualExam {} ^will not compile You can do: import java.util.Date; class AnnualExam{ Date date1; java.sql.Date date2;

Do you need to import java.lang?

You don't need an explicit import statements to use members from the java.lang package. Classes and interfaces in this package are automatically imported in all other Java classes, interfaces, or enums.

What is type safety?

You must declare a variable with its data type before you can use it. Compile Checks ensure you never assign the wrong type to a variable.

Executed code: java EJava java one one Output: java one Which one is right? a. class EJava { public static void main(String sun[] ){ System.out.println(sun[0] + " " + sun[2]); }} b. class EJava { static public void main(String phone[] ){ System.out.println(phone[0] + " " + phone[1]); }} c. class EJava { static public void main(String[] arguments[] ){ System.out.println(arguments[0] + " " + arguments[1]); }} d. class EJava { static void public main(String args[] ){ System.out.println(args[0] + " " + args[1]); }}

a and b are correct c is a 2 dimensional array and will not be treated as the main method d won't compile because void cannot be before public

What does this output? a. String Name = /* Harry */ "Paul"; System.out.printIn(name); b. String Name = "/* Harry */ Paul"; System.out.printIn(name); c. String Name = "Shre /* ya */ Paul"; System.out.printIn(name);

a. Paul b. */ Harry*/ Paul c. Won't Compile because the value assigned to name is an unclosed string literal value

Which will not fail? a. class Course { } package certification; b. class Course{ package com.cert; } c. package com.cert; package com.exams; class Course{ } d. Package certification; class Course { }

a. will fail because package can't come after class declaration class Course { } package certification; b. will fail because package statement cannot be within the class class Course{ package com.cert; } c. will fail because a class can't define multiple package statements package com.cert; package com.exams; class Course{ } d. Will succeed Package certification; class Course { }

How can you declare a class Curtain in a package building so that it isn't visible outside the package building? a. package building; public class Curtain {} b. package building; protected class Curtain {} c. package building; class Curtain {} d. package building; private class Curtain {}

c is correct (default) a is accesible to outside package b and d - top level class cant be private or protected

What can the private access modifier be applied to ?

it can be applied to: - classes variables and methods - instance variables and methods it cannot be applied to: - top level class, interface, enum - method parameter and local variables

What can the protected access modifier be applied to ?

it can be applied to: - classes variables and methods - instance variables and methods it cannot be applied to: - top level class, interface, enum - method parameter and local variables

A source code file can't define __ __ __ public class or interface

more than one

If you include a package and an import statement in Java source code file Multiple.java, the interfaces & classes defined become a part

of the same package that was defined (pg35) package com.manning.code; import com.manning.*; interface Printable{} interface Movable {} ^Printable and Movable are now a part of the package com.manning.code because of the import statement, all classes and interfaces define in package com.manning are accessible to Printable & Movable

Members of a classes and interfaces defined with the private access modifier are accessible to

only the classes and interfaces in which they are defined

A top-level class can be defined only by using the ___ or ___ access modifiers

public or default


Kaugnay na mga set ng pag-aaral

CSS overflow, float and clear, align

View Set

Week 2 Valvular Disorders and Pulmonary Embolism

View Set

Chapter 29: PrepU - Contraception and Unplanned Pregnancies

View Set

Chapter 14 InQuizitive: The Bureaucracy

View Set

2022 A&P General Written Exam - Mathematics

View Set

OB Chapter 4: Common Reproductive Issues

View Set

Unit Practice Test 5 (Chapter 9 Exam)

View Set