Module 1: Getting started with Java
Knowledge check: Introduction to software development
Practice Assignment
1. In the SDLC, in which phase will you code the actual software?
- Planning
- Design
- Testing
- Development
Explanation: The development phase is where the actual coding happens. During this phase, developers write the code based on the designs created in the previous phase. It is where the software begins to take shape and becomes functional.
2. What is the purpose of the maintenance phase in the SDLC?
- Software development
- Planning the blueprint for how the software should work
- Fixing defects and making enhancements
- Requirement gathering
Explanation: The maintenance phase is about ensuring the software remains functional after it is deployed. It includes fixing defects, updating software to add new features, and making improvements as per user feedback.
3. What is the primary goal of the deployment phase in the SDLC?
- Coding
- Requirement gathering
- Fixing defects
- Delivering the software to users
Explanation: The deployment phase involves the actual release of the software to the end users. This includes installing the software on users’ machines and ensuring it operates in the real-world environment.
4. What is the primary purpose of a version control system like GIT?
- To fix defects and make enhancements
- To gather requirements
- To keep track of changes and updates to the code over time
- To deploy code
Explanation: A version control system like GIT tracks changes to the codebase, allowing developers to collaborate, roll back to previous versions, and manage updates efficiently.
5. What is meant by "End-to-end development" in the context of a fullstack developer's responsibilities?
- Focusing solely on user interface design
- Involvement in every stage of the development process, from designing user interfaces to writing server-side logic to deploying and maintaining applications
- Focusing only on database management
- Specializing in either frontend or back-nd development only
Explanation: End-to-end development refers to developers who are involved in all stages of application development, from the front end (user interface) to the back end (server-side logic and databases).
Knowledge check: Java basics: Variables and operators
Practice Assignment
6. What is the correct way to declare and initialize an integer variable named yearsOld in Java?
- String yearsOld = 10;
- double yearsOld = 10.0;
- int yearsOld = 10;
- int yearsOld = “ten”;
Explanation: In Java, an integer variable is declared using the int
keyword, followed by the variable name and the value to initialize it.
7. What will be the output of the following code?
- 12, Tom
- Age: 12, Name: Tom
- Age: 12 Name Tom
- Age: 12 Name: Tom
Explanation: The code outputs the formatted string with a variable age
and name
. The correct syntax for concatenating strings and variables ensures proper formatting.
8. What is wrong with the following code?
- The value 5 should be a number and should be assigned as number = 5.
- The variable number should be declared as a character.
- The value 5 should be a character.
- The variable number is not declared.
Explanation: The variable number
needs to be declared before it is assigned a value. The code is missing the declaration of the variable, leading to an error.
9. Which of the following variable declarations is the most appropriate to store a single letter 'K' in Java?
- String letter = ‘K’;
- char letter = ‘K’;
- char letter = “K”;
- String letter = “K”;
Explanation: In Java, the char
data type is used to store a single character, such as 'K'
. A String
is used to store a sequence of characters.
10. What is the output of the following code?
- Sum: a + b
- Sum: 15
- Sum: 5 + 10
- Sum: 510
Explanation: The code is concatenating the values of a
and b
correctly to display the sum of 5 and 10 as 15.
Knowledge check: Strings: Working with text in Java
Practice Assignment
11. In the following code block, which snippets would you insert to correctly determine the String length in Java?
- length()
- count()
- size()
- getLength()
Explanation: In Java, the length()
method is used to find the number of characters in a string.
12. While formatting a document, you’re asked to replace all instances of the word "old" with the word "new". You’ve been provided with the following code. What method would you insert to make the required changes?
- change()
- substitute()
- swap()
- replace()
Explanation: The replace()
method is used to replace one substring with another within a string.
13. You are developing a user registration system and need to ensure that the first letter of a username is not a digit. You are using the following code block. Which method would you insert to check the first character?
- getChar(int index)
- firstChar()
- charAt(int index)
- initialChar()
Explanation: The charAt()
method returns the character at a specified index of a string. It is useful for checking individual characters in a string.
14. Toni is writing a Java program to set a password for the alarm system. Toni needs to compare a user's input with a stored password in a case-sensitive manner. Toni is working with the following code. Which method would Toni insert to achieve this?
- compare()
- equals()
- match()
- ==
Explanation: The equals()
method compares two strings in a case-sensitive manner. If the strings are equal, it returns true
; otherwise, it returns false
.
15. True or False: To capitalize the first letter of each word in a String, you can use the toTitleCase() method.
- True
- False
Explanation: Java does not have a built-in toTitleCase()
method. Instead, you can use other string manipulation methods or libraries to convert the first letter of each word to uppercase.
Module quiz: Getting started with Java
Graded Assignment
16. If score = 6, what is the new value of score after score *= 3?
- 2
- 9
- 6
- 18
Explanation: The *=
operator multiplies the value of score
by 3, resulting in 18.
17. Select the correct output for the following code:
- false
- 25
- true
- 20
Explanation: The condition evaluates to true
based on the values and logic in the code.
18. Alex is writing a program to store user information. How would he declare an integer variable named age and initialize it to 20?
- int age = 20;
- age = 20;
- var age = 20;
- integer age = 20;
Explanation: In Java, an integer is declared using the int
keyword, followed by the variable name and its value.
19. Imagine you’re developing a Java application and need to check the size of Strings. What method would you use to find the length of the String text = "OpenAI"?
- text.getLength()
- text.length()
- text.count()
- text.size()
Explanation: The length()
method is used to determine the number of characters in a string.
20. Imagine you are writing a welcome message for a user in an application. Which code snippet would you use to combine the Strings greeting = "Hello" and name = "Alice" with a comma and a space in between?
- greeting.concat(“, “, name)
- greeting.concat(“, ” + name)
- greeting + name + “, “
- greeting + “, ” + name
Explanation: This approach concatenates the strings properly with the required comma and space in between.
21. Tom is debugging his Java program and comes across a line of code that is causing an error. What is wrong with the following code snippet that Tom identifies?
- The variable name is incorrect.
- The variable type int cannot hold decimal values.
- The value should be enclosed in double quotes.
- There’s nothing wrong; the code is correct.
Explanation: An incorrect variable name causes the error. In Java, variable names should follow the naming conventions.
22. Which of the following is a valid variable name?
- 1stName
- int
- my_variable
- class
Explanation: Variable names must start with a letter, underscore, or dollar sign and cannot contain spaces or special characters like 1stName
.
23. To represent a logical OR operation in Java, use ________.
- !
- &
- &&
- ||
Explanation: The ||
operator represents logical OR in Java, which evaluates to true
if either condition is true.
24. Select the correct output for the following code:
- 3
- 2
- 4
- 1
Explanation: Based on the operation in the code, the output will be 3
.
25. In Java, Strings are mutable, meaning they can be changed after creation.
- False
- True
Explanation: In Java, Strings are immutable. Once created, they cannot be changed. Any modification results in the creation of a new string.