Made with ScholarlyMake flashcards like these from your own notes
Question:What is the difference between procedural programming and object-oriented programming (OOP)?
Answer:Procedural programming executes instructions in a step-by-step literal order, guiding the application through sequences of commands, while OOP views program components as objects similar to real-world entities, focusing on creating and using these objects, which better suits complex and multifaceted problems.
Question:List and briefly explain the five main features of Object-Oriented Programming (OOP).
Answer:1. Encapsulation: Capturing data and protecting it from outside access. 2. Inheritance: Creating a new class from a base class, inheriting features and adding new ones, enhancing code reusability. 3. Polymorphism: The ability of objects to exist in various forms. 4. Abstraction: Representing data conceptually without details, hiding information from other classes. 5. Message Passing: Objects communicate by invoking methods and sending data through function parameters.
Question:Why is Object-Oriented Programming considered better suited for most tasks compared to procedural programming?
Answer:Because most problems are complex and multifaceted and do not conform easily to a linear approach, OOP's conceptualization of program components as interacting objects provides a more flexible and natural modeling of such problems.
Question:What was the original purpose behind the development of the Java programming language in 1991?
Answer:Java was designed by James Gosling and his team at Sun Microsystems to programming home appliances controlled by various computer processors, requiring a language accessible across different hardware.
Question:How did Java's connection to the internet begin and which company was involved?
Answer:In 1994, James Gosling realized Java's suitability for web browsers, and in 1995 Netscape Incorporated released a browser version capable of running Java programs, marking Java's strong connection to the internet.
Question:What was the original name of the Java programming language and why was it changed?
Answer:The original name was Oak, but it was changed because a programming language already existed with that name. After struggling to find a new name, the development team went out for coffee and the name Java was born.
Question:Is Java only a programming language for Internet applications?
Answer:No, Java is a general all-purpose language that can be used independently of the Internet, despite being viewed as a language for designing Internet applications.
Question:What does Java stand for?
Answer:Java does not stand for "Just Another Vague Acronym." It is both a programming language and a platform.
Question:Who developed the Java programming language and what type of language is it?
Answer:Java was developed by SUN Microsystems (Stanford University Network) as an object-oriented language used for general-purpose business programs and interactive internet programs.
Question:What are some advantages of the Java programming language that have contributed to its popularity?
Answer:Its security features and architectural neutrality, meaning Java programs can run on any platform.
Question:List the characteristics that describe Java as a high-level programming language.
Answer:Java is simple, architecture-neutral, object-oriented, portable, distributed, high-performance, interpreted, multithreaded, robust, dynamic, and secure.
Question:How does the compilation and interpretation process work in Java?
Answer:Java programs are first compiled into an intermediate language called Java bytecodes. These bytecodes are platform-independent and are interpreted by the Java interpreter each time the program runs. Compilation happens once, interpretation happens every execution.
Question:What is the role of Java bytecodes?
Answer:Java bytecodes are the machine code instructions for the Java Virtual Machine (Java VM) and enable the 'write once, run anywhere' capability by being platform-independent.
Question:What is the Java Virtual Machine (Java VM)?
Answer:The Java VM is an implementation (either a software Java interpreter or hardware) that runs Java bytecodes. Every Java interpreter, including browsers running applets, is an implementation of the Java VM.
Question:How does the Java platform differ from most other platforms?
Answer:The Java platform is a software-only platform that runs on top of other hardware-based platforms, unlike most platforms that combine hardware and operating systems.
Question:What are the two main components of the Java platform?
Answer:The two main components of the Java platform are the Java Virtual Machine (Java VM) and the Java Application Programming Interface (Java API).
Question:What is the role of the Java Virtual Machine (Java VM)?
Answer:The Java VM serves as the base for the Java platform and is ported onto various hardware-based platforms, enabling Java programs to run independently of underlying hardware.
Question:What does the Java API provide?
Answer:The Java API is a large collection of ready-made software components grouped into libraries (packages) that provide many useful capabilities, such as graphical user interface (GUI) widgets.
Question:How does the Java platform ensure program portability across different hardware?
Answer:The Java API and Java Virtual Machine isolate Java programs from hardware dependencies, creating a platform-independent environment.
Question:Why might Java programs run slower than native code, and how is this performance gap addressed?
Answer:Java programs can be a bit slower than native code due to the platform-independent layer, but smart compilers, well-tuned interpreters, and just-in-time bytecode compilers help bring Java's performance close to native code without sacrificing portability.
Question:What is a Java applet?
Answer:A Java applet is a Java program that follows certain conventions allowing it to run within a Java-enabled browser.
Question:What is the difference between a Java applet and a Java application?
Answer:A Java applet runs within a Java-enabled browser following specific conventions, while a Java application is a standalone program that runs directly on the Java platform.
Question:In the provided HelloWorld example, what is the purpose of the 'public class HelloWorld' statement?
Answer:It defines a class named HelloWorld, with 'public' as an access modifier specifying that the class can be accessed liberally.
Question:What does the 'public' access modifier signify in Java?
Answer:It indicates the most liberal type of access, meaning the class or member can be accessed from any other class.
Question:What are the three types of comments in Java, and what are their uses?
Answer:1) Documentation comments: used at the beginning of the program; 2) Block comments: used to comment a block or multiple lines; 3) Line comments: used to comment a single line of the program.
Question:What does the keyword 'static' mean in the context of the main method in Java?
Answer:Static means unchanging or stationary, indicating that every member created for the class (like the main method) belongs to the class itself rather than to any instance of the class.
Question:What is the role of the argument 'String args' in the main method?
Answer:'String args' is an argument that holds any Strings sent to the main method. It provides information the method requires to perform its task.
Question:Why is the identifier 'args' used in the main method's parentheses, and can it be named differently?
Answer:By convention, 'args' is used as the identifier, but it can be any legal Java identifier. You must place an identifier within the main method's parentheses even if it is not used.
Question:What is the purpose of the statement 'System.out.println("Hello World!")' in Java?
Answer:This statement displays 'Hello World!' on the screen. 'System' is a class, 'out' is an object representing the screen, and 'println' is a method that prints a line of output and moves the cursor to the next line.
Question:How are classes, objects, and methods represented in the statement 'System.out.println'?
Answer:The dots separate the class (System), the object (out), and the method (println). This shows the hierarchical relationship between them.
Question:What are the naming rules and conventions highlighted for Java programming?
Answer:Java is case-sensitive, so proper casing must be maintained. Also, the file name must be the same as the class name with a '.java' extension (e.g., HelloWorld.java). Statements must terminate properly (typically with a semicolon).
Question:What are variables in programming, and what are their two main components?
Answer:Variables are the entities that hold values or data that act or are acted upon. A variable declaration consists of the type of the variable and its name.
Question:How does the location of a variable declaration affect its scope?
Answer:The scope of a variable is determined by where its declaration appears in relation to other code elements.
Question:What determines the values a variable can hold and the operations that can be performed on it in Java?
Answer:A variable's data type determines both the values it can contain and the operations that can be performed on it.
Question:Give an example of a variable declaration and explain its components.
Answer:For example, 'int count' declares 'count' as an integer variable capable of holding integral values. 'int' is the data type, and 'count' is the variable name.
Question:What are primitive data types in Java and how are their sizes and ranges described?
Answer:Primitive data types include integer types like byte (8-bit, -128 to 127), short (16-bit, -32,768 to 32,767), int (32-bit, -2,147,483,648 to 2,147,483,647), and long (64-bit). Real numbers include float (32-bit, single precision) and double (64-bit). Sizes relate to the number of bits, and ranges describe the span of values each type can hold.
Question:What is the purpose of Unicode in Java variable names?
Answer:Unicode is a character-coding system designed to support text written in diverse human languages. It allows Java variable names to include characters from various alphabets, such as Japanese, Greek, Cyrillic, and Hebrew, enabling programmers to write code meaningful in their native languages.
Question:What are the three conditions that a variable name must satisfy in Java?
Answer:1. It must be a legal Java identifier comprised of a series of Unicode characters. 2. It must not be a keyword or a boolean literal (true or false). 3. It must not have the same name as another variable declared in the same scope.
Question:What is the conventional style for naming variables and classes in Java?
Answer:By convention, variable names begin with a lowercase letter, and class names begin with an uppercase letter. If a variable name consists of more than one word, the words are joined together, and each word after the first starts with an uppercase letter (e.g., isVisible).
Question:How do you initialize a variable in Java and what must match in the assignment?
Answer:A variable can be initialized with an assignment statement when it's declared, for example, int count = 0;. The data type on both sides of the assignment must match, meaning the value assigned must correspond to the variable's declared type.
Question:What is a final variable in Java and what restriction applies after initialization?
Answer:A final variable is declared with the keyword 'final' and cannot change its value after it has been initialized. Any subsequent attempts to assign a value to a final variable cause a compiler error.
Question:What is a 'blank final' variable in Java?
Answer:A blank final is a final variable that has been declared but not yet initialized. It must be initialized later, and once initialized, it cannot be assigned a new value. Attempting to assign a value afterward results in a compile-time error.
Question:List the basic arithmetic operators supported by Java for floating-point and integer numbers and their meanings.
Answer:Addition (+): adds two operands. Subtraction (-): subtracts the second operand from the first. Multiplication (*): multiplies two operands. Division (/): divides the first operand by the second. Modulo (%): computes the remainder of division of the first operand by the second.
Question:What does the operator * do when used between op1 and op2?
Answer:It multiplies op1 by op2.
Question:How does the / operator work with op1 and op2?
Answer:It divides op1 by op2.
Question:What is the purpose of the % operator between op1 and op2?
Answer:It computes the remainder of dividing op1 by op2.
Question:What does the unary + operator do when used on op?
Answer:It promotes op to int if it's a byte, short, or char.
Question:What is the function of the unary - operator on op?
Answer:It arithmetically negates op.
Question:How do the increment operators ++ and -- work as shortcuts?
Answer:++ increments the operand by 1, and -- decrements the operand by 1.
Question:What is the difference between prefix and postfix usage of ++ and --?
Answer:Postfix (op++ or op--) evaluates to the value before incrementing/decrementing, while prefix (++op or --op) evaluates to the value after incrementing/decrementing.
Question:What is a relational operator used for?
Answer:A relational operator compares two values and determines the relationship between them.
Question:What does the != operator return when comparing op1 and op2?
Answer:It returns true if op1 and op2 are not equal.
Question:Provide examples of relational operators and what they check.
Answer:Examples include > (op1 is greater than op2), >= (op1 is greater than or equal to op2), < (op1 is less than op2), <= (op1 is less than or equal to op2), == (op1 and op2 are equal), and != (op1 and op2 are not equal).
Question:How are relational operators often combined for decision-making?
Answer:They are often used with conditional operators to construct more complex decision-making expressions.
Question:What does the && operator do?
Answer:It returns true if both op1 and op2 are true and conditionally evaluates op2.
Question:What does the || operator do?
Answer:It returns true if either op1 or op2 is true and conditionally evaluates op2.
Question:Explain the difference between conditional operators &&, || and their logical counterparts &, |.
Answer:&& and || conditionally evaluate the second operand, stopping early if the result is determined, while & and | always evaluate both operands.
Question:What does the ! operator do when used on op?
Answer:It returns true if op is false; it performs boolean NOT.
Question:Describe the ternary operator ? and its use.
Answer:The ternary operator evaluates an expression and returns op1 if it's true and op2 if it's false; it's shorthand for an if-else statement.
Question:In the example 'x > y ? z = x : z = y;', what happens if x > y is false?
Answer:z is assigned the value of y.
Question:What is the purpose of the assignment operator '='?
Answer:It assigns one value to another.
Question:How can you shorten the code 'i = i + 2' using shortcut assignment operators?
Answer:By writing 'i += 2'.
Question:Give an example of a shortcut assignment operator and its equivalent long form.
Answer:The operator 'op1 += op2' is equivalent to 'op1 = op1 + op2'.
Question:What is the role of expressions in a Java program?
Answer:Expressions perform the work of a Java program by computing and assigning values to variables and helping control the execution flow. They perform computations indicated by their elements and return a value as the result.
Question:How is an expression defined in Java?
Answer:An expression is a series of variables, operators, and method calls constructed according to the syntax of the language that evaluates to a single value.
Question:What Java classes can be used to put data into variables from the keyboard?
Answer:BufferedReader and Scanner are predefined Java classes used to read input from the standard input device (keyboard).
Question:How does BufferedReader work to read input in Java?
Answer:BufferedReader reads text from an input stream by buffering characters, arrays, or lines. It creates an object to read input from the standard input device using the statement: BufferedReader buffread = new BufferedReader(new InputStreamReader(System.in)).
Question:How do you read an integer, a floating-point number, and a string using BufferedReader?
Answer:For integer: Integer.parseInt(buffread.readLine()) For floating-point: Double.parseDouble(buffread.readLine()) For strings: buffread.readLine()
Question:What must you do when using BufferedReader in your Java program?
Answer:You must import java.io package before the class declaration and include 'throws IOException' in the main method declaration.
Question:What is a Scanner in Java and how is it used for input?
Answer:Scanner is a predefined Java class used to get user input of primitive types. You create an object with: static Scanner console = new Scanner(System.in) and use its methods like console.nextInt(), console.nextDouble(), console.next(), or console.nextLine() for input.
Question:How do you read different types of input using Scanner?
Answer:For integer input: console.nextInt() For floating-point input: console.nextDouble() For string input: console.next() For string input including spaces (whole line): console.nextLine()
Question:What must be declared before the class declaration when using Scanner for input in Java?
Answer:The java utilities package (import java.util) must be declared before the class declaration.
Question:How does the parsing method in Java work and what is its purpose?
Answer:Parsing in Java is a static method that can have one or two arguments and reads the value of one object to convert it to another type, for example, converting strings to integers or doubles.
Question:What is required when using parsing methods like Integer.parseInt and Double.parseDouble in a Java program?
Answer:You must input the values when executing the program, for example: InputData3 juan cruz 20 110.00.
Question:What Java package provides prepackaged dialog boxes for displaying messages to users?
Answer:The javax.swing package provides the JOptionPane class that enables programs to display dialog boxes.
Question:What does the first argument 'null' in JOptionPane.showMessageDialog(null, "message") signify?
Answer:When the first argument is null, the dialog box appears at the center of the screen.
Question:What kind of button does a JOptionPane message dialog box automatically contain, and what is its function?
Answer:It contains an OK button that allows the user to dismiss (hide) the dialog box by clicking it.
Question:What does the method call System.exit(0) do in a Java program?
Answer:It terminates the application, where the argument 0 indicates that the application has ended normally.
Question:How are Java's packages categorized and what do their names indicate?
Answer:Java's packages are grouped into core packages and extension packages. Core packages start with 'java' and extension packages start with 'javax'.
Question:Why are GUI components important in Java programs and what package often contains them?
Answer:GUI components facilitate data entry by the user and formatting of data outputs. The javax.swing package contains many classes for defining GUIs.
Question:What does a non-zero value usually indicate when a program terminates?
Answer:A non-zero value normally indicates that an error has occurred.
Question:What is the purpose of the JOptionPane.showInputDialog method in the provided program?
Answer:It displays an input dialog box that allows the user to enter a string value to be used in the program.
Question:In the example program, what type of data does the variable 'name' hold?
Answer:The variable 'name' holds a string value entered by the user in the input dialog box.
Question:What are the four arguments used in the JOptionPane.showMessageDialog method in the example, and what does each represent?
Answer:1. 'null' indicates the dialog appears at the center of the screen. 2. 'Hello name' is the message displayed to the user. 3. 'Welcome' is the title that appears in the dialog box's title bar. 4. JOptionPane.PLAIN_MESSAGE specifies the type of message dialog to display with no icon.
Question:How does the PLAIN_MESSAGE type differ from other message dialog types in JOptionPane?
Answer:PLAIN_MESSAGE displays a dialog containing only the message with no icon, whereas other message dialog types display an icon indicating the message nature (error, information, warning, question).
Question:List and describe the different message dialog types available in JOptionPane.
Answer:.ERROR_MESSAGE: Indicates an error to the user. .INFORMATION_MESSAGE: Shows an informational message that the user can dismiss. .WARNING_MESSAGE: Warns the user of a potential problem. .QUESTION_MESSAGE: Poses a question to the user. .PLAIN_MESSAGE: Displays a message without any icon.
Question:In the integer addition program using dialog boxes, how are string inputs converted to integers?
Answer:Using Integer.parseInt on the string inputs converts them to integer values for computation.
Question:What is the general process followed by the sum program involving dialog boxes?
Answer:1. Display input dialogs to enter two numbers as strings. 2. Convert these strings to integers. 3. Calculate the sum of the two integers. 4. Display the result in a message dialog box.
Question:What is an array as introduced in Lesson 3?
Answer:An array is a collection of elements that have the same data type; an array is an object.
Question:How do you declare and allocate an integer array with 5 elements in Java?
Answer:You declare it as int anintarray = new int[5]; which allocates 5 elements.
Question:How do you assign a value to an element of an integer array in Java?
Answer:You assign it by specifying the index, e.g., anintarray[0] = 100; assigns 100 to the first element.
Question:What is automatic initialization when declaring an integer array with values in Java?
Answer:You can declare and initialize an array like int anintarray[] = {100, 200, 300, 400, 500}; where the size is allocated automatically based on the initial values.
Question:Can you explicitly declare the size of an array when initializing it with values in Java?
Answer:No, you cannot explicitly declare the array size while declaring initial values, so int anintarray[5] = {100, 200, 300, 400, 500}; is not allowed.
Question:What is printed by the following Java code snippet? int[] x = {2, 4, 70, 33, 3}; for (int y = 0; y < 5; y++) System.out.println("x[" + y + "] " + x[y]);
Answer:The contents of array x are printed with indices: x[0] 2 x[1] 4 x[2] 70 x[3] 33 x[4] 3
Question:What is the String data type in Java and how is it related to classes?
Answer:String with a capital S is a class built into Java, defined in java.lang.String, which is automatically imported into every program.
Question:How do you declare and initialize a String object in Java?
Answer:You can do it in two steps: 1. Declare: String strSample; 2. Initialize: strSample = new String("Hello"); Or combine in one line: String strSample = new String("Hello");
Question:What is the shortcut for initializing a String variable with a value in Java?
Answer:You can write String str1 = "Hello" without explicitly using the new keyword.
Question:How do you assign a new value to a String variable in Java?
Answer:You simply assign a new string, e.g., str1 = "Hi"; which replaces the previous value.
Question:What is string concatenation and how is it performed in Java?
Answer:Concatenation joins strings together using the + operator, e.g., str1 = str1 + " Again"; adds " Again" to the existing string.
Question:Can you directly assign an int value to a String variable in Java?
Answer:No, assigning an int to a String variable causes an exception because they are incompatible types.
Question:How does the split method work for a String in Java?
Answer:The split method divides the string at a specified regular expression (regex) and returns an array of substrings.
Question:How does the compareTo method compare two strings in Java?
Answer:compareTo compares two strings lexicographically in dictionary order based on Unicode values of each character.
Question:What does the compareToIgnoreCase method do?
Answer:It compares two strings lexicographically ignoring the case differences (uppercase/lowercase).
Question:What will be the output of compareTo between "ANGELS" and "Angels"?
Answer:The result is a negative value because uppercase letters have lower Unicode values than lowercase letters.
Question:What does the length method do for a String?
Answer:The length method returns the number of characters present in the string.
Question:How do the replace, replaceAll, and replaceFirst methods work on Strings?
Answer:- replace replaces each matching character/text - replaceAll replaces all occurrences matching a regex pattern - replaceFirst replaces only the first occurrence
Question:What does the substring method do on a String in Java?
Answer:substring extracts a part of the string and returns it. It can take one or two parameters indicating start and optional end indices.
Question:What does the equalsIgnoreCase method do when comparing two strings?
Answer:It returns true if two strings are identical regardless of case, and false if the strings are different.
Question:How does the contains method work in a string?
Answer:The contains method checks whether the specified sequence of characters is present in the string or not.
Question:What does the indexOf method return when used on a string?
Answer:It returns the index of the first occurrence of the specified character or substring within the string.
Question:What is the function of the trim method on a string?
Answer:The trim method removes any leading (starting) and trailing (ending) whitespaces from the specified string.
Question:What does the charAt method return when called on a string with an index?
Answer:It returns the character at the specified index of the string.
Question:What do the toLowerCase and toUpperCase methods do to a string?
Answer:toLowerCase converts all characters in the string to lowercase, while toUpperCase converts all characters to uppercase.
Question:What is the purpose of the startsWith and endsWith methods on strings?
Answer:startsWith checks whether the string starts with the letters passed as arguments, and endsWith checks whether the string ends with the letters passed as arguments.
Question:How does the concat method work between two strings?
Answer:The concat method appends one string to the end of another string, combining them.
Question:What does the setEchoChar method do in the context of input fields?
Answer:The setEchoChar method changes the input string display to the given character or string.
Question:How is an array populated and manipulated in the sample Java program?
Answer:The program inputs 5 numbers into the array, calculates their sum, and sorts them in ascending order using Arrays.sort.
Question:What method is used to find the length of a string in the given code?
Answer:The method str5.length is used to find the length of the string str5.
Question:How can you retrieve a character at a specific index in a string?
Answer:By using the method str4.charAt(10), you can get the character at index 10 of the string str4.
Question:Explain how to extract a substring from a string starting at a given index.
Answer:Using str3.substring(5) extracts the substring of str3 starting from index 5 to the end.
Question:What do the methods indexOf and lastIndexOf return when used on a string?
Answer:str5.indexOf("ss") returns the index of the first occurrence of "ss" in str5, and str3.lastIndexOf('a') returns the index of the last occurrence of 'a' in str3.
Question:How are two strings compared for equality considering and ignoring case sensitivity?
Answer:Using st1.equals(st2) compares two strings for exact equality with case sensitivity, and st1.equalsIgnoreCase(st2) compares the strings ignoring case differences.
Question:What method converts a string to uppercase and which converts it to lowercase?
Answer:str3.toUpperCase converts to uppercase, and str5.toLowerCase converts to lowercase.
Question:What is the difference between compareTo and compareToIgnoreCase methods when comparing strings?
Answer:compareTo compares two strings lexicographically with case sensitivity, while compareToIgnoreCase compares them ignoring case.
Question:How do you remove leading and trailing spaces from a string?
Answer:By using the trim() method, e.g., st3.trim removes leading and trailing spaces from st3.
Question:How can you replace all occurrences of a specific character in a string?
Answer:Using the replace method, for example st3.replace('e', char kar2), replaces all 'e' characters with char kar2.
Question:What does the toCharArray method do in the string context?
Answer:st1.toCharArray converts the string st1 into an array of characters.
Question:How can you check if a string contains a specific substring?
Answer:Using str5.contains("you") checks if str5 contains the substring "you".
Question:In the vowel and consonant counting program, how are vowels identified in the sentence?
Answer:By checking if the character at position i is between 'a' and 'z' and is one of 'a', 'e', 'i', 'o', or 'u', it is counted as a vowel.
Question:How does the vowel and consonant counting program differentiate digits, spaces, and special characters?
Answer:If the character is between '0' and '9', it is counted as a digit; if it is a space ' ', it is counted as space; otherwise, it is considered a special character.
Question:Describe the approach to find the longest words in an input sentence provided in the code.
Answer:The sentence is split into words using split(" "), then the length of each word is stored and compared to find the longest length. Words matching the longest length are printed.
Question:What is the purpose of the cls_ReverseWord class in the given code?
Answer:It takes a string input and prepares to reverse the input string by creating character arrays rev and revWord to hold the reversed characters.
Question:What is a pangram according to the provided text?
Answer:A pangram is a string that contains all the letters of the alphabet.
Question:In the pangram detection program, what is the purpose of sorting the characters of the string?
Answer:Sorting the characters helps in comparing adjacent characters to count distinct letters and determine if all 26 letters of the alphabet are present.
Question:How does the pangram program determine that the sentence is a pangram?
Answer:It counts distinct letters by checking if adjacent sorted characters differ, increasing the count, and if the count reaches 26, it concludes the sentence is a pangram.
Question:What is a method as described in the text?
Answer:A method is a blocked statement that performs a specific task; in object-oriented languages, it is similar to a procedure, function, or subroutine in procedural languages.
Question:How is a simple method defined and used in the example 'MySecond' class?
Answer:The simple method 'greet' is defined with no arguments and no return value, it prints a greeting message, and is called within the main method.
Question:What parts of a method are identified in the example and their roles?
Answer:The method header defines the method's signature (like 'public static void greet'), the method call invokes the method (e.g., 'greet()'), and the method definition contains the body or statements executed.