Shared Flashcard Set

Details

Java Finals Flashcards
Questions from my Java Class
42
Computer Science
Graduate
11/29/2024

Additional Computer Science Flashcards

 


 

Cards

Term
Is JavaScript statically typed?
Definition
No, you don't need to declare any types
Term
How is JavaScript similar to Java? (3)
Definition
1) Both are object-oriented
2) Java is pure OO, meaning everything is in a class or interface. JavaScript can be OO or structured
3) Similar syntax (if/else, loops, operators, comments)
Term
What is a React component? (3)
Definition
1) A JavaScript function that returns a single web element
2) The web element can either be part of a page or an entire page
3) The web element has HTML, CSS, and can have dynamic data with JavaScript
Term
What are the restrictions on how React components should be named? Give an example. How about the HTML tags within it?
Definition
You can refer to another component in the return statement of a component using a capital. Like . HTML tags within the statement must be lowercase.
Term
How to make your React component return more than one HTML element
Definition
Return a single div with multiple elements inside
Term
In the HMTL embedded in a React component, how do you incorporate JavaScript to access dynamic data?
Definition
JavaScript variables are placed within braces. These variables are replaced with data when the code runs.
Term
How do you render a list of data in React?
Definition
See code
Term
Code a React component for a button that, when clicked, changes its text to "I was clicked."
Definition
See code
Term
What is useState for?
Definition
Variables in a function normally die after the function completes. If you need data that lives on through the life of a component, you use useState.
Term
Explain the following code:
const [text, setText] = useState('Click me');
Definition
Initializes a piece of state with an initial value of 'Click me'. Text accesses the current state, and setText updates the state.
Term
How is useState in React similar to HTTP session objects in Java Servlets? (2)
Definition
1) Session objects allow you to store data for the life of a user session, and thus serve a similar purpose as useState in React.
2) Session data lives for a user session and is more global in scope. useState data lives while its enclosing component is alive and is local in scope.
Term
What is the advantage of React's component-based approach compared to a traditional or template-based approach? (3)
Definition
1) A template-based approach has the full webpage all together, with scripting inside for dynamic data. React organizes things in components -- chunks -- that can be reused in varies web pages. 2) The advantage is more reusable code 3) HTML can do this, but React makes it easier
Term
How is a URL like a remote function call? How are parameters specified? How do you call "FindByAuthor" in the bookstore example? (4)
Definition
-- A URL has a function name and parameter names

-- The first parameter begins with ? and subsequent parameters begin with &. Then the format is parameter=argument, with hyphens used to separate the words of a multi-word argument.

-- http://yoursite.com/findByAuthor?author=jones is analogous to findByAuthor(author), once "Jones" has been passed to the function.

-- From the frontend, you are calling a function that lives in the backend, on a remote server
Term
In Spring Java, what does the code look like to handle a URL request (a remote function call)? How does the code get the request parameters?
Definition
-- The URL request is mapped to a function. For example: @GetMapping("/findByAuthor") public list findByAuthor(@RequestParam String author) -- @RequestParam tells the query that these parameters are coming from the URL
Term
Spring Java relies on naming conventions -- you name things in a particular way and the framework does a lot of work for you. Describe the naming convention for queries within the framework. Do you need to code query methods?
Definition
-- A corresponding Repository interface extends DataStoreRepository. Using the findByDataMember format in the interface and the controller class enables DataStoreRepository to facilitate queries, so you don't need to code query methods.
Term
How to code Select * from Books where author='wolber' in Java Spring? (3)
Definition
this.BookRepository.findByAuthor("wolber")

Your entity class, Book.java, must be marked as @Entity and have a data member named author with set/get methods.

There must also be a method header for findByAuthor in the repository interface.
Term
What is persistent data and transient data? Where is each stored?
Definition
-- Persistent data exists even after an app closes and reopens. Transient data dies when the app closes.

-- Persistent data is stored on a disk and transient data is stored in RAM
Term
What are primary keys and foreign keys?
Definition
In a relational database, one entity can have many categories. For example, an entity can be a Person and belong to a Group. The entity will have an ID, a Person ID, and a Group ID. The ID that pertains to the entity is the primary key, and the IDs assigned to a Person and a Group are the foreign keys.
Term
1-1 relationship (with example)
1-to-many relationship
many-to-many relationship
Definition
There are two data types.
-- 1-1: one member of the first data type maps to one member of the second data type. One member of the second data type maps to one member of the first data type. Example: each person has a social security number, and each social security number has one person

-- 1-to-many: one member of the first data type maps to multiple members of the second data type. Many members of the second data type map to one member of the first data type. Example: one person has many social media links. A social media link only belongs to one person.

-- many-to-many: multiple members of the first data type map to multiple members of the second data type. Example: a person can belong to many groups, and a group can have many people.
Term
What is SQL? How do you call from the database and store to the database? What is the traditional way to use SQL in a Java program?
Definition
-- SQL is a server-side programming language for databases.

-- SELECT calls from the database and INSERT stores in the database

