OOP Flashcards

1
Q
  • What is the JVM?
A

The JVM (Java Virtual Machine) is like a virtual computer that runs Java programs. It translates Java code into instructions that the computer can understand. It also manages memory and ensures that the program runs smoothly. The JVM allows Java programs to work on different types of devices and operating systems without needing to be rewritten.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  • What does Java compilation mean?
A

Java compilation is the process of converting human-readable Java code into a format that can be understood and executed by the Java Virtual Machine (JVM), enabling platform-independent execution of Java programs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  • What is Java bytecode?
A

Java bytecode is a low-level, platform-independent representation of Java code. It is generated during the compilation process and can be executed by the JVM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  • What is the difference between the JRE and the JDK?
A

JRE (Java Runtime Environment) is responsible for executing Java applications, while the JDK (Java Development Kit) includes the JRE and additional tools needed for developing Java applications, such as compilers and debuggers. The JRE is used for running Java programs, while the JDK is used for both developing and running Java programs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  • What is the Iterable interface?
A

The Iterable interface is the foundation of classes that represent a collection of elements that can be iterated (looped) through.

The main purpose of the Iterable interface is to provide a standardized way to iterate over the elements of a collection using an enhanced for loop (also known as a “for-each” loop) or by using iterators.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  • What is the Collection interface?
A

The Collection interface in Java is the root interface of the Java Collections Framework hierarchy. It represents a group of objects, known as elements, and provides a set of methods for working with these elements like adding, removing, and querying elements in a collection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  • What is the Map interface?
A
  • The Map interface in Java represents a collection of key value pairs, where each key is unique. It provides a way to associate values with keys and retrieve values based on their corresponding keys. The Map interface does not inherit from the Collection interface but is an independent interface in the Java Collections Framework.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  • Compare sets, lists, and queues in Java.
A
  • Sets:
    • Collection of unique elements. No specific order.
    • Ensures uniqueness.
    • Key implementations: HashSet, TreeSet, LinkedHashSet.
  • Lists:
    • Ordered collection of elements.
    • Allows duplicates.
    • Maintains insertion order.
    • Accessible by index.
    • Key implementations: ArrayList, LinkedList, Vector.
  • Queues:
    • Follows First-In-First-Out (FIFO) order.
    • Elements added at the end, removed from the beginning.
    • Key implementations: LinkedList, ArrayDeque, PriorityQueue.
    • Useful for task scheduling, event handling, and algorithms like breadth-first search.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  • Compare ArrayList and LinkedList in Java.
A
  • ArrayList provides efficient random access and retrieval by index but slower for insertion and removal in the middle.
  • LinkedList offers fast insertion and removal at the beginning and end but slower for random access.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  • Are sets sorted in Java?
A
  • In general, sets in Java do not guarantee a specific order of elements.
  • The Set interface itself does not define any order for its implementations.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  • What control statements are available in Java?
A
  • Conditional Statements
  • Looping Statements : for, while, do-while, foreach
  • Jump Statements: break, continue, return, throw
  • Branching Statements: switch
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  • Compare the different looping constructs in Java.
A
  • for loop: Used when the number of iterations is known or can be determined.
  • while loop: Used when the number of iterations is not known in advance and depends on a condition.
  • do-while loop: Similar to the while loop, but the condition is evaluated after the loop body.
  • foreach loop: Designed specifically for iterating over arrays or collections.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  • Compare the different conditional constructs in Java.
A
  • if statement: Used for basic conditional branching.
  • if-else statement: Allows for branching between two blocks of code.
  • if-else if-else statement: Enables branching between multiple conditions.
  • switch statement: Provides multiple branching options based on the value of a variable or expression.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  • What is a while loop?
A

A while loop is a control flow statement in Java that allows for repeated execution of a block of code as long as a given condition remains true. It is used when the number of iterations is not known in advance and depends on the condition being evaluated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  • How do you manually break out of a loop?
A

