ITP 120 Chapter 1 Study Guide, ITP 120 MIDTERM EXAM STUDY GUIDE, ITP 120 Chapters 1-6

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

What steps must the programmer take to create an executable Java program?

First, the programmer must create the source code. The programmer creates a Java class that is contained in a file with the same name as the class. The source code file ends with the .java extension. Next, the programmer uses a tool called a compiler to translate the source code into Java byte-code. If the source code is syntactically correct, a file containing the byte-code is created. This file has the same name as the source code file with one exception, the byte-code file ends in the .class extension. Once the .class is generated, it may be executed on any computer that has the Java Virtual Machine (JVM) installed on it.

Which operator is used to concatenate two strings?

+

Which of the following is not a primitive type? Answers: 1 char 2 float 3 int 4 String

4

Which of the following statements is true? Answers: 1 A while statement cannot be nested inside another while statement. 2 An if statement cannot be nested inside another if statement. 3 A while statement cannot be nested inside an if statement. 4 None of these is true.

4

Which of the following terms is not used to refer to a sentinel value that breaks out of a while loop? Answers: 1 Signal value 2 Dummy value 3 Flag value 4 Maximum value

4

Define high-level languages, machine language and low-level language. Explain how the languages correspond to one another.

High-level programming languages were designed to be easy for humans to read and use. High-level languages consist of English like statements. Machine language or low-level language is the language that the computer understands directly. The source code created with Java, a high-level language, is compiled or translated into the machine language, or object code. The object code is the program that executes on the computer.

Things an object knows about itself are called

Instance Variables. Instance variables represent the objects state (the data) and can have unique values for each object of that type.

What is echoing?

It is a good idea to echo the user's input back to them after they have entered it so that they can ensure its accuracy. This allows user to make sure they put in right input. Echoing may display subtle errors. Ex: user enters 5 display 5 in output.

Looping

Java has three types of loop statements: the while, do-while, and for statements. Most branching and looping statements are controlled by Boolean expressions.

Enumerations

Java permits enumerated types. An enumerated type is a type in which all the values are given in a (typically) short list. The definition of an enumerated type is normally placed outside of all methods in the same place that named constants are defined: enum TypeName {VALUE_1, VALUE_2, ..., VALUE_N};

Package creation and modififcation

Java uses packages to form libraries of classes. A package is a group of classes that have been placed in a directory or folder, and that can be used in any program that includes an import statement that names the package. The import statement must be located at the beginning of the program file: Only blank lines, comments, and package statements may precede it. To make package, group all classes into a single directory (folder) and add package package_name. All classes in current directory belong to unnamed package called default package.

Where are standard code libraries?

Libraries in Java are called packages. A package is a collection of classes that is stored in a manner that makes it easily accessible to any program. In order to use a class that belongs to a package, the class must be brought into a program using an import statement

The hardest kind of error to detect in a computer program is a:

Logic error

What is the output produced by the following lines of code? int value1 = 3; int value2 = 4; int result = 0; result = value1++ * value2--; System.out.println("Post increment/decrement: " + result); result = ++value1 * --value2; System.out.println("Pre increment/decrement: " + result);

Post increment/decrement: 12 Pre increment/decrement: 10

Whats object-oriented language?

Programming methods that view program as consisting of objects that interact with each other by means of actions (methods)

Sorts

Sorting an Array. This example uses the partially filled array class that has been written before and Selection sort to sort the elements of the array from smallest to largest. Instructors that do not want to discuss sorting at this point in a course can leave this example out and come back to it at a more appropriate time.

Standard methods from Math and Object)

Standard methods from Math class include: Math.pow, Math.abs, Math.min, Math.max, Math,round, Math.ceil, Math.floor, and Math.sqrt, and math.random.

How to call/implement static methods and nonstatic methods

Static Methods. This type of method can be called without the creation of a calling object for it. The keyword static in the method definition shows that this method is static. You cannot call non-static methods from within a static method.

Types of Errors in Compile?

Syntax or logic errors. Syntax errors are errors with the code itself not compiling. Often caused by incorrect lines of code or statements. Logic errors are harder to spot because program runs fine on the surface but produces an incorrect result.

Printf How to format output (lots to review here)

System.out.printf takes arguments along with the data to be outputted to format the output as specified by the user. The Java method printf is similar to the print method. Like print, printf does not advance the output to the next line. System.out.printf can have any number of arguments. The first argument is always a format string that contains one or more format specifiers for the remaining arguments. All the arguments except the first are values to be output to the screen

Correct code for println?

System.out.println()

What is the dot (.) operator used for?

The dot operator (.) gives you access to an object's state and behavior (instance variables and methods).

