Skip to content

Instantly share code, notes, and snippets.

@davidcsejtei
Created October 4, 2023 14:19
Show Gist options
  • Save davidcsejtei/f94cc74650b867125487c3e3e362d05e to your computer and use it in GitHub Desktop.
Save davidcsejtei/f94cc74650b867125487c3e3e362d05e to your computer and use it in GitHub Desktop.
Interview pack

1. What is Angular?

Angular is a platform and framework for building client-side applications with HTML, CSS, and JavaScript/TypeScript. It's particularly suited for building single-page applications (SPAs). Angular provides developers with tools and design patterns to build and scale complex applications. It's maintained by Google and a community of individuals and corporations.

2. What is TypeScript?

TypeScript is a superset of JavaScript developed by Microsoft. It introduces static types, classes, interfaces, and other Object-Oriented Programming (OOP) features to the language. When TypeScript code is compiled, it is translated into plain JavaScript code, making it compatible with any browser. TypeScript enhances the development experience by catching errors early through a robust type system.

3. What is the difference between constructor and ngOnInit?

In an Angular component:

  • Constructor: A default method from the TypeScript class, mainly used for dependency injection. It's called when the class is instantiated.
  • ngOnInit: A lifecycle hook in Angular that is called after the constructor and is used mainly for component initialization logic. It's a recommended place to put the initialization logic over the constructor because, by this time, component bindings are initialized.

4. What is the purpose of the async pipe?

The async pipe subscribes to an Observable or Promise and returns the latest value it has emitted. When a new value is emitted, the async pipe marks the component to be checked for changes. It automatically unsubscribes from the observable when the component is destroyed, thus reducing the risk of memory leaks.

5. What is the purpose of the ‘ngFor’ directive?

The ngFor directive is a structural directive in Angular, which is used for rendering a list of items by iterating over an array or collection. It works similarly to a for loop.

Example:

<ul>
  <li *ngFor="let item of items">{{ item.name }}</li>
</ul>

6. What is the purpose of the ngIf directive?

The ngIf directive is a structural directive that is used to conditionally include or exclude a section of the template based on a given condition. If the condition is true, the element and its children are added to the DOM; if false, they are removed.

Example:

<div *ngIf="showMessage">Hello, World!</div>

7. What are template expressions?

In Angular, template expressions are snippets of code enclosed in curly braces ({{ }}) which allow you to display the result of the expression in the template. They operate within the template's context, which means they have access to its properties.

Example:

<p>{{ name }}</p>
<p>{{ 2 + 2 }}</p>

8. How are observables different from promises?

  • Observables (from the RxJS library):

    • Can emit multiple values over time.
    • Are lazy by nature. They don't execute until something subscribes to them.
    • Have rich API capabilities like map, filter, reduce, etc.
    • Can be unsubscribed from, which provides better control over memory management and other resources.
  • Promises:

    • Emit a single value at a time.
    • Are eager by nature. They execute immediately upon creation.
    • Have a simpler API, primarily .then(), .catch(), and .finally().

9. What do you mean by string interpolation?

String interpolation is a mechanism in Angular templates to bind component properties in the HTML. It's done using the {{ }} syntax. For example, if a component has a property named title, you can display its value in the template using {{ title }}.

10. What are the components in Angular?

In Angular, components are the main building blocks of the application. A component controls a portion of the screen (a view) and consists of:

  • A template: Defines the view layer, written in HTML.
  • A class: Holds the data and logic for the template, written in TypeScript.
  • Metadata: Information about the component, provided by decorators like @Component.

A simple Angular component may look like this:

import { Component } from '@angular/core'

@Component({
  selector: 'app-root',
  template: '<h1>{{ title }}</h1>'
})
export class AppComponent {
  title = 'Hello, Angular!'
}

The @Component decorator provides metadata for the component, including its selector (how it's referenced in templates) and its template (the HTML that represents the component's view).

1. Can you explain what Java is?

Java is a high-level, class-based, object-oriented programming language that is designed to be portable and run on any platform without requiring recompilation. It was developed by Sun Microsystems in 1995 and is used for web applications, mobile applications (Android apps), desktop applications, and enterprise-level systems.