In Java, you can manually break out of a loop using the break statement.
The break statement allows you to exit the loop prematurely, regardless of the loop condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  • What does the var keyword mean?
A

The var keyword in Java is a feature introduced in Java 10 that allows for local variable type inference. It allows you to declare variables without explicitly specifying their types, and the compiler infers the type based on the assigned value. The var keyword can only be used for local variables within method bodies, loops, or other blocks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
  • What are lambda expressions? How are they used in Java development?
A

In Java, lambda expressions are primarily used in functional interfaces, which are interfaces with a single abstract method (SAM). Instead of implementing the interface explicitly with a separate class or anonymous inner class, you can use lambda expressions to provide a concise implementation of the abstract method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
  • What are primitive types in Java? Give some examples.
A

Primitive types in Java are basic data types that represent the most fundamental types of data.
They are not objects and do not have methods or properties like objects do.
Example:
- boolean isTrue = true
- int i = 42
- char c = ‘A’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
  • What is the difference between primitive types and reference types?
A

The main difference between primitive types and reference types in Java lies in how they are stored, treated, and interacted with in the program. Here are the key distinctions:

1. Primitive types:

  • Represent basic data types in Java and are predefined by the language itself (e.g., int, boolean, float,char).
  • Store their values directly in memory, typically on the stack, with a fixed size.
  • Have default values if not explicitly initialized (e.g., 0 for numeric types, false for boolean).
  • Are assigned by value, meaning the value is copied to the assigned variable. Comparison checks the equality of values.
  • Do not have methods or properties since they are not objects.
  • Have corresponding wrapper classes (e.g., Integer, Boolean) that provide object representations and additional functionality.

2. Refrence types:

  • Represent complex objects that are created using classes or interfaces defined by the programmer or language(e.g., String, ArrayList).
  • Store a reference (memory address) to the actual object in memory, typically on the heap, allowing for dynamic memory allocation.
  • Have a default value of null if not explicitly initialized, indicating that they don’t reference any object.
  • Are assigned by reference, meaning the reference (memory address) is copied to the assigned variable. Comparison checks if the references point to the same object, not the equality of values.
  • Can have methods and properties, as they are objects that can be instantiated from classes or interfaces.
  • Do not have wrapper classes since they are already objects.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
  • What is a class in Java?
A

In Java, a class is a fundamental building block and a blueprint for creating objects. It serves as a template that defines the properties (attributes) and behaviors (methods) that objects of that class can have.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
  • What is an object in Java?
A

In Java, an object is a fundamental entity created from a class. It represents a specific instance of that class and encapsulates its own set of data (attributes) and behaviors (methods). Objects are the main building blocks used to interact with and manipulate data in Java programs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
  • What is a constructor?
A

In Java, a constructor is a special method that is used to initialize objects of a class. It is called automatically when an object is created using the new keyword. The purpose of a constructor is to ensure that the newly created object is properly initialized and in a valid state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
  • What is an enum in Java?
A

In Java, an enum is a special data type used to define a set of constants. It represents a fixed number of predefined values, often referred to as enumeration constants or enumerators. enum provides a way to define a collection of related values that have a specific meaning or purpose.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q
  • Explain the difference between a class and an enum.
A

1. Purpose:
- Class: A class is a blueprint or template for creating objects. It defines the structure, behavior, and state of objects.
- Enum: An enum is used to represent a fixed set of predefined values or constants. It provides a way to define a collection of related values.
2. Instantiation:
- Class: Objects are created from classes using the new keyword. Each object has its own set of attributes and can have its own state.
- Enum: Enum constants are predefined and finite. They are automatically instantiated and have a fixed number of instances determined by the defined constants.
3. Extensibility:
- Class: Classes can be extended by creating subclasses that inherit the properties and behaviors of the parent class. Inheritance allows for code reuse and the creation of hierarchical relationships between classes.
- Enum: Enums cannot be extended or subclassed. The set of enum constants is fixed and cannot be modified once defined.
4. Usage:
- Class: Classes are used to model complex entities, encapsulate data and behavior, and implement various features and functionalities in an object-oriented program.
- Enum: Enums are typically used to represent a limited set of related constants or options, such as days of the week, months, status codes, or choices for an enumeration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q
  • Explain the difference between a class and a record.