How is the % (modulus) operator used? What is the common use of the modulus operator?

The modulus operator is used to return the remainder in integer division. For example, the modulus operator is commonly used to determine if a number is an even or an odd value.

Preconditions/postconditions

The precondition of a method states what is assumed to be true when the method is called. The postcondition of a method states what will be true after the method is executed, as long as the precondition holds

What does the String method trim() do? Give an example of its use.

The string method trim removes leading and trailing white space, as well as the tab and new-line character from String objects. For example, the following is an invocation of trim(): String s = userInput.trim(); where s represents the new String with no white space, tab or new-line characters.

What is the syntax and semantics of a programming language?

The syntax of a language describes the arrangement of words and punctuations that are legal in the language. The syntax of a language is synonymous with the grammar of a language. The semantics of a language describes the meaning of things written while following the syntax rules of the language. The syntax describes how you write a program and the semantics describes what happens when you run the program.

An advantage of using the Unicode character set is that it easily handles languages other than English.

True

Java began as a language for home appliances.

True

Java is an interpreted language.

True

The Java programming language allows you to concatenate two strings using the plus sign.

True

The modulus operator, %, returns the remainder of integer division.

True

The result of integer division is truncated in Java.

True

In Java, source code is compiled into object code called ______________.

Byte-code

Why is using named constants a good programming practice?

Using named constants is a good programming practice because it is less prone to the introduction of bugs into the source code. A retail program that calculates customer purchases needs to include sales tax on the amount of purchase. The sales tax percentage seldom changes, therefore using a named constant whose value is set once reduces the risk of the programmer mistyping the percentage, which would in turn introduce a logic error, the hardest error to track.

What is the value of the variable amountDue? double price = 2.50;double quantity = 5; double amountDue = 0; amountDue = price * quantity;

Value of amount due is price (2.50) * quantity (5) which gets you 12.5

Java is an object-oriented programming language. An object-oriented language

Views a program as consisting of objects which communicate through interactions.

Identify the invalid Java identifier.

Week

Wrapper Classes

Wrapper classes provide a class type corresponding to each of the primitive types. This makes it possible to have class types that behave somewhat like primitive types. The wrapper classes for the primitive types byte, short, long, float, double, and char are (in order) Byte, Short, Long, Float, Double, and Character. Wrapper classes also contain a number of useful predefined constants and static methods. Each java primitive has a corresponding wrapper class

The escape sequence the represents the new-line character is:

\n

Boolean / OR

a Boolean expression is an expression that is either true or false. Simplest Boolean expressions compare value of two expressions. Ex: your score == my score. When two Booleans are combined using OR (||) operator, entire expression is true as long as one of the expression is true.

What is the escape sequence?

a backslash \ immediately preceding a character (without any space). Character following / no usual meaning, formed using two symbols but single character. Ex: \n = New Line

Constants

any item in java which has one certain value that cant change. Ex: rate = 10. (can be of most data types)

In Java, the equal sign is used as the ___________ operator.

assignment

List the primitive data types Java supports. Indicate the kind of values each type can store.

boolean , true or false char, single Unicode character byte, integer (8 bits) short, integer (16 bits) int, integer (32 bits) long, integer (64 bits) float, floating-point number (32 bit IEEE 754) double, floating-point number (64 bit IEEE 754)

The controlling expression for a switch statement includes all of the following types except:

controlling expression in switch can include char, int, short, or byte. NO STRING.

Variable visibility circumvention (read: Directly altering private variables, etc.)

In java, hiding details is done by making them private. The modifier public means that there are no restrictions on where an instance variable or method can be used. The modifier private means that an instance variable or method cannot be accessed by name outside of the class. It is considered good programming practice to make all instance variables private. Most methods are public, and thus provide controlled access to the object

Variable scope, types, uses, and visibilities (local, global, static, nonstatic, private, public, etc.)

Local Variables. You can have instance variables that are accessible to the entire class. You can also create variables inside a method that are only accessible within that method. With these variables, you do not need to give them a visibility, just a type and a name. You can then use these variables in any computation within the method. Global Variables. This type of variable in not available in Java. The modifier public means that there are no restrictions on where an instance variable or method can be used. The modifier private means that an instance variable or method cannot be accessed by name outside of the class

Loops

Loops in Java are similar to those in other high-level languages. Java has three types of loop statements: the while, the do-while, and the for statements. The code that is repeated in a loop is called the body of the loop. Each repetition of the loop body is called an iteration of the loop.

Things an object can do are called

Methods. Methods operate on data (instance variables)

Define the terms class, object, method and method call.

