Java Programming Exam Questions and Answers, Exams of Computer Science

A comprehensive set of java programming exam questions and answers, covering fundamental concepts such as object-oriented programming, data types, operators, control flow statements, and object instantiation. It is a valuable resource for students preparing for java programming exams or seeking to reinforce their understanding of core java concepts.

Typology: Exams

2024/2025

Available from 01/09/2025

Upstudy
Upstudy 🇺🇸

2.8

(9)

14K documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
JAVA EXAM QUESTIONS AND
CORRECT ANSWERS
What do real-world objects contain? - ANSWER state and behaviour
Where is a software object's state contained? - ANSWER fields (Java) or
variables (C++)
Where is a software object's behaviour exposed? - ANSWER methods (Java)
or functions (C++)
Hiding internal data from the outside world, and accessing it only through
publicly exposed methods is known as data. - ANSWER encapsulation
A blueprint for a software object is called a. - ANSWER class
Common behavior can be defined in a ___ and inherited into a ___ using the
___ keyword. - ANSWER superclass, subclass, extends
A collection of methods with no implementation is called an ___. - ANSWER
interface
A namespace that organizes classes and interfaces by functionality is called a
___. - ANSWER package
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Java Programming Exam Questions and Answers and more Exams Computer Science in PDF only on Docsity!

JAVA EXAM QUESTIONS AND

CORRECT ANSWERS

What do real-world objects contain? - ANSWER state and behaviour Where is a software object's state contained? - ANSWER fields (Java) or variables (C++) Where is a software object's behaviour exposed? - ANSWER methods (Java) or functions (C++) Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data. - ANSWER encapsulation A blueprint for a software object is called a. - ANSWER class Common behavior can be defined in a ___ and inherited into a ___ using the ___ keyword. - ANSWER superclass, subclass, extends A collection of methods with no implementation is called an ___. - ANSWER interface A namespace that organizes classes and interfaces by functionality is called a ___. - ANSWER package

The term API stands for ___? - ANSWER application programmer interface The term "instance variable" is another name for. - ANSWER non-static field - instance variables are specific to an object The term "class variable" is another name for ___. - ANSWER static field - class variables are fixed and global across all instances of the class A local variable stores temporary state; it is declared inside a ___ - ANSWER method - for example a counter in a loop int i=0; A variable declared within the opening and closing parenthesis of a method signature is called a ____. - ANSWER parameter What are the eight primitive data types supported by the Java programming language? - ANSWER byte short long float double int char boolean The class ___ represents character strings. - ANSWER java.lang.String