A

The main difference between a class and a record in Java is that a class is a general-purpose blueprint for creating objects with customizable attributes and behaviors, while a record is a special type of class primarily used for immutable data storage and automatic generation of common methods like constructors, getters, setters, and equals()/hashCode().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q
  • What are interfaces? Why should we use them?
A

Interfaces in Java are a way to define contracts or sets of rules that classes must follow. They allow for abstraction, polymorphism, multiple inheritance of types, code reusability, modularity, and facilitate API design and collaboration. Interfaces provide a foundation for designing reliable, maintainable, and flexible software systems by promoting consistent behavior and enabling interchangeable components.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q
  • What is inheritance?
A

Inheritance is a mechanism in object-oriented programming where a subclass can inherit attributes and methods from a superclass. It promotes code reuse, creates a hierarchical structure, and allows for specialization and customization of behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q
  • Is multiple inheritance allowed in Java?
A

No, Java does not support multiple inheritance of classes, where a class can inherit from multiple parent classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q
  • What is a static class member?
A

A static class member in Java is a variable or method that belongs to the class itself, not to individual instances. It is shared among all instances, accessed using the class name, and stored in a common memory location. It is useful for defining shared data or behavior associated with the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q
  • Can a static method use non-static members?
A

No, a static method in Java cannot directly access or use non-static members of a class. Static methods are not associated with any particular instance of a class, so they cannot access instance-specific data or behaviors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q
  • What does the final keyword mean in Java?
A

In Java, the final keyword is used to denote that a variable, method, or class is immutable or cannot be overridden or extended, depending on its usage.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q
  • What does the abstract keyword mean in Java?
A

The abstract keyword in Java is used to declare abstract classes and methods, providing a framework for creating related subclasses and enforcing a common structure and behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q
  • What is overloading in Java?
A

In Java, method overloading refers to the ability to have multiple methods in a class with the same name but different parameter lists. It allows a class to provide different ways of invoking a method based on the type and/or number of arguments passed to it.

34
Q
  • What is overriding in Java?
A

In Java, method overriding refers to the ability of a subclass to provide a different implementation of a method that is already defined in its superclass. It allows a subclass to provide its own specific implementation of an inherited method, thereby customizing the behavior of that method for the subclass.

35
Q
  • What is the difference between overloading and overriding?
A

Overloading is about providing multiple methods with the same name but different parameter lists within a class, while overriding is about providing a different implementation of a method in a subclass that is already defined in its superclass. Overloading occurs within the same class, while overriding occurs between a superclass and its subclass.

36
Q
  • What is null?
A

In Java, null is a special literal that represents the absence of a value or the absence of an object reference. It is used to indicate that a variable or object reference does not currently refer to any object in memory.

37
Q
  • Compare the access modifiers in Java.
A

Access modifiers in Java control the visibility and accessibility of classes, methods, variables, and constructors within a program.

  • public: Provides the highest level of accessibility. Public members are accessible from any class or package.
  • protected: Allows access within the same package and subclasses, even if they are in a different package.
  • default (no keyword specified): Provides access within the same package only. It is the default access level when no access modifier is specified.
  • private: Provides the highest level of encapsulation. Private members are accessible only within the same class.
38
Q
  • What is the default access modifier in a class?
A

The default access modifier in a class limits access to the same package and does not allow access from other packages. It helps establish package-level encapsulation and is the default access level when no access modifier is specified.

39
Q
  • What is the purpose of the equals() method?
A