Objects are entities that store data and can take actions. A class is the name for a type whose values are objects. Methods are defined as the actions that an object can take. A method call is invoked with the dot operator by the calling object, usually a variable of some type. Any information, called arguments, needed by the called method is passed in parenthesis. Objects have a collection of methods.

Method signatures

The signature of the method consists of its visibility, return type, method name, and parameter list. This term can also be introduced right when methods are first introduced in the text.

What is the value of the variable c in the statements that follow? String phrase = "Make hay while the sun is shining."; char c = phrase.charAt(10);

h

Data Types ex: Int, strings

int (integer, 8 bytes, -high num to + high num). double (floating point num. 8 bytes +-). Char (single character like x).string predefined class to store strings in quotes (ex "welcome to java") byte -> short -> int -> long -> float -> double = assignment compatibility order.

Write a Java statement to determine the length of a string variable called input. Store the result in an integer variable called strLength.

int strLength = input.length();

The hardest kind of error to detect in a computer program is a:

logic error.

'null' keyword (comparisons, assignments, etc.)

null is a special constant that may be assigned to a variable of any class type (ex: YourClass yourObject = null or != null). Null indicates the variable has no real value and null is an object placeholder. A method cannot be invoked using a variable that is initialized to null.

Concatentation

using + operator, two strings can be connected together Ex: "the answer is" + 42 would be: "The answer is 42"

How to terminate a program

using return. The return statement aborts execution of the current method.

Know the brackets: (), <>, {}, [] and what they are used for { }

{}Braces ("curly braces") Braces are used to group statements and declarations. The contents of a class or interface are enclosed in braces. Method bodies and constructor bodies are enclosed in braces. Braces are used to group the statements in an if statement, a loop, or other control structures [ ] Brackets ("square brackets"): Brackets are used to index into an array ( )Parentheses: Parentheses are used for two purposes: (1) to control the order of operations in an expression, and (2) to supply parameters to a constructor or method.

Variables, when do you delare a variable?

Variable is storage. var. declar. are used by giving var. type and name followed by semicolon: ex: int answer;

Which operator returns the remainder of integer division?

%

What is the Java expression for 27xy?

(27 x y)

What is the Java expression for 4a^2 + 2b * c?

(4 * a * a) + ((2 * b) * c)

Understand some math, for example: What is the Java expression for 4a2 + 2b * c?

(4 a a) + ((2 b) c).

In Java, a block comment is delimited by:

/** **/

To mark a block comment for inclusion in the Javadoc documentation, the block must be delimited by:

/** */

In an expression containing values of the types int and double, the ________ values are ________ to ________ values for use in the expression. Answers: 1 int, promoted, double 2 double, demoted, int 3 int, demoted, double 4 double, promoted, int

1

What is the result value of c at the end of the following code segment? int c = 8; c++; ++c; c %= 5; Answers: 1 0 2 1 3 3 4 None of the above

1

What is the value of the variable amountDue? double price = 2.50; double quantity = 5; double amountDue = 0; amountDue = price * quantity;

12.5

What does the expression x %= 10 do? Answers: 1 Adds 10 to the value of x, and stores the result in x. 2 Divides x by 10 and stores the remainder in x. 3 Divides x by 10 and stores the integer result in x. 4 None of these.

2

The value of the expression (int) 27.6 evaluates to:

27

What is the Java expression for 27xy?

27 ** x ** y

In an activity diagram, the merge symbol has the same shape as what other symbol? Answers: 1 Action symbol 2 Initial state 3 Transition arrows 4 Decision symbol

4

Keyword ________ indicates the inheritance relationship. Answers: 1 super 2 inherits 3 parent 4 extends

4

What is the size in bits of an int? Answers: 1 8 2 64 3 16 4 32

4

Which of the following is not a common name for one of the three phases that a program often can be split into using pseudocode? Answers: 1 Initialization phase 2 Termination phase 3 Processing phase 4 Action phase

4

Blocks

A block is another name for a compound statement, that is, a set of Java statements enclosed in braces,{}A variable declared within a block is local to that block, and cannot be used outside the block. Once a variable has been declared within a block, its name cannot be used for anything else within the same method definition

What is the difference between a class and an object?

A class is not an object, it is a blueprint for an object. A class is used to construct objects

Arrays (How to use, how to declare, how to pass, index order, exceptions...)

An array is a data structure used to process a collection of data that is all of the same type. An array behaves like a numbered list of variables with a uniform naming mechanism. It has a part that does not change: the name of the array. It has a part that can change: an integer in square brackets. For example, given five scores: score[0], score[1], score[2], score[3], score[4]. An can be indexed vars. Of any type, all indexed vars. in one array must be of similar type. An array is declared and created in almost the same way that objects are declared and created:BaseType[] ArrayName = new BaseType[size];