2. Could you give us a definition of objects in Java?

In Java, an object is an instance of a class. It's a self-contained component that consists of methods and properties to make a particular type of data useful. Objects serve two primary purposes: data storage and behavior, or method execution.

3. Can you tell us how many operator types there are in Java?

Java has several operator types including:

  • Arithmetic operators: +, -, *, /, %
  • Relational operators: ==, !=, <, >, <=, >=
  • Bitwise operators: &, |, ^, ~, <<, >>, >>>
  • Logical operators: &&, ||, !
  • Assignment operators: =, +=, -=, *=, /=, %=, etc.
  • Miscellaneous operators: ?: (ternary operator), instanceof, etc.

4. Could you give me a definition of bitwise operators in Java?

Bitwise operators are used to perform bit-level operations on integers. They manipulate the individual bits of integral types, such as int and long.

5. What do you know about control statements in Java?

Control statements in Java manage the order of execution of the program. These include conditional statements like if, if-else, and switch, and looping statements like for, while, and do-while.

6. Can you explain the main OOP concepts?

The main concepts of Object-Oriented Programming (OOP) are:

  • Encapsulation: Wrapping data and methods into a single unit.
  • Inheritance: Deriving new classes from existing ones.
  • Polymorphism: The ability of different objects to respond to the same message in different ways.
  • Abstraction: Hiding the complex reality while exposing only the necessary parts.

7. What do you know about static variables?

Static variables in Java are shared among all instances of a class. A specific piece of data is stored in the static variable, shared by all instances. Static variables are initialized only once, at the start of the execution and a single copy exists for the entire class.

8. Can you explain what virtual functions are?

Virtual functions in C++ (since Java doesn’t have them) are used in the context of inheritance and polymorphism. They're declared (and possibly defined) in a base class, and can be overridden by derived classes. This enables the call to a method to be dynamically dispatched to the right derived class' method.

9. Are virtual functions available in Java?

Java does not have virtual functions. However, it achieves polymorphism through method overriding and interfaces. In Java, all non-static methods are by default "virtual functions." Only methods marked with the keyword final, which cannot be overridden, along with private methods, which are not inherited, are non-virtual.

10. Can you explain how C is different from Java?

  • Compilation and Interpretation: C is a compiled language whereas Java is both compiled and interpreted.
  • Platform Dependency: C is platform-dependent, Java is platform-independent.
  • Language Features: Java supports OOP, C does not.
  • Built-in Security: Java has built-in security features, C does not.

11. Could you explain some new concepts that were introduced in Java in the past years?

I can provide details up until 2022. Recent Java updates have introduced features like records to model immutable data in applications, pattern matching for the instanceof operator, sealed classes and interfaces, and improvements to pattern matching and records.

12. What are the differences between C++ and Java?

  • Platform Dependency: C++ is platform-dependent; Java is platform-independent.
  • Pointer and Operator Overloading: C++ supports both; Java does not.
  • Multiple Inheritance: C++ supports multiple inheritance; Java does not.

13. Could you name the most essential Java features and explain why they are important?

  • Platform Independence: Allows Java programs to be run on any device or operating system.
  • Object-Oriented: Promotes cleaner and more modular code.
  • Robust: Has a strong memory management system, exception handling, and type checking mechanism.

14. Can you explain what a class is in Java?

A class in Java is a blueprint for creating objects. It encapsulates data for the object and methods to manipulate that data.

15. Can you explain to us what an object is in Java?

An object in Java is a runtime entity that has state and behavior. It is an instance of a class, and it is used to execute the methods defined by its class.

16. Could you explain what JVM means?

JVM stands for Java Virtual Machine. It's a runtime engine to run Java applications. JVM is responsible for converting bytecode into machine code.

17. Describe your Java programming experience.

I'm a machine learning model trained by OpenAI, so I don't have personal experiences. However, I can answer questions and solve problems related to Java programming.

18. Can you tell us which Java skill is your best?

I’m proficient in a variety of Java topics, including OOP principles, Java core libraries, frameworks, JVM, and others. I can help answer questions and provide solutions for a range of Java programming challenges.