The equals() method in Java is used to compare the equality of two objects. It is defined in the Object class and can be overridden in custom classes to provide a specific implementation for object comparison.

40
Q
  • What is the difference between == and equals()?
A

The == operator compares the memory addresses of objects or the values of primitive types, while the equals() method compares the content or internal state of objects.

41
Q
  • What is the difference between long and Long?
A

long is a primitive data type representing a 64-bit signed integer, while Long is a wrapper class that provides an object representation of long and offers additional methods and functionality.

42
Q
  • Which can store bigger numbers, long or Long?
A

Both long and Long can store the same range of values, but long is a primitive type that uses less memory, while Long is a wrapper class that provides additional functionality at the cost of increased memory usage.

43
Q
  • What kind of packages do you know under java.util.* ? Bring at least 3 examples.
A
  • java.util.ArrayList: it provides a dynamic array implementation.
  • java.util.HashMap: it provides a key-value mapping implementation.
  • java.util.Date: it represents a specific moment in time.
  • java.util.LinkedList: it provides a doubly-linked list data structure, allowing efficient insertion and deletion operations at both ends of the list.
  • java.util.HashSet: it provides an unordered collection of unique elements, where duplicate values are not allowed.
  • java.util.Scanner: it provides a convenient way to read input from various sources in Java, such as standard input, files, or strings.
44
Q
  • Explain the Single Responsibility Principle.
A

The Single Responsibility Principle (SRP) states that a class should have only one responsibility or purpose. In Java, this principle encourages classes to focus on a specific functionality, leading to improved code maintainability and reusability. Classes should have clear responsibilities, encapsulate related behavior, avoid bloated classes, and separate concerns.

45
Q
  • Explain the Interface Segregation Principle.
A

It promotes the idea of segregating interfaces into smaller, more specialized ones instead of having a single large interface. By doing so, clients can choose to implement only the interfaces that are relevant to them, reducing unnecessary dependencies and improving code flexibility and maintainability.

46
Q
  • What is composition over inheritance?
A

Composition over inheritance is a design principle that suggests favoring object composition over class inheritance for code reuse and extensibility. Instead of inheriting behavior from parent classes, composition involves creating complex objects by combining or “composing” simpler objects.

47
Q
  • What is a model class?
A

A model class represents a specific entity or concept in an application, encapsulating its data and behavior. It defines the structure, attributes, and operations related to that entity.

48
Q
  • What is a service class?
A

A service class encapsulates the implementation details of a service and exposes methods or interfaces through which other components or modules can interact with the service. It acts as an intermediary between the presentation layer (user interface) and the data access layer, coordinating the processing of requests and managing the business logic.

49
Q
  • Explain the Open/Closed principle.
A

The Open/Closed Principle is a fundamental principle in software design that states should be open for extension but closed for modification. In other words, the behavior of a software entity should be extendable without modifying its existing code.

50
Q
  • Explain the Liskov Substitution Principle.
A

If a program is designed to work with objects of a certain class, it should also work correctly with objects of any of its subclasses without requiring any changes to the program. So a subclass should be able to replace its parent class in any context without affecting the correctness of the program.

51
Q
  • Explain the Dependency Inversion Principle.
A

The Dependency Inversion Principle (DIP) aims to reduce the coupling between high-level modules and low-level modules in a software system. It suggests that high-level modules should not directly depend on low-level modules; both should depend on abstractions instead. This helps create more flexible, maintainable, and easily extendable software.

52
Q
  • What do we mean by the Gang of Four (GoF) Design Patterns? Can you name some of these patterns?
A