If-else

An if-else statement chooses between two alternative statements based on the value of a Boolean expression. Ex: if (Boolean_Expression) Yes_Statement else No_Statement Compare strings

When is java.util imported?

At the beginning of the code (top of code before class) imported to a class like scanner.

Where is the scanner class?

At the beginning of the file with the keyboard input code import.java.util.scanner.

What is byte-code? What is its importance?

Byte-code is the object code the Java compiler creates. Byte-code executes on the Java Virtual Machine (JVM). The JVM is platform independent, that is, not tied to one particular computer. The JVM is a fictitious computer, common to all computers; this feature makes Java unique in that the program can be created on one platform and executed on any other platform.

How do you create a comment line? a comment block?

Comment line begins with the symbols // and causes compiler to ignore remainder of line (this comment type used for code writer or modifier). Comment Block begins with the symbol pair /* and ends with symbol pair */ (compiler ignores anything in between and this comment can span several lines)

Operators Compiled Code is?

Compiler. The compiler is the program that translates source code into a language that a computer can understand. This process is different in Java than some other high-level languages. Java translates its source code into byte-code using the javac command.

A variable of type Boolean can be explicitly converted to that of type int.

False

Applets were designed to run as stand-alone applications.

False

In Java, Strings are immutable objects. Immutable objects can be changed.

False

Java does not require that a variable be declared before it is used within a program.

False

Java uses the ASCII character set.

False

Objects of type String are strings of characters that are written within single quotes.

False

The syntax that declares a Java named constant named SALES_TAX is:

public static final double SALES_TAX = 7.50;

How are line comments and block comments used?

A line comment is included as documentation to the programmer or a programmer that might need to modify the code at some later date. A line comment is signified by //. Anything that follows the // is considered a comment by the compiler until the end of the line is reached. A block comment is included as documentation to users of the class. Users of the class are usually other programmers. A block comment begins with /* and ends with */. A block comment my span many lines. Any text between the beginning /* and the ending */ is part of the block comment. A special block comment can be used with Javadoc, a documentation tool included in Java's SDK, to create documentation for users of the class.

Explain the difference between an implicit type cast and an explicit type cast.

A type cast takes a value of one type and produces a value of another type. Java supports two kinds of type casts: explicit and implicit. Java performs an implicit type cast automatically. This can be seen in the declaration of a variable of type double that is assigned an integer value. double castExample = 72; The integer value assigned to the variable castExample is automatically converted to a floating point number. The number assigned to castExample is converted to 72.0. An explicit type cast occurs when the programmer explicitly forces a value of one type into a value of another type. In the example that follows, the value 72.5 is explicitly cast into an integer value. int castExample = (int) 72.5;

Set methods/accessor methods

Accessor methods allow the programmer to obtain the value of an object's instance variables. The data can be accessed but not changed. The name of an accessor method typically starts with the word get. Mutator methods allow the programmer to change the value of an object's instance variables in a controlled manner. Incoming data is typically tested and/or filteredThe name of a mutator method typically starts with the word set

The 'this' keyword

All instance variables are understood to have <the calling object>. in front of them. If an explicit name for the calling object is needed, the keyword this can be used. myInstanceVariable always means and is always interchangeable with this.myInstanceVariable. this must be used if a parameter or other local var. with same name is used in method.

Call-by-value vs pass-by-reference

All parameters in Java are call-by-value parameters. A parameter is a local variable that is set equal to the value of its argument. Therefore, any change to the value of the parameter cannot change the value of its argument. Class type parameters appear to behave differently from primitive type parameters. They appear to behave in a way similar to parameters in languages that have the call-by-reference parameter passing mechanism

What is an algorithm?

An algorithm is a set of precise instructions that lead to a solution. An algorithm is normally written in pseudocode, which is a mixture of programming language and a human language, like English

There are two kinds of Java programs, applications and applets. Define and discuss each.

An application is just a regular program that runs on your computer. An applet is a little Java program that runs in a Web browser. Applets and applications are almost identical. The difference is that applications are meant to be run on your computer like any other program, whereas an applet is meant to be run from a Web browser. An applet can be sent to another location on the Internet and run there.


Kaugnay na mga set ng pag-aaral

Fraud 4235 Midterm, Fraud: Chapter 10

View Set

Federal Government | Chapter 5: Equal Rights

View Set

Business Law - Chapter 20: Banking

View Set

Adult Health Exam #5 (CH Review Qs, W9 Quiz, Misc. Quizlets)

View Set

Test 3: Mobility, Tissue Integrity, and Intracranial Regulation

View Set