is an assignment operator Explain the following code sample: result = someCondition? value1 : value2; - ANSWER if someCondition is true then assign value1 to result else if someCondition is false then assign value2 to result In the following program explain why the value "6" is printed out twice in a row: class PrePostDemo { public static void main(String[] args){ int i = 3; i++; System.out.println(i); // "4" ++i; System.out.println(i); // "5" System.out.println(++i); // "6" System.out.println(i++); // "6" System.out.println(i); // "7" } } - ANSWER i++ is a postfix operator. 1 will be added to 1 after it's printed. ++i is a prefix operator. If it had been used, the println would have been "7" What is a compound assignment operator? - ANSWER All of the arithmetic operators can also be combined with the simple assignment operator to produce compound assignments. So for instance the statements x+=1; and x=x+1; both are incrementing the value of x by one. The simplest control flow statement that the Java programming language has is the ___ statement. - ANSWER if then

The ___ statement allows for any number of possible execution paths. - ANSWER switch The ___ statement is similar to the while statement, but evaluates its expression at the ___ of the loop. - ANSWER do while end How do you write an infinite loop using the for statement? - ANSWER for ( ; ; ) { //do something } How would you write an infinite loop using the while statement? ANSWER while (true) { //do something } Consider the following code snippet. if (aNumber >= 0) if (aNumber == 0) System.out.println("first string"); else System.out.println("second string"); System.out.println("third string");

"private" modifier - ANSWER The private modifier specifies that the member is accessible only in its own class. "protected" modifier - ANSWER The protected modifier specifies that the member is accessible in its own package (as with package-private) and, in addition, by a subclass of its class in another package. What if class has no modifier? - ANSWER If class has no modifier (it is default or package-private), it is visible only within its own package (package are named groups of related classes) "static" modifier - ANSWER Sometimes you want to have variables that are common to all objects. This is accomplished with the static modifier. A field is called a class variable when it has the static modifier as part of its declaration. Class variables are associated with the class, rather than associated with any object. All instances of a class share a class variable: If it is located in one place in memory; there is only one copy of the class variable; Any object can access the class variable, but class variables can also be changed without making instances of the class. Class variables are referenced by the class name itself, as in Bicycle.numberOfBicycles This makes it clear that they are class variables, and not instance variables. "final" modifier - ANSWER use "final" to create a constant. The static modifier, used in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change.

For example, the declaration of the following constant, named PI, defines an approximate value for pi (the ratio of the circumference of a circle to its diameter): static final double PI = 3.141592653589793; "new" operator - ANSWER You instantiate a class by using the new operator with a constructor. The new operator returns a reference of the instantiated object. You can assign the reference to a variable or pass it as an argument to a method or use it directly. Following is an example of a class: public class IdentifyMyParts { public static int x = 7; public int y = 3; a) Which are class variables? b) Which are instance variables? c) What is the output from the following program, and why? IdentifyMyParts a = new IdentifyMyParts(); IdentifyMyParts b = new IdentifyMyParts(); a.y = 5;

} - ANSWER myRect is declared, but it is not instantiated or initialized. area() is not a method of Rectangle. The public method is getArea. The following code declares one array and one string object. How many objects does this code create in total? How many references to the objects exist after the code executes? Is either object eligible for garbage collection? . String[] students = new String[10]; String studentName = "Peter Parker"; students[0] = studentName; studentName = null;

. - ANSWER There is one reference to the students array and that array has one reference to the string Peter Smith. Neither object is eligible for garbage collection. How does a program destroy an object that it creates? - ANSWER set the value to null "enum" type - ANSWER An enumeration type is a type whose fields consist of a fixed set of constants. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week. "interface" type - ANSWER an interface is a reference type, somewhat similar to class, but it can contain only constants, method signatures and nested types.

The method bodies do not exist. Interfaces cannot be instantiated-they can only be implemented by classes or extended by other interfaces. An Interface defines a protocol of communication between two objects. An interface declaration contains signatures, but no implementations, for a set of methods, and may also contain constant definitions. A class that implements an interface must implement all the methods declared in the interface. An interface name can be used anywhere a type can be used. What access control applies to interfaces? - ANSWER All methods declared in an interface are implicitly public, so the public modifier can be omitted. Besides method declarations, an interface can also contain constant declarations. By default, all constant values defined in an interface are implicitly public, static, and final. Again, these modifiers can be omitted, as shown below. What does casting have to do with interfaces? - ANSWER If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface. Here's an example of a method to find the largest object in a pair of objects, for any objects that are instantiated from a class that implements Relatable:

Returns a string containing the characters in this sequence in the same order as this sequence. What is wrong with the following interface? public interface SomethingIsWrong { void aMethod(int aValue){ System.out.println("Hi Mom"); }

  • ANSWER aMethod() should not have a method body. To correct it, eliminate the {Sytem.out.println("Hi Mom");} Is the following interface valid? public interface Marker { } - ANSWER No, there is no requirement to contain constants or method signatures. In fact, empty interfaces can be used as types and to mark classes without requiring any particular method implementations. For an example of a useful empty interface see java.io.Serializable. varargs construct - ANSWER You can use a construct called varargs to pass an arbitrary number of values to a method. You use varargs when you don't know how many of a particular type of argument will be passed to the method. It's a shortcut to creating an array manually To use varargs, you follow the type of the last parameter by an ellipsis (three dots,.), then a space, and the parameter name. The method can then be called with any number of that parameter, including none.

Why should I gather classes and interfaces into a package? - ANSWER 1. You and other programmers can tell at a glance that these types are related.

  1. You and other programmers know where to find types that can provide related functions.
  2. The names of your types won't conflict with the type names in other packages because the package creates a new name space.
  3. You can allow types within the package to have unrestricted access to one another yet still restrict access for types outside the package.