The Gang of Four (GoF) Design Patterns refer to a collection of 23 design patterns. These patterns provide solutions to common design problems in object-oriented software development.

  • Singleton: Ensures that a class has only one instance and provides a global point of access to it.
  • Factory Method: Is a design pattern that provides an interface for creating objects in a super class, but allows subclasses to alter the type of objects that will be created.
  • Observer: Establishes a one-to-many dependency between objects, so that when one object changes its state, all dependent objects are notified and updated automatically.
  • Adapter: Converts the interface of a class into another interface that clients expect, enabling classes with incompatible interfaces to work together.
  • Decorator: Allows behavior to be added to an individual object dynamically, without affecting the behavior of other objects of the same class.
  • Strategy: Defines a family of algorithms, encapsulates each one, and makes them interchangeable. It allows the algorithm to vary independently from the clients that use it.
  • Template Method: Defines the skeleton of an algorithm in a base class, allowing subclasses to override certain steps of the algorithm without changing its structure.
  • Composite: Composes objects into tree structures to represent part-whole hierarchies. It allows clients to treat individual objects and compositions of objects uniformly.
  • Iterator: Provides a way to access elements of an aggregate object sequentially without exposing its underlying representation.
  • State: Allows an object to alter its behavior when its internal state changes. It appears as if the object has changed its class.
53
Q
  • What are the risks associated with using the GoF design patterns?
A

The risks associated with using the Gang of Four design patterns include over-engineering, increased complexity, a learning curve for developers, potential performance overhead, implementation constraints, maintenance challenges, and potential difficulties in team collaboration.

54
Q
  • What do we mean by YAGNI?
A

YAGNI stands for “You Ain’t Gonna Need It.” It is a principle in software development that suggests avoiding unnecessary or premature implementation of functionality. The YAGNI principle states that developers should only implement features or code that is necessary for the current requirements rather than speculating and adding functionality that may be needed in the future.

55
Q
  • What do we mean by SLAP?
A

SLAP stands for “Single Level of Abstraction Principle.” It is a software design principle that emphasizes the importance of keeping code at a consistent level of abstraction within a method or function. The principle suggests that each method should have a clear and focused purpose, with all the statements within the method operating at the same level of abstraction.

56
Q
  • What do we mean by KISS?
A

KISS stands for “Keep It Simple, Stupid.” It is a principle in software development that advocates for simplicity and avoiding unnecessary complexity in design and implementation. The KISS principle suggests that systems, code, and solutions should be kept as simple as possible, without sacrificing functionality or quality.

57
Q
  • What is the Repository Pattern?
A

The Repository Pattern is a software design pattern that separates the application’s data access logic from the rest of the application. It provides a layer of abstraction between the application and the data persistence layer, such as a database or external API. The pattern defines a set of interfaces or contracts for performing CRUD (Create, Read, Update, Delete) operations on data entities. By using the Repository Pattern, developers can easily switch between different data sources or storage technologies without affecting the application’s core logic, making it more modular and testable.

58
Q
  • What is a CRUD interface?
A

A CRUD interface defines methods or functions that enable developers to perform these operations on the underlying data source, such as a database, file system, or API. These operations are the essential building blocks for interacting with data and are widely used in software development to manage data persistence.

59
Q
  • Why is unit testing a good practice?
A

unit testing improves code reliability, maintainability, and developer productivity. It reduces the occurrence of bugs, aids in code refactoring, and supports agile development practices such as continuous integration and deployment.

60
Q
  • What is JUnit?
A

JUnit is an open-source testing framework for Java. It provides a simple and standardized way to write and execute unit tests for Java applications.

61
Q
  • What is a parameterized test?
A

A parameterized test is a feature provided by testing frameworks, including JUnit, that allows you to run the same test logic with different sets of input data. It enables testing multiple scenarios or variations of a test case without duplicating the test code.

62
Q
  • What options do you have in NUnit to create parameterized tests?
A
  • TestCaseAttribute: NUnit provides the TestCaseAttribute that allows you to specify test cases with explicit values directly in the test method. You can provide multiple instances of the attribute, each representing a different set of input values.
  • TestCaseSourceAttribute: With the TestCaseSourceAttribute, you can specify a method or property that provides the test cases. The method or property should return an IEnumerable or an array of objects, where each object represents a set of input values.
  • ValueSourceAttribute: The ValueSourceAttribute allows you to specify an array or a constant as the source of test case values. Each value in the array or constant represents a single input value.