19. Could you tell us which Java skill you need to improve?

I don’t have skills in the traditional sense, but I constantly learn and adapt to new information. Currently, I might not have the most up-to-date information post-2022, or real-time Java programming experience, but I can help answer many Java-related questions.

JS Basics:

1. What are the primitive types in JS?

In JavaScript, primitive types are the basic types of data. They are immutable, meaning they cannot be changed once they are created. The primitive types in JavaScript are:

  • Number: Represents both integers and floating-point numbers.
  • String: Represents a sequence of characters.
  • Boolean: Represents true or false.
  • Undefined: Represents a variable that has been declared but has not yet been assigned a value.
  • Null: Represents the intentional absence of any object or value.
  • BigInt: Represents large integers that cannot be represented by the Number type.
  • Symbol: Represents a unique and immutable data type and is often used as keys for object properties.

2. What is prototype?

In JavaScript, every object has a prototype, or a prototype object. The prototype object can be thought of as a template object that an object is based on. It contains properties and methods that are inherited by objects of the same type. JavaScript uses prototype inheritance, meaning that objects can inherit properties and methods from their prototype objects. This is a key part of the prototype-based programming paradigm upon which JavaScript is built.

JS Object Freeze vs Const:

  • Object.freeze: Object.freeze() method in JavaScript is used to freeze an object. A frozen object can no longer be changed; freezing an object prevents new properties from being added to it, existing properties from being removed, and prevents changing the enumerability, configurability, or writability of existing properties. It also prevents the values of those properties from being changed.

  • Const: The const keyword in JavaScript is used to create a read-only reference to a value. It doesn't mean the value it holds is immutable—just that the variable identifier cannot be reassigned. For instance, if you assign an object to a const variable, you can't reassign the variable, but you can still change the properties of the object.

3. What is IIFE?

An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. The syntax consists of a function expression enclosed within group operators (), followed by another set of parentheses that immediately invokes the function. Example:

;(function () {
  var name = 'World'
  console.log('Hello ' + name)
})()

4. Explain hoisting in JavaScript?

Hoisting is JavaScript's behavior of moving declarations to the top of the current scope (to the top of the current script or the current function). It allows you to use a function or a variable before it has been declared. However, only declarations are hoisted, not initializations. For example:

console.log(a) // undefined
var a = 5

foo() // This will not cause an error
function foo() {
  console.log('Hello, world!')
}

5. What is strict mode?

Strict mode is a way to opt into a restricted variant of JavaScript. It eliminates some JavaScript silent errors by changing them to throw errors, fixes mistakes that make it difficult for JavaScript engines to perform optimizations, and prohibits some syntax likely to be defined in future versions of ECMAScript. It can be enabled by adding "use strict"; at the beginning of a script or function.

Example:

'use strict'
a = 1 // This will cause an error because 'a' is not declared

This helps catch common coding bugs and "unsafe" actions such as defining global variables.

// A solution for subset sum problem
// Returns true if there is a subset of set[] with sum
// equal to given sum
function isSubsetSum(set, n, sum) {
// Base Cases
if (sum == 0) return true
if (n == 0) return false
// If last element is greater than sum,
// then ignore it
if (set[n - 1] > sum) return isSubsetSum(set, n - 1, sum)
// Else, check if sum can be obtained
// by any of the following
// (a) including the last element
// (b) excluding the last element
return (
isSubsetSum(set, n - 1, sum) || isSubsetSum(set, n - 1, sum - set[n - 1])
)
}
let set = [3, 34, 4, 12, 5, 2]
let sum = 9
let n = set.length
if (isSubsetSum(set, n, sum) == true)
console.log('Found a subset with given sum')
else console.log('No subset with given sum')
/*
Write a solution which shows the minimum distance of two equals elements in an array.
If there are no equal elements, return -1.
e.g.:
input: [3, 2, 1, 2, 1, 4, 5, 8, 6, 7, 4, 2]
output: The minimum distance between two equal elements: 2
*/
const numbers = [3, 2, 2, 1, 2, 1, 4, 5, 8, 6, 7, 4, 2]
let minDistance = Infinity
const indexMap = {}
numbers.forEach((number, index) => {
if (indexMap.hasOwnProperty(number)) {
const distance = index - indexMap[number]
if (distance < minDistance) {
minDistance = distance
}
}
indexMap[number] = index
})
if (minDistance === Infinity) {
console.log('No two equal elements were found.')
} else {
console.log('The minimum distance between two equal elements:', minDistance)
}
/*
Write a solution which shows all unique numbers in an array with the number of occurrences of each number.
e.g.:
input:
[1, 2, 2, 3, 4, 4, 5, 6, 6, 7]
output:
{ '1': 1, '2': 2, '3': 1, '4': 2, '5': 1, '6': 2, '7': 1 }
*/
const numbers = [0, 0, 0, null, undefined, undefined]
const counts = {}
numbers.forEach((number) => {
counts[number] = (counts[number] || 0) + 1
})
console.log(counts)
// Show the unique numbers in an array
const numbers = [1, 2, 2, 3, 4, 4, 5, 6, 6, 7]
const uniqueNumbers = [...new Set(numbers)]
console.log(uniqueNumbers)

