Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction to Java Programming. It serves as a review outline. All questions in quizzes and exams will be based on concepts listed in this document. Most of the skills that you need for writing Java programs are also listed in this document. Make sure that you can answer all the questions or understand the listed concepts for each section that we have covered. Copyright Dr. Lixin Tao, Pace University, ltao@pace.edu. This document should not be copied partially or in full, or published in any form, without the permission of the author.
1 Computer Systems and Programming Languages 1.1 Problem vs. problem instances 1. What are the differences between a problem and a problem instance for the problem? 2. Is a computer program supposed to solve a problem or a problem instance? 3. What is the function of a computer program in problem-solving? 4. What do we mean when we say that a program is correct? 5. What do we mean when we say that a program is robust? 1.2 Structure of a personal computer 1. What is a machine language program? 2. What are the relationship among computer hardware, software, and data? 3. What are the functions of CPU, main memory, hard disk, keyboard, and display? 4. Can data in CPU and main memory survive power failure? 5. Can data in hard disk survive power failure? 6. How many devices can send data through the system bus simultaneously? 7. How many devices can receive data through the system bus simultaneously? 8. Why we need the main memory, instead of letting CPU run programs on the hard disk directly? 1.3 CPU 1. Which computer device defines and implements the machine language of a computer architecture? 2. What is the function of the program counter? 3. What is the function of general-purpose registers in a CPU? 4. Which are the major phases for the CPU to execute a machine instruction? 5. How are activities of a CPU synchronized? 1.4 Memory 1. For a computer, what is a bit, a byte, or a word? 2. How is the main memory organized? 3. What is memory address? 4. What is the smallest memory address? 5. What is memory value corresponding to a memory address? 6. When a value is written into a memory cell, what happens to the original value in that cell? 7. When a value is read out of a memory cell, is the current value in the memory cell modified? 8. The main memory is used to store programs or data? 9. Even though memory hardware can only store binary bits, how can a memory store different types of data like integers, floating-point numbers, strings, and programs? 1
1.5 Disk file system 1. What is the basic unit for storing programs or data on a hard disk? 2. What components a file name may contain? 3. What is the function of a file name extension? 4. What is a file system directory? 5. Can a directory contain another directory? 6. Should we use short and encrypt file names, or concise file names that convey the purpose of the files? 7. What is a file system directory tree? 8. What is the working directory for a Command Prompt window? 9. What is the absolute path of a file? 10. What is the relative path of a file? 11. Which directory does period (.) represent? 12. Which directory do double periods (..) represent? 1.6 Display 1. What is the main function of a computer display? 2. What is a pixel? 3. What is the resolution of a display? 4. What are the text mode and graphics mode of a display? 5. In the text mode of a display, what is the function of a display cursor? 6. Which key strike will cause the display cursor to terminate the current line and move to the beginning of the following line? 7. In the graphics mode of a display, what kind of coordinate system is used to define the position of a pixel? 1.7 Keyboard 1. What is the function of a keyboard? 2. What is an active window? 3. Which keys can generate white space characters? 1.8 Computer Software 1. What is computer software? 2. What are the two major types of software? Give examples. 3. Can you list the names of three high-level programming languages? 1.9 Java programming language 1. What is a Java keyword or reserved word? 2. Why we need high-level programming languages? 3. What is source code? 4. What is executable code? 5. What is a bytecode file? 6. What is a class file? 7. What is an interpreted programming language? 2
8. What is a compiled programming language? 9. How is Java program source code translated into machine instructions to be executed by a CPU? 10. What is Java virtual machine? 11. What is the name of the command for compiling Java source files into bytecode files? 12. What is the name of the command for executing the Java bytecode files? 13. Is a Java bytecode file hardware or operating system dependent? 14. Java was launched by which company and in which year? 15. What is the syntax structure of an identifier? 16. What is good practice for identifiers so Java program maintenance can be easier? 17. What are Java reserved words or keywords? 18. Does a Java class s name capitalize its first letter? 19. Does a Java method s name capitalize its first letter? 20. Does a Java variable s name capitalize its first letter? 2 Software Installation 1. What is J2SE (Java 2 Standard Edition) for? 2. What is J2SE SDK (Software Development Kit), also called JDK, for? 3. What is J2SE JRE (Java Runtime Environment) for? 4. What is the current version of J2SE? 5. What is the base URL for downloading Sun J2SE SDK? 6. Does the installation of J2SE SDK also install J2SE JRE as part of it? 7. Where are commands javac.exe and java.exe located in a J2SE SDK installation? 8. What is the composition and function of environment variable PATH? 9. What is the composition and function of environment variable CLASSPATH? 10. How to temporarily change the value of CLASSPATH in a Command Prompt window? 11. If we don t set any value for CLASSPATH, what is CLASSPATH s default value? 12. How to start a Command Prompt window? 13. Inside a Command Prompt window, how to change the working directory, how to compile and run a Java program? 3 Java Basics 3.1 Basic Hello World program 1. How to write the basic Hello World program (as simple as possible, but it can compile and run)? 2. Are public, class, static, and void Java s reserved words? 3. Are Java programs case-sensitive? 4. What file name should be used for holding the declaration of a public class named Welcome? 5. Can a Java source code file contain more than one public class? 6. To print a message to the display and then move the display cursor to the beginning of the following line, use System.out.println("MessageToBeDisplayed"); 7. Each statement must terminate with semicolon (;). 3
8. A Java program contains one or more classes, and a class contains one or more methods. 9. The body of a class or a method is enclosed in a pair of curly braces. 10. Java character string constants (literals) are enclosed in straight double quotes, as "Hello world". 11. When a Java program is run, its method main() is the first method to be called and executed. Method main() must read as public static void main(string[] args) { }. 3.2 Code style and command-line arguments 1. On any source code line, // signifies the beginning of a Java comment, and the comment ends at the end of the line. A comment will be replaced by the compiler with a white space character. 2. It is important to add proper comments inside a source file so people can understand the program s logic. Comment is an important form of software documentation. 3. Space, Tab, and Enter keys generate white space characters, and in a Java source code file, a single white space is equivalent to any number of white space characters. 4. How source code should be indented for supporting a good source code style? 5. What are the two main styles of writing Java source code as to the position of the opening curly braces? 6. Should we use a mixture of the two source code styles in the same project? 7. What are command-line arguments of a Java program? 8. How to access the command-line arguments inside method main()? 9. How to print a message and then leave the display cursor to the right of the last character printed? 10. How can operator + concatenate a few strings into a single one? 3.3 Java package 1. What are Java packages for? 2. Can a Java package contain another package? 3. How to declare that a class belongs to a particular package? 4. Where must the package declaration be in a source code file? 5. Does a package declaration need a semicolon to terminate it? 6. How is Java package implemented in a file system? 7. If a class belongs to package cs121, in which directory that the bytecode file for the class must be kept? 8. What is the most convenient way to compile a Java class into a specified class base directory and generate the necessary directories for the corresponding packages on the package path for the class automatically? 9. How to run a class that belongs to a package? 3.4 Best practice for organizing multiple class projects 1. What is the best practice for organizing multiple class projects? 2. Where should we save the source files for classes belonging to some Java packages? 3. What is the advantage of separating Java source code from the bytecode files? 4. How should we set up CLASSPATH value so that we can run the projects from any working directory? 4
5. In which working directory should we compile a project? 6. Do we need to compile explicitly each Java class source code file in a multiple-class project? 7. How can we compile a project so that the bytecode files are stored in the same directories as their corresponding Java source files? 8. Can the class-level variables of a class be accessed by all methods of the class? 9. If a class-level variable has no value initialization during declaration, what will be its default value? 10. If a variable has value null, what is the meaning? 11. How can we access a public and static variable of a class? 12. What is the meaning for a method to invoke another method? 3.5 Local variables, expressions, and assignment 1. Where can local variables be declared? 2. The local variables will be accessible in which scope? 3. Why a variable needs to be declared to have a fixed data type? 4. What do people mean when they say that Java is a strongly-typed language? 5. Will the value stored in a variable be modified if we read the variable s value out? 6. Will the value stored in a variable be modified if we store a new value into the variable? 7. If a local variable is declared with no initial value, is the variable going to have a default initial value? 8. How to declare local variables with initial values? 9. How to declare multiple local variables of the same type in the same Java declaration? 10. Can we declare the type of a variable more than once in the same method? 11. Can we read the value of a variable before it is declared? 12. Can we read the value of a variable before it is initialized? 13. What is the difference between the = operator in a Java assignment from the mathematical relational operator =? 14. How is an assignment statement executed? 15. Can the left side of operator = in an assignment statement be anything other than a variable? 16. What is the meaning for a variable to appear to the right side of operator = in an assignment statement? 17. Assume x = 1. What is the value of variable x after the execution of expression ++x? 18. Assume x = 1. What is the value of expression ++x? 19. Assume x = 1. What is the value of variable x after the execution of expression x++? 20. Assume x = 1. What is the value of expression x++? 21. What is the result of applying operator + to a string value and an integer value, like "CS" + 121? 22. What kind of expressions has side effects? 3.6 Basic built-in data types and type casting 1. List four popular Java built-in data types. 2. Do Java data types have fixed lengths on all hardware and operating system platforms? 3. How many bits are used to store a Java int value? 4. How many bits are used to store a Java double value? 5. How many bits are used to store a Java char value? 5
6. What is the data type for literal 12? 7. What is the data type for literal 12.0? 8. What is the value of 7/2? 9. What is the value of 7.0/2? 10. What is 5 % 2? 11. What is type promotion? 12. How can we change the operator evaluation order in an arithmetic expression? 13. Literal values of type char must be enclosed in what kind of delimiters? 14. What is implicit data type casting? 15. Can we assign a double value into an int type variable? 16. What is explicit data type casting? 17. When do we need to use explicit data type casting? 18. What are the valid values of a boolean type variable? 19. Is a relational expression also a boolean expression? 20. What is the value of (1 >= 1)? 21. What are the three most important Boolean operators? 22. When will the conjunction of two Boolean expressions be true? 23. What is the value of ((i < 5) && (i >= 5))? 24. When will the disjunction of two Boolean expressions be false? 25. What is the value of ((i < 5) (i >= 5))? 26. How to convert a string-form int value into an int value? 27. How to convert a string-form double value into a double value? 28. How to convert a string-form boolean value into a boolean value? 29. How to retrieve the second character of a string? 30. How to convert a lower-case letter into its upper-case counterpart? 31. What are the values of the following method invocations against class Math? a. abs(-2) b. ceil(1.5) c. floor(1.5) d. exp(0.0) e. log(1) f. max(1, 2) g. min(1.0, 2.0) h. pow(2.0, 3.0) i. sqrt(4.0) j. random() 3.7 Command-line arguments and loops 1. How to specify a command-line value that contains space characters? 2. Assume the method main() has parameter args. Which command-line argument will be stored in args[0]? 3. If there is no command-line arguments used for a run, what is the value of expression args.length? 4. Which Java library class can be used to sort an array of numbers or strings? 5. What is a compound statement? 6. Can a compound statement contain local variable declarations? Where? 7. Is the body of a method a compound statement? 6
8. Is the body of a class a compound statement? 9. Can we replace any statement in a program with a compound statement without violating the syntax specification for Java? 10. If we declare a local variable inside a compound statement, can we access that variable outside of the compound statement? 11. When will a while loop terminate? 12. Can the body of a while loop be a single statement? 13. Enumerate the steps for a while loop to be executed. 14. What is a loop iteration? 15. What is a loop variable? 16. Where should the loop variable for a while loop be declared and initialized? 17. What will happen if we don t modify the value of the loop variable for a while loop during its iteration? 18. What is the control block of a for loop? 19. What is the function of each of the three components of the control block of a for loop? 20. Which character should be used to separate the three components of the control block of a for loop? 21. If we declare the loop variable in a for loop s control block, can we access this loop variable outside the loop body? 22. Enumerate the steps for a for loop to be executed. 23. Normally where do we declare a loop variable and initialize it for a for loop? 24. If a program runs into an infinite loop for some special input data, can the program still be a correct program? 25. What is lexicographic order for character strings? 3.8 Calculator with if-else statements 1. How to find the length of a one-dimensional array? 2. What is \n for in a string literal? 3. What is \t for in a string literal? 4. How is an if statement executed? 5. Is the pair of parentheses for the Boolean expression of an if statement optional? 6. How to chain a list of several if statements together? 7. What is System.exit(int); for? 8. What is the meaning of values for variable x in System.exit(x);? 9. When must we use System.exit(int);? 10. When can we use * as a command-line argument? 3.9 Calculator with switch statement 1. Variables of which data types can be used for the switch expression of a switch statement? 2. Can we omit the pair of parentheses around the switch expression? 3. Do we need a semicolon at the end of a compound statement? 4. Can the case values of the case labels be of different data types from that of the switch expression? 5. Can multiple case labels have the same case value? 7
6. Is the default case mandatory? 7. Is the order of the cases important? 8. Can a statement have multiple case labels in a switch statement? 9. What is the meaning of a break statement in a switch statement? 10. How is a switch statement executed? 11. What will happen if the statements for a switch case is not terminated with a break statement or a System.exit(int); statement? 3.10 Calculator with exception processing 1. What is a Java exception? 2. Give two examples that Java code will throw exception objects? 3. If a method receives an exception object not caught by a try-catch block, what will happen? 4. What is a try-catch block for? 5. Is the try clause of a try-catch block optional? 6. Are the catch clauses of a try-catch block optional? 7. Is the finally clause of a try-catch block optional? 8. What statements should be put in the try clause? 9. What is the parameter of a catch clause for? 10. When will a catch clause be executed? 11. When will a finally clause be executed? 12. What kind of exceptions will be caught by catch (Exception e) { }? 13. How does a try-catch block work? 14. What will happen if an exception is thrown to method main() and it is not caught by a catch clause? 15. What is the difference between Java comments delimited by // and those delimited by /* and */? 16. What will happen if // form of comments are embedded inside a pair of /* and */? 3.11 Conditionally interrupting loops 1. If a loop executes statement break, what will happen? 2. If a loop executes statement continue, what will happen? 3.12 Arrays 1. How to declare 1000 int-type variables on one Java source code line? 2. How to declare that variable x is of type array of ints? 3. How many bits will be used to implement an array variable? 4. Does the declaration of an array variable also allocate space for the array? 5. How to allocate an array for 10 integers and connect this array with an int array variable x? 6. If an array has length 10, what are its valid index values? 7. What is the easiest way to declare y to be an int array variable and connect it with an int array of length 1 containing value 1? 8
8. How to declare a 2-D int array variable with name z? 9. How to allocate space for a 4 x 4 square int array and connect it with variable z? 10. To access the cell at row 2 and column 3 of array z, should we use expression z[2][3], or z[3][2], or z[2, 3]? 11. How to declare that z3 is a 3-D int array variable? 12. How to allocate space for a 4 x 5 x 6 int array and connect it with a 3-D int array variable z3? 13. To access the cell on plane 1, row 2 and column 3, should we use expression z3[1][2][3], or z3[3][2][1], or z3[1, 2, 3]? 3.13 Method declaration and invocation 1. What are Java methods for? 2. What is a method s signature? 3. Are parameter names parts of a method s signature? 4. What is method return type? 5. What do we mean if we say that a method has void as its return type? 6. What is method parameter list? 7. What is a method declaration? 8. What is a method invocation? 9. What is a method s body? 10. If a method has no parameters, do we still need the pair of parentheses for the parameter list? 11. If a method has two parameters x and y both of type int, can we declare them as in (int x, y)? 12. Can we declare local variables inside a method declaration? Where? 13. Can we declare the type of a variable more than once in the same method? 14. What are the differences between method parameters and method arguments? 15. How are the values of method arguments passed to method parameters? 16. Can a method invocation contain different number of arguments that the number parameters in that method s declaration? 17. How is a method invocation executed? 18. Are parameters of a method part of the local variables of the method? 19. Where can the method parameters be accessed? 20. If a method contains only one statement, can we omit the enclosing curly braces for the method body? 21. Should we capitalize the initial letter of a method s name? 22. What character is used to separate the successive parameters declarations in a method s parameter list? 23. How can we change the name of a parameter without changing the containing method s signature or function? 24. If a method s body executes statement return;, what will happen? 25. If a method s body executes statement return 2*2;, what will happen? 26. If method a() calls method b(), which in turn calls method c(), what is the order for the three methods to complete their executions? 27. Is the order of method declarations in a class important? 28. Can a static method s body invoke a non-static method? 29. What is the value of expression (1 > 2? 1 : 2)? 9
3.14 Java documentation comments 1. How to write a Java doc comment? 2. What are Java doc comments for? 3. Where should Java doc comments be located? 4. What is the general structure of a Java doc comment? 5. Which tag should be used to specify a parameter in a Java doc comment? 6. Which tag should be used to specify the method return value in a Java doc comment? 7. How to generate Java documentation HTML files for all the Java source code files in the current working directory? 8. Which generated file is the root file for the resulting HTML documentation? 9. How to let the Java documentation list both public and private methods? 3.15 Scopes of local variables 1. Where can the method parameters be accessed? 2. What is the scope of a local variable? 3. Can a local variable declared in a compound statement be accessed from a nested compound statement after this declaration? 4. If a loop variable is declared in the control block of a for statement, can we access that variable outside of the for loop? 3.16 Formatting output 1. What is the purpose of method System.out.printf()? 2. What is the first parameter of System.out.printf() for? 3. What does %4.2f stand for as part of the first argument to System.out.printf()? 4. What does %6d stand for as part of the first argument to System.out.printf()? 5. If the first argument of System.out.printf() contains 3 substrings of form similar to %4.2f or %6d, how many total arguments must be used in this method invocation? 6. What is the major difference between a variable of type int and a variable of type Random? 7. How can we generate random integers between 0 and 9 inclusive? 8. How can we generate random double values between 0.0 (inclusive) and 10.0 (exclusive)? 9. What is the meaning of importing a class? 10. How to declare that a class needs to import a particular class from a particular package? 3.17 Timing the evaluation of π 1. Can Java double typed variables store all accurate floating-point numbers? 10
2. What may happen if a loop is controlled by a Boolean expression that checks whether a double variable has a particular value? 3. What value will be returned by an invocation to method System.currentTimeMillis()? 4. How many bits are used to store a Java long typed variable? 3.18 Matrix multiplication 1. What is the function of method setseed(int) of class Random? 2. How to check the number columns that a two-dimensional array a[][] has? 3. How to avoid repeating similar code in a program? 3.19 Interactive command-line data input 1. How to use class java.util.scanner to get the next integer from a Command Prompt window? 2. How to use class java.util.scanner to get the next double value from a Command Prompt window? 3. How to use class java.util.scanner to get the next line from a Command Prompt window? 4. What should we do before we wait for some user input from a Command Prompt window? 3.20 Window-based data input/output 1. How to pop up a window and request the user to input a string? 2. How to pop up a window to print out some message? 3. When should we use System.exit(int) to terminate a program s execution? 3.21 Text file input/output 1. How to open a text file for reading its contents line by line? 2. How to open a text file for writing text data into it with methods print(), println(), or printf()? 3. How to check whether a file is in existence already? 4. How to close a file? 5. Why do we need to close a file? 6. When should we use a clause like throws IOException for a method? 7. How to remove the leading and trailing white space characters in a string? 8. How to parse a string into white-space-separated tokens (strings that don t contain white space characters)? 9. How to import all classes in a package? 10. What type of exception that the file manipulation methods may throw? 3.22 Recursive vs. iterative methods: factorial 11
1. What kind of methods are iterative methods? 2. What kind of methods are recursive methods? 3. What is an activation record for a method s invocation? 4. Can we have multiple unfinished invocations to the same method? 5. Are the local variables of a method shared by multiple unfinished invocations to the method? 6. What will happen if a method executes statement throw new Exception( reason for this exception )? 7. When should we use recursive solutions? 8. When should we use iterative solutions? 3.23 Recursive vs. iterative methods: Fibonacci numbers 1. Why the recursive solution for Fibonacci numbers is much slower than that based on a loop? 2. Which version of the solution is easier to write and read? 3.24 Algorithm and Towers of Hanoi 1. What is an algorithm? 2. Does an algorithm have to stop for any valid input data? 3. What is the time complexity of an algorithm? 4. What is the space complexity of an algorithm? 5. What is the Towers of Hanoi problem? 3.25 Bubble sort 1. What do we mean for sorting an array of integers? 2. What is the main idea of bubble sort? 3. What is the function of the outside loop of the bubble sort method? 4. What is the function of the inner loop of the bubble sort method? 5. The running time of the bubble sort method will be proportional to what expression of the input size? 3.26 Binary search 1. What is a data record? 2. What is the key field of a data record? 3. Can different data records have the same value for their key field? 4. What is the data search problem? 5. How does sequential search work? 6. What are the best-case and worst-case time complexities of sequential search? 7. How does binary search work? 8. What are the best-case and worst-case time complexities of binary search? 12