63
Q
  • What is mocking?
A
  • Mocking is a technique used in software testing and development to create objects that simulate the behavior of real objects. It involves creating mock objects that mimic the interactions of real objects or components, allowing you to isolate and test specific parts of a system independently.
64
Q
  • What is the difference between mocking, stubbing and faking?
A
  • Mocking is about simulating interactions to verify behavior creating mimic objects.
  • Stubbing is about providing controlled responses to specific testing method calls.
  • Faking is about replacing real components(Classes, Interfces etc) with simplified versions for testing efficiency.
65
Q
  • What are relational databases? What are their advantages and disadvantages?
A

Relational databases are a type of database management system that organize and store data in a structured manner using tables with rows and columns.

They offer advantages such as data integrity, flexible querying with SQL, scalability, and standardization.

However, relational databases have limitations including schema rigidity, scaling challenges, potential performance issues with complex data structures, and may be less suited for unstructured data.

66
Q
  • How do you associate entities to each other in a relational database model?
A
  • One-to-One (1:1) Relationship: Each record in one entity is associated with exactly one record in another entity. This relationship is represented by linking the primary key of one entity as a foreign key in the other entity.
  • One-to-Many (1:N) Relationship: Each record in one entity can be associated with multiple records in another entity, but each record in the second entity can be associated with only one record in the first entity. This relationship is represented by placing the primary key of the “one” entity as a foreign key in the “many” entity.
  • Many-to-Many (N:M) Relationship: Multiple records in one entity can be associated with multiple records in another entity. This relationship is implemented using an intermediate table that holds the foreign keys of both entities.
67
Q
  • What are tables in a relational database?
A

Tables in a relational database are the fundamental structures that store and organize data. They are rectangular arrangements of rows and columns, resembling a spreadsheet. Each table represents a specific entity or concept in the database schema.

68
Q
  • What is a primary key?
A

A primary key is a unique identifier for each record or row in a database table. It uniquely identifies individual rows and ensures the integrity and uniqueness of data within the table.

69
Q
  • What is a foreign key?
A

A foreign key is a concept in relational databases that establishes a relationship between two tables. It is a column or a set of columns in one table that refers to the primary key in another table. The purpose of a foreign key is to enforce referential integrity and maintain the integrity and consistency of the data within the database.

70
Q
  • What does the SQL abbreviation stand for?
A

SQL stands for Structured Query Language.

71
Q
  • What are some of the SQL database providers that you’ve heard of?
A
  • PostgreSQL
  • Oracle Database
  • Microsoft SQL Server
72
Q
  • What are SQL data types? Are there any differences in data types between different SQL databases?
A

SQL data types are used to define the type of data that can be stored in a database column.

While there is a general consensus on commonly used data types, there can be some variations and proprietary data types specific to certain database systems. For example, Oracle has data types like CLOB (character large object) and BFILE (binary file), while Microsoft SQL Server has data types like MONEY and XML.

73
Q
  • What are constraints in SQL?
A

Constraints define limitations or requirements that the data must adhere to when performing data manipulation operations, such as inserting, updating, or deleting records to enforce data integrity and ensure the consistency and correctness of the data.

74
Q
  • How can we program different SQL databases in Java?
A

To program different SQL databases in Java, you can utilize the JDBC (Java Database Connectivity) API, which provides a standard way to interact with various databases using Java

75
Q
  • Which SQL statement is used to create tables? Describe the syntax briefly.
A

The SQL statement used to create tables is the CREATE TABLE statement. It is used to define the structure of a new table in a database.

CREATE TABLE table_name (
  column1 datatype [constraint],
  ...
  [table_constraint]
  );
  • CREATE TABLE: This keyword combination starts the CREATE TABLE statement.
  • table_name: This is the name you give to the table you want to create.
  • column1, ...: These are the names of the columns you want to include in the table. You need to specify a column name and its data type for each column.
  • datatype: This specifies the data type of the corresponding column.
  • [constraint]: Optional constraints can be added to the columns to enforce specific rules or requirements, such as primary key, foreign key, not null, unique, and check constraints.
  • [table_constraint]: This allows for defining constraints that apply to the entire table, such as primary key constraints spanning multiple columns.
76
Q
  • Which SQL statement can be used to insert values? Describe the syntax briefly.
A

The SQL statement used to insert values into a table is the INSERT INTO statement. It allows you to add new rows of data into an existing table.

INSERT INTO table_name (column1, ...)
  VALUES (value1, ...);
  • INSERT INTO: This keyword combination starts the INSERT INTO statement.
  • table_name: This is the name of the table where you want to insert the values.
  • column1, ...: These are the names of the columns in the table where you want to insert the values.
  • VALUES: This keyword indicates the beginning of the values being inserted.
  • value1, ...: These are the values you want to insert into the respective columns.
77
Q
  • Which SQL statement can be used to update values? Describe the syntax briefly.
A

The SQL statement used to update values in a table is the UPDATE statement. It allows you to modify existing data within a table.

UPDATE table_name
   SET column1 = value1, column2 = value2, ...
   WHERE condition;
  • UPDATE: This keyword starts the UPDATE statement.
  • table_name: This is the name of the table you want to update.
  • SET: This keyword indicates that you are specifying the columns to be updated.
  • column1, column2, ...: These are the names of the columns you want to update.
  • value1, value2, ...: These are the new values you want to assign to the respective columns.
  • WHERE: This keyword allows you to specify a condition that determines which rows to update. It is optional, but if omitted, all rows in the table will be updated.
  • condition: This is the condition that determines which rows to update. It uses comparison operators, logical operators, and column values to specify the condition.
78
Q
  • Which SQL statement can be used to delete rows?. Describe the syntax briefly.
A

The SQL statement used to delete rows from a table is the DELETE statement. It allows you to remove specific rows or all rows from a table.

DELETE FROM table_name
   WHERE condition;
  • DELETE FROM: This keyword combination starts the DELETE statement.
  • table_name: This is the name of the table from which you want to delete rows.
  • WHERE: This keyword allows you to specify a condition that determines which rows to delete. If omitted, all rows in the table will be deleted.
  • condition: This is the condition that determines which rows to delete. It uses comparison operators, logical operators, and column values to specify the condition.
79
Q
  • Which SQL statement can be used to create queries?. Describe the syntax briefly.
A

The SQL statement used to create queries is the SELECT statement. It allows you to retrieve data from one or more tables in a database. The SELECT statement is also commonly referred to as a query.

SELECT column1, column2, ...
   FROM table_name
   WHERE condition
//   GROUP BY column1, column2, ...
//  HAVING condition
//   ORDER BY column1, column2, ... ASC|DESC;
  • SELECT: This keyword starts the SELECT statement.
  • column1, column2, ...: These are the names of the columns you want to retrieve from the table. You can also use wildcard characters (*) to select all columns.
  • FROM: This keyword specifies the table or tables from which you want to retrieve data.
  • GROUP BY: This keyword is used to group the retrieved rows based on one or more columns. It is typically used with aggregate functions.
  • HAVING: This keyword allows you to specify conditions for filtering the grouped rows.
  • ORDER BY: This keyword is used to sort the retrieved rows based on one or more columns. You can specify the sorting order as ASC (ascending) or DESC (descending).
80
Q
  • How can you join tables together in SQL? When should you do it?
A

In SQL, you can join tables together using the JOIN clause(Inner Join, Left Join, Right Join, Full Join). Joining tables allows you to combine data from multiple tables based on a common column or relationship between them.

Joins are used when you need to combine data from multiple tables to retrieve information that is interconnected or related.