-- The traditional way is to make SQL calls as parameters sent in JDBC function calls
Term
Determine a query for selecting all customers from London.
Definition
SELECT * FROM Customers
WHERE City = 'London'
Term
Determine a query for selecting all customers with CustomerName starting with 'b' and ending with 'n'
Definition
SELECT * FROM Customers
WHERE CustomerName LIKE 'b%n'
Term
What is a relational database? What is a NoSQL database?
Definition
-- A relational database has tables that refer to each other. The tables have records (rows), predetermined properties (columns), and a fixed schema. You can't add tables and columns on the fly.

-- NoSQL does not have a fixed schema. A common type has key-value pairs. It can mean "not SQL" or "not only SQL".
Term
What is ORM, and how does Java Spring use it? (4)
Definition
-- ORM is object-relational mapping. It maps a Java entity class to a relational database table.

-- Each object corresponds to a row and each data member corresponds to a column.

-- Traditionally, you would have to manually query and modify a database directly with SQL. Instead of making SQL queries, you can code in Java using Spring methods like findByX and save.
Term
Define the terms exception and exception handling, and give an example.
Definition
-- An exception is a rare event that deviates from the natural flow of your code. Exception handling is a response to this error.

-- Example: user behavior. If a user puts in invalid input, exception handling prints a message to tell them. Now there is no runtime error and the user can try again.
Term
What are checked and unchecked exceptions? Give an example of each.
Definition
-- A checked exception is an exception that Java requires you to put in your code. You will get a compiler error if you don't. An unchecked exception is not required by Java.

-- FileNotFoundException is a checked exception. ArithmeticException is an unchecked exception.
Term
Difference between throw and throws (2)
Definition
-- Throw: you use throw in the body of your code when you detect that an exception has occurred. Library code often uses "throw", it is less common in application code. When library code uses throw, a program that uses this library must address it with a try-catch.

-- Throws: part of your code has thrown an exception and you don't want to catch it. This passes the exception on to the caller.
Term
How is an exception related to a compiler error, a run-time error, and an assertion?
Definition
-- Compiler error: not including checked exceptions causes a compiler error

-- Run-time error: exceptions are always run-time errors. They are a rare subset of run-time errors.

-- Assertion: used by developers to test that a part of the code is working. Like exceptions, they address errors, but unlike exceptions, they are turned off before deployment.
Term
Advantages of exception handling (4)
Definition
1) Separates error-handling code from normal code, improving readability

2) Propagates errors up the call stack, allowing the function in which the error occurred to return something other than an error message

3) Groups and differentiates error types

4) A simpler alternative to extensive if-else branching
Term
Draw top-down design diagram for Parser project
Definition
See lecture slides
Term
Design by contract
Definition
A methodology whereby each method in a program is specified with pre- and post-conditions, and each class with class invariants, clearly delineating the responsibilities of caller and callee
Term
Class invariant
Definition
A rule that must always be true after any method of the class is executed
Term
pre-conditions (2)
Definition
what should be true about the incoming function parameters, the responsibility of the caller
Term
post-conditions (2)
Definition
what should be true about the return value and by-reference parameters, responsibility of the function itself
Term
What are the parts of an assertion, and how is it coded?
Definition
assert expr1: expr2;
expr1 is a boolean condition
expr2 is the String that will be reported if the assertion fails
Term
advantages of assertions (4)
Definition
1) Catch bugs before they get bigger and harder to find

2) Make code more readable and offer the client more information on how to use the function

3) Coder thinks defensively

4) Can turn them on and off, making catching errors more efficient
Term
How do assertions fit into design-by-contract?
Definition
pre-conditions are coded as assertions at the top of functions
Term
What occurs after a caught exception? Does execution continue?
Definition
Execution of the code will continue, it doesn't kill the process. What occurs is often a String message explaining the error, but it can be anything that gently moves away from the issue.
Term
Document Object Model (3)
Definition
1) The Document Object Model (DOM) is like a map or blueprint of a webpage that web browsers create when they load a website.

2) It represents all the content and structure of the page as objects that developers can interact with.

3) This means you can use the DOM to add, change, or remove elements on a webpage without needing to reload it, making the website more dynamic and interactive for users.
Term
With straight Javascript apps, the code modifies html elements by modifying the DOM directly. How is React’s process different?
Definition
With React, you don’t directly interact with the DOM. Instead you redraw a component or components, and that html/scripting refers to useState variables in {}, which can be modified. So you modify the dynamic data in the useState variables, then re-render. React takes care of modifying the DOM.
Term
Describe the traditional servlet- and template-based web app process, and how the React/JavaScript process is different. (4)
Definition
1) With servlets and templates, each user action triggers a request to the server. The server does some processing, then builds a new version of the page by replacing variables in the template with dynamic data. The entire new page is sent to the client and rendered.

2) React is based on a single-page process.

3) User actions cause the client to call APIs for processing, then update the DOM directly, without redrawing the entire page.

4) More processing is performed on the client side.
Term
What is the difference between these two React statements? (3)

let x = "abc"

const[x, setX] = useState("abc");
Definition
-- Both initialize x to "abc".

-- "Let" is a local variable that will live for one call of the function

-- The useState variable will live for the length of the component, and when it changes, it triggers a redraw of the component
Supporting users have an ad free experience!