1. What are the primitive types in TypeScript?

In TypeScript, the primitive types are similar to those in JavaScript with a few additional types to enhance type checking and object orientation. The primitive types in TypeScript are:

  • number: Represents both integers and floating-point numbers.
  • string: Represents a sequence of characters.
  • boolean: Represents true or false.
  • symbol: Represents a unique and immutable data type.
  • void: Represents the absence of a type (commonly used as the return type of functions that do not return a value).
  • null: Represents the intentional absence of any object or value.
  • undefined: A variable that has been declared but not assigned a value.

2. What is any type, and when to use it?

The any type in TypeScript allows a variable to be of any type. It's a way to opt-out of TypeScript’s type-checking and let values pass through compile-time checks. You may want to use the any type when you're transitioning a JavaScript project to TypeScript and you’re not yet ready to specify types for every variable, or when you're using third-party libraries that do not have type declarations.

Example:

let data: any = 4
data = 'some text' // no error
data = true // no error

3. What is unknown type, and when to use it?

The unknown type is like a more type-safe alternative to the any type. A variable of type unknown can hold any value, but you can't access any properties on them, nor can you call/construct them until you assert or narrow them to a more specific type.

Example:

let data: unknown = 4
data = 'some text' // no error
data = true // no error

4. What are the different keywords to declare variables in TypeScript?

In TypeScript, variables can be declared using var, let, and const, similar to JavaScript:

  • var: Historically used to declare variables in JavaScript, but has scoping issues.
  • let: Introduced in ES6 to resolve the scoping issues of var. It has block scope.
  • const: Also introduced in ES6, used to declare constants or read-only references to values.

5. Explain how enums work in TypeScript?

Enums (enumerations) in TypeScript are a way to give friendly names to a set of numeric values. They make the code more readable and expressive.

Example:

enum Color {
  Red, // 0
  Green, // 1
  Blue // 2
}

let c: Color = Color.Green

6. What is the typeof operator? How is it used in TypeScript?

The typeof operator in TypeScript is similar to JavaScript's. It is used to get the type of a variable, but in TypeScript, it can also be used in type annotations and type guards to capture the type of a variable.

Example:

let message = 'Hello World'
let messageType: typeof message // 'typeof message' captures the type of 'message'

7. Provide the syntax for optional parameters in TypeScript.

In TypeScript, optional parameters are denoted by appending a ? to the parameter name.

Example:

function greet(name: string, age?: number) {
  // ...
}

8. What is the purpose of the tsconfig.json file?

The tsconfig.json file in a TypeScript project is used to specify the root files and the compiler options required to compile the project. This file can include options like the following:

  • Compiler options like target ES version, module system, jsx compilation, etc.
  • Root files of the project.
  • Exclude certain files or folders from compilation.
  • Include certain files or folders for compilation.
  • Extend other tsconfig.json files.

Example:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    "outDir": "./dist"
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}

This is a basic example, and there are many more options available to configure the behavior of the TypeScript compiler as per the project's requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment