Ñòóäîïåäèÿ
rus | ua | other

Home Random lecture






Java Basics Tests (“Cert. Java 7”)


Date: 2015-10-07; view: 634.


D1? D1

How a Disk cache Works

Disk caching works in essentially the same way whether you have a cache on your disk controller or you are using a software-based solution. The CPU requests specific data from the cache. In some cases, the information will already be there and the request can be met without accessing the hard disk.

If the requested information isn't in the cache, the data is read from the disk along with a large chunk of adjacent information. The cache then makes room for the new data by replacing old. Depending on the algorithm that is being applied, this may be the information that has been in the cache the longest, or the information that is the least recently used. The CPU's request can then be met, and the cache already has the adjacent data loaded in anticipation of that information being requested next.

 

CPU 3 3 3 5 Cache supplies CPU with requested data

6 1 CPU requests new data 5

6 5 4 Cache replaces old data with new data

A1 C2 B3 A4 D1C2 B3D2

C3 A2 C1 B2 Cache (before) C3 D3 C1 B2 " A1A2A3A4 Cache (after)

B4 C4 B1 A3B4 C4 B1D4

6 5 3 Cache reads data from multiple sectors on disk,

D1? D1D2D3D4including data adjacent to that requested

6 5

2 Data is not found in cache. Cache requests data from hard disk.

B Re-read the text to find the answers to these questions:

 

1 Match the terms in Table A with the statements in Table B.

TABLE A TABLE B

A Cache hit 1 The process of writing changes only to the cache and not to main

B Cache controller memory unless the space is used to cache new data

C Cache coherency 2 The amount of data transferred to the cache at any one time

D Write-through cache 3 The process of writing directly to both the cache and main memory

E Line size at the same time

4 The process is successful in finding the data in the cache

5 Ensuring that any changes written to main memory are reflected

within the cache and vice versa

6 The logic circuits used to control the cache process

 

2Mark the following statements as True or False:

 

A Cache memory is faster than RAM.

B The processor looks for data in the main memory first.

C Write-through cache is faster than write-back cache.

D Write-back cache requires a more intelligent cache controller.

E Most programs use instructions that are stored in sequence in memory.

F Most cache controllers transfer one item of data at a time.

G Hardware and software disk caches work in much the same way.

 

 

Q1)What are the valid components of a Java class (choose all that apply):

a) package statement

b) import statements

c) methods

d) variables

e) Java compiler

f) Java runtime environment

 

Answer) a, b, c, d

Explanation: The Java compiler and Java runtime environment aren't components of a Java

class.

 

Q2)The following is the set of Java class components listed numerically in order. Select the correct order of their occurrence in a Java class (choose all that apply):

 

1 - Comments

2 - import statement

3 - package statement

4 - methods

5 - class declaration

6 – variables

 

a) 1, 3, 2, 5, 6, 4

b) 3, 1, 2, 5, 4, 6

c) 3, 2, 1, 4, 5, 6

d) 3, 2, 1, 5, 6, 4

 

Answer) a, b, d

Explanation: The comments can appear anywhere in a class. They can appear before and after package, and import statements. They can appear before or after a class, method, or variable declaration.

The first statement (if present), in a class should be a package statement. It cannot be placed after an import statement, or a declaration of a class.

The import statement should follow a package statement and be followed by a class declaration.

The class declaration follows the import statements, if present. It is followed by the

declaration of the methods and variables.

The answer option (c) is incorrect. None of the variables or methods can be defined before the definition of a class or an interface.

 

 

Q3)Which of the following examples define the correct Java class structure?

a)

#connect java compiler;

#connect java virtual machine;

class EJavaGuru {}

b)

package java compiler;

import java virtual machine;

class EJavaGuru {}

c)

import javavirtualmachine.*;

package javacompiler;

class EJavaGuru {

void method1() {}

int count;

}

d)

package javacompiler;

import javavirtualmachine.*;

class EJavaGuru {

void method1() {}

int count;

}

e)

#package javacompiler;

$import javavirtualmachine;

class EJavaGuru {

void method1() {}

int count;

}

f)

package javacompiler;

import javavirtualmachine;

Class EJavaGuru {

void method1() {}

int count;

}

 

 

Answer) d

Explanation: The answer option (a) is incorrect because #connect isn't a statement in Java.

For your quick information, # is used to add comments in UNIX.

 

Q4)Given the following contents of Java source code file MyClass.java, select the correct options:

 

// contents of MyClass.java

 

package com.ejavaguru;

 

import java.util.Date;

 

class Student {}

class Course {}

 

a) The imported class, java.util.Date, can only be accessed in class Student.

b) The imported class, java.util.Date, can be accessed by both Student and

Course classes.

c) Both classes Student and Course are defined in the package com.ejavaguru.

d) Only class Student is defined in the package com.ejavaguru. Class Course gets

defined in the default Java package.

 

 

 

Answer) b, c

Explanation: You can define multiple classes, interfaces, and enums in a Java source code file.

Option (a) is incorrect. The import statement applies to all the classes, interfaces, and enums defined within the same Java source code file.

Option (d) is incorrect. If a package statement is defined in the source code file, all the classes, interfaces, and enums defined within it will exist in the same Java package.

Q5)Given the following definition of class EJavaGuru:

 

class EJavaGuru {

public static void main(String[] args) {

System.out.println (args[1]+":"+ args[2]+":"+ args[3]);

}

}

 

What is the output of the previous class, if it is executed using the following command?

 

java EJavaGuru one two three four

 

a) one:two:three

b) EJavaGuru:one:two

c) java:EJavaGuru:one

d) two:three:four

 

 

 

 

Answer) d

Explanation: The command-line arguments passed to the main method of a class do not contain the word Java and the name of the class.

Because the position of an array is zero based, the method argument is assigned the

following values:

 

args[0] -> one

args[1] -> two

args[2] -> three

args[3] -> four

 

And the class prints two:three:four.

Q6)Which of the following options, when replaced with //INSERT CODE HERE, will print out EJavaGuru?

 

public class EJavaGuru {

// INSERT CODE HERE

{

System.out.println ("EJavaGuru");

}

}

 

a) public void main (String[] args)

b) public void main(String args[])

c) static public void main (String[] array)

d) public static void main (String args)

e) static public main (String args[])

 

 

Answer) c

Explanation: Option (a) is incorrect. This option defines a valid method, but not a valid main method. The main method should be defined as a static method, which is missing from the method declaration in option (a).

Option (b) is incorrect. This option is identical to the method defined in option (a) – with one difference. In this option, the square brackets are placed after the name of the method argument. The main method accepts an array as a method argument. To define an array, the square brackets can be placed either after the data type or the method argument name.

Option (c) is correct. Extra spaces in a class are ignored by the Java compiler.

Option (d) is incorrect. The main method accepts an array of String as a method

argument. The method in this option accepts a single String object.

Option (e) is incorrect. It isn't a valid method definition, and does not specify the return type of the method. This line of code will not compile.

Q7)Select the correct options:

 

a) You can start execution of a Java application through the main method.

b) Java compiler calls and executes the main method.

c) Java runtime environment calls and executes the main method.

d) A class calls and executes the main method.

 

Answer) a, c

Explanation: The Java runtime environment calls and executes the main method.

Q8)A class Course is defined in package com.ejavaguru. Given that the physical location of its corresponding class file is /mycode/com/ejavaguru/Course.class, and execution takes place within the “mycode” directory, which of the following lines of code when replaced with

 

// INSERT CODE HERE,

will import it in class MyCourse?

 

// INSERT CODE HERE

class MyCourse {

Course c;

}

 

a) import mycode.com.ejavaguru.Course;

b) import com.ejavaguru.Course;

c) import mycode.com.ejavaguru;

d) import com.ejavaguru;

e) import mycode.com.ejavaguru*;

f) import com.ejavaguru*;

 

 

 

Answer) b

Explanation: Option (a) is incorrect. The path of the imported class, used in an import statement isn't related to the class's physical location. It reflects the package and subpackage a class is in.

Option (c) and (e) are incorrect. The class's physical location isn't specified in the import statement.

Option (d) and (f) are incorrect. ejavaguru is a package. To import a package and its members, it should be followed by .*, as follows:

import com.ejavaguru.*;

Q9)Examine the following code:

 

class Course {

String courseName;

}

 

class EJavaGuru {

public static void main(String args[]) {

Course c = new Course();

c.courseName = "Java";

System.out.println (c.courseName);

}

}

 

Which of the following statements will be true if the variable courseName is defined as a private variable?

 

a) class EJavaGuru will print Java.

b) class EJavaGuru will print null.

c) class EJavaGuru will not compile.

d) class EJavaGuru will throw an exception at runtime.

 

 

 

Answer) c

Explanation: If the variable courseName is defined as a private member, it won't be accessible from the class EJavaGuru. An attempt to do so will cause it to fail at compile time. Because the code won't compile, it can't execute.

Q10)Given the following definition of class Course:

 

package com.ejavaguru.courses;

 

class Course {

public String courseName;

}

 

What is the output of the following code?

 

package com.ejavaguru;

import com.ejavaguru.courses.Course;

 

class EJavaGuru {

public static void main(String args[]) {

Course c = new Course();

c.courseName = "Java";

System.out.println (c.courseName);

}

}

 

a) Class EJavaGuru will print Java.

b) Class EJavaGuru will print null.

c) Class EJavaGuru will not compile.

d) Class EJavaGuru will throw an exception at runtime.

 

 

 

Answer) c

Explanation: The class will fail to compile because a non-public class cannot be accessed outside a package in which it is defined. The class Course therefore cannot be accessed in the class EJavaGuru, even if it is imported explicitly in it. If the class itself isn't accessible, there's no point in accessing a public member of a class.

Q11)Given the following code, select the correct options:

 

package com.ejavaguru.courses;

 

class Course {

public String courseName;

 

public void setCourseName(private String name) {

courseName = name;

}

}

 

a) You can't define a method argument as a private variable.

b) A method argument should either be defined with public or default accessibility.

c) For overridden methods, method arguments should be defined with protected

accessibility.

d) None of the above.

 

 

Answer) a

Explanation: You cannot add any explicit accessibility keyword to the method parameters. If you do, the code won't compile.


<== previous lecture | next lecture ==>
Cache Memory | The Position
lektsiopedia.org - 2013 ãîä. | Page generation: 1